Spring boot 使用spring security OAuth设置CORS标头
我正在尝试为OAuth Rest API设置CORS头:Spring boot 使用spring security OAuth设置CORS标头,spring-boot,spring-security,Spring Boot,Spring Security,我正在尝试为OAuth Rest API设置CORS头: @Configuration @EnableWebSecurity @EnableResourceServer public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { @Bean CorsConfigurationSource corsConfigurationSource() { CorsCon
@Configuration
@EnableWebSecurity
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.authorizeRequests()
.antMatchers("/oauth/token", "/oauth/authorize**", "/publica")
.permitAll();
http.requestMatchers().antMatchers("/funds/**").and().authorizeRequests().antMatchers("/funds/**")
.access("hasRole('USER')");
...
但是,当我访问/oauth/token时,我在响应(Postman、localhost)中没有看到CORS头:
无CORS标头,例如访问控制允许来源::(
此外,我希望此设置也适用于所有路由(例如/funds),但只是尝试首先使/oauth/token路由工作
我的位置是否正确?我如何获取要为此/oauth/令牌路由(和其他路由)设置的CORS头?据我所知,如果定义了默认的CORSCOConfiguration源,则应该选择它。您可以使用注释
@CrossOrigin(origins=“*”,maxAge=3600)方法头上的
。但我不拥有映射到/oauth/token的方法,因此我不能使用它,它是Spring Security的一部分。我只能将其应用于我自己的路由(例如/funds/**)哦,对了,所以你可以实现一个过滤器,这样你就可以添加cors响应和/或配置
谢谢。这样似乎给了我很多对响应的控制。我遵循了这个示例-很酷,很高兴听到你有一个解决方案:)