【问题标题】:Cannot allow crossOrigin in spring security春季安全中不能允许 crossOrigin
【发布时间】:2017-11-08 16:12:15
【问题描述】:

我有一个带有 oauth2 身份验证的 spring-rest 应用程序。一切正常,我可以收到一个令牌并用它来验证和所有这些东西。现在我正在使用 Angular2 开发应用程序的前端。

这里的主要问题是:如何在我的 Oauth2 安全配置中允许 CORS? 我已经设法通过 @CrossOrigin 注释在我的 Controller 类中允许它,但它在安全配置中是如何工作的?

我试过这个:

@Configuration
@EnableWebSecurity
public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
        .csrf().disable()
        .anonymous().disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll()
        .and()
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }
    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000"));
        configuration.setAllowedMethods(Arrays.asList("GET","POST"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

但我仍然收到错误:

请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'http://localhost:3000' 不允许访问。响应的 HTTP 状态代码为 401。

【问题讨论】:

标签: java spring spring-boot spring-security oauth-2.0


【解决方案1】:

根据 Spring 文档,CorsConfigurationSource 不是用作 bean,而是在 CorsFilter 中使用,请参阅 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html#_filter_based_cors_support

public class MyCorsFilter extends CorsFilter {

    public MyCorsFilter() {
        super(configurationSource());
    }

    private static UrlBasedCorsConfigurationSource configurationSource() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("http://domain1.com");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return source;
    }
}

另一种方法是在 WebMvcConfigurerAdapter 中提供 cors 映射,请参阅 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cors.html#_javaconfig

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
            .allowedOrigins("http://domain2.com")
            .allowedMethods("PUT", "DELETE")
            .allowedHeaders("header1", "header2", "header3")
            .exposedHeaders("header1", "header2")
            .allowCredentials(false).maxAge(3600);
    }
}

【讨论】:

  • 我正在使用 WebSecurityConfigurerAdapter。 这个类中没有名为“addCorsMappings”的方法。如果我使用第一种方法,我会在我的配置类中添加什么?我添加了一个名为 MyCorsFilter 的新类,但我不知道如何使用它
  • WebSecurityConfigurerAdapter 中没有方法 addCorsMapping(),因为 CORS 不是 Spring 安全性的一部分。只需按照文档中的说明添加WebMvcConfigurerAdapter
  • 我仍然遇到同样的错误,我已经尝试了这两种方法,但它们似乎都不起作用。我仍然可以向邮递员发出请求,但不能在我的 Angular 应用程序中提出请求
猜你喜欢
  • 2015-06-02
  • 2013-12-22
  • 2014-02-02
  • 2012-12-13
  • 2017-11-30
  • 2021-05-12
  • 2019-03-31
相关资源
最近更新 更多