【问题标题】:Configuring cors in spring boot application. Bean CorsConfigurationSource doesnt work在 Spring Boot 应用程序中配置 cors。 Bean CorsConfigurationSource 不起作用
【发布时间】:2019-08-25 07:17:38
【问题描述】:

所以我无法配置 cors。由于错误,我的 Angular 应用程序无法发送 api 请求。 我可以通过soapUI 提出请求,它们工作正常。但从浏览器有:

从源“http://localhost:4200”访问“http://localhost:8080/backend/api/public”处的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。

我正在寻找答案,它们看起来像我的班级。

我正在使用 Spring Boot 网络安全。

@EnableWebSecurity
@Configuration
public class AuthConfig extends WebSecurityConfigurerAdapter {

    @Value(value = "${auth0.apiAudience}")
    private String apiAudience;
    @Value(value = "${auth0.issuer}")
    private String issuer;


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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        JwtWebSecurityConfigurer
                .forRS256(apiAudience, issuer)
                .configure(http)
                .authorizeRequests()
                .antMatchers(HttpMethod.GET,"/api/public").permitAll()
                .antMatchers(HttpMethod.GET, "/api/private").authenticated()
                .antMatchers(HttpMethod.GET, "/api/private-scoped").hasAnyAuthority("read:posts");
    }
}

这个 bean 应该添加这个 cors 头,但它没有。也许您知道这样做的更好主意吗? beetwen WebSecurityConfigurerAdapter 和 WebMvcConfigurerAdapter 有什么区别?

【问题讨论】:

    标签: java spring websecurity


    【解决方案1】:

    根据this,您也应该在configure() 方法中添加.cors().and()

    还有其他可能的解决方案,例如通过设置特定于每个端点的 CORS 配置,如 here 所示。

    如果您想为整个应用程序启用 CORS,第一种方法更漂亮。

    WebSecurityConfigurerAdapter 和 WebMvcConfigurerAdapter 之间的区别在于,WebSecurityConfigurerAdapter 用于配置与您的 web 应用程序的安全性相关的任何内容,例如身份验证和授权。使用 WebMvcConfigurerAdapter,您可以为 Spring MVC 自定义基于 Java 的配置。

    【讨论】:

    • 我尝试了第一种方法,当我在做 http.cors() 时,我的 ide 告诉我它找不到符号。
    • 我可以给你链接到我的仓库
    • 当然,但前提是您对此感到满意。你确定你导入了正确的 HttpSecurity 吗?对我来说,Spring Boot 2.0.0 中的 org.springframework.security.config.annotation.web.builders.HttpSecurity 有方法 cors()
    • 是的,进口是coreet
    • 你的 spring-security-config 依赖是旧的,不支持 cors 设置。对于您当前使用的版本(3.2.4),我也无法使用cors() 方法。删除标签或升级到至少版本4.2.11 应该可以解决问题。我建议只删除它,因为它只会使用父母的版本。
    【解决方案2】:

    我遇到了同样的问题,我无法让 CorsConfigurationSource bean 工作。所以我尝试了不同的方法。我没有使用 CorsConfigurationSource bean 配置。相反,我使用了 cors 注册表替代方案或全局 CORS 配置。

    @Configuration
    @EnableWebMvc
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                .allowedOrigins("http://localhost:4200")
                .allowCredentials(true);
        }
    }
    

    从 AuthConfig 类中删除 CorsConfigurationSource 配置 bean。 我从https://www.baeldung.com/spring-cors得到了这个截断的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-26
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      相关资源
      最近更新 更多