当前位置: 首页 > 知识库问答 >
问题:

启用跨源请求Spring引导

叶华皓
2023-03-14

我正在开发一个应用程序,它由一个用Spring Boot开发的后端应用程序和一个用Angular 8开发的前端应用程序组成。现在,我想启用Cross Origin来让前端调用后端的API。
根据Spring关于CORS的文档,我编写了以下配置类:

@EnableWebSecurity
public class WebConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors();
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource()
    {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

第一次尝试是将AllowedOrigins设置为arrays.aslist(“*”)

当我从Angular应用程序调用api/dinners/cartitions时,我在控制台中发现了以下错误:

CORS策略阻止了从来源“http://localhost:8081/dinners/capitions?page=0&pageize=10"访问来自”http://localhost:4200“的XMLHttpRequest:请求的资源上没有”access-control-allog-origin“标头。
获取http://localhost:8081/dinners/capitions?page=0&pageize=10 net::err_failed

共有1个答案

葛玉堂
2023-03-14

您发布的文档引用了spring security 4.2(您可以在您提供的url中看到这一点),如果您查看最近的spring security文档(5.x),您将看到,为了用外部bean配置标准实现的cors过滤器,您需要调用withdefaults()方法。

Cors配置Spring5.x

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // by default uses a Bean by the name of corsConfigurationSource
            .cors(withDefaults()) // <------ this part
            ...
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("https://example.com"));
        configuration.setAllowedMethods(Arrays.asList("GET","POST"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}
 类似资料:
  • 我正在工作的角度前端项目和使用angular7项目。我使用aws api作为后端。每当我调用aws api时,它都会给我带来以下错误。 跨源请求被阻止:同一源策略不允许读取https://xxxxxxx.execute-api.us-east-1.amazonaws.com/xxx/subscriptionPayment上的远程资源。(原因:在CORS飞行前信道的CORS标头'Access-Con

  • 如果我们向另一个网站发送 fetch 请求,则该请求可能会失败。 例如,让我们尝试向 http://example.com 发送 fetch 请求: try { await fetch('http://example.com'); } catch(err) { alert(err); // fetch 失败 } 正如所料,获取失败。 这里的核心概念是 源(origin)—— 域(domai

  • 当我使用axios发送XMLHttpRequest时,就会发生这种情况。 错误:XMLHttpRequest无法加载https://nuxt-auth-routes.glitch.me/api/login。对飞行前请求的响应没有通过访问控制检查:在所请求的资源上没有“访问-控制-允许-起源”标头。因此,不允许访问源'http://localhost:3000'。

  • 我使用的是Spring boot,这里是maven依赖项 我试图用Angular创建一个HTTP请求帖子(我也有一个工作正常的GET请求),但我得到了这个 在响应头中 我意识到在响应中,allow没有POST方法。 控制器中的方法

  • 我有一个web应用程序,我正在使用Angular 2和Spring Boot开发。我使用依赖项将存储库公开为HTTPendpoint。 在开发过程中,我在运行端口8080的本地tomcat上运行后端spring boot项目。为了开发前端,我使用angular-cli在端口4200上为我的Angular2应用程序提供服务。我在4200上运行的前端需要能够到达在8080上公开的endpoint,但这

  • 因此,当我试图使用React将数据发送到后端时,我遇到了这个错误。据我所知,我需要允许在后端和文件中进行通信。以下是我使用的一些链接: 无“访问控制允许源”-节点/Apache端口问题 访问控制允许起源头是如何工作的? 他们两个都有代码,但都没用。 到目前为止,我的服务器端代码如下: 这是我的客户端代码: 那么我错过了什么?我没有文件,但我都是在本地完成的,所以我不确定是否可以使用它。 在我看来,