私はoauth2認証のスプリングレストアプリケーションを持っています。すべてうまく動作し、私はトークンを受け取ることができ、それを認証するために使用します。今私はAngular2でアプリケーションのフロントエンドを開発しています。春のセキュリティでcrossOriginを許可することはできません
主な問題は次のとおりです.Outh2のセキュリティ設定でCORSを許可するにはどうすればよいですか。 @CrossOrigin注釈を使用してコントローラクラスで許可することができましたが、セキュリティ設定ではどのように機能しますか?
私はこの試みている:
@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;
}
}
を私はまだエラーました:CorsConfigurationSource
がBeanとしてではなくCorsFilter
で使用されていない春のドキュメントによると
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:3000 ' is therefore not allowed access. The response had HTTP status code 401.
https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – StanislavL