【发布时间】: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