2017-06-07 8 views
0

私は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.

+0

https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – StanislavL

答えて

-1

を、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; 
    } 
} 

もう1つの方法は、 ORS WebMvcConfigurerAdapterでのマッピングは、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); 
    } 
} 
+0

を見ます** WebSecurityConfigurerAdapterを使用しています**このクラスには "addCorsMappings"というメソッドはありません。私が最初のアプローチを使用する場合、私は自分の設定クラスに何を追加するのですか? MyCorsFilterという新しいクラスを追加しましたが、使用方法はわかりません。 –

+0

CORSはSpringセキュリティの一部ではないため、 'WebSecurityConfigurerAdapter'には' addCorsMapping() 'メソッドがありません。ドキュメントで説明されているように 'WebMvcConfigurerAdapter'を追加するだけです。 –

+0

私はまだ同じエラーが出ている、私は両方のアプローチを試みたが、どれもうまくいかないようだ。私は依然として郵便配達員に依頼することができますが、角度のあるアプリでは依頼できません –

関連する問題