【问题标题】:CORS filter does not apply for spring oauth/tokenCORS 过滤器不适用于 spring oauth/token
【发布时间】:2018-02-23 09:20:15
【问题描述】:

我的过滤器有以下配置。在向localhost:8080/oauth/token 发出 CORS 请求时,OPTIONS 请求似乎没有通过。

@Bean
  public FilterRegistrationBean corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*"); // @Value: http://localhost:8080
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(-1);
    return bean;
  }

@Override
protected void configure(HttpSecurity http) throws Exception{
    http.csrf().disable().authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll().antMatchers(HttpMethod.OPTIONS, "/**").permitAll();
}

我是否遗漏了配置中的某些内容?

【问题讨论】:

    标签: spring-boot cors


    【解决方案1】:

    问题在于过滤器的顺序应该是这样设置的:

       bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
    

    【讨论】:

      猜你喜欢
      • 2018-03-09
      • 2015-04-03
      • 2021-04-07
      • 2018-09-16
      • 1970-01-01
      • 2018-10-16
      • 2020-02-08
      • 2014-02-10
      • 2016-11-10
      相关资源
      最近更新 更多