2016-03-27 11 views
0

に春のセキュリティ保護から1つのURIを除外するために失敗私は、次のようなHttpSecurityがありますはPOSTリクエスト

http 
    .exceptionHandling().and() 
    .anonymous().and() 
    .servletApi().and() 
    .headers().cacheControl().and().and() 
    .authorizeRequests() 
     // Need authentication sur POST 
     .antMatchers(HttpMethod.POST).authenticated() 
     // Allow anonymous resource requests 
     .anyRequest().permitAll().and() 
    // Custom Token based authentication based on the header previously given to the client 
    .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), 
         UsernamePasswordAuthenticationFilter.class) 
    // Filtre pour gérer la clef d'api 
    .addFilterBefore(new ApiKeyFilter(), StatelessAuthenticationFilter.class) 
    // Filtre pour ajouter les headers nécessaires à la consommation par les différents clients 
    .addFilterBefore(new CorsFilter(), ApiKeyFilter.class); 

問題は、私は403 Forbidden応答を得ることなく/userPOSTにできるようにする必要があるということです。そこで、/userに匿名リクエストを許可するルールを追加して、POSTとしました。

.antMatchers(HttpMethod.POST, "/user").anonymous() 

が、問題は、私はこれを追加する場所がわからないということです、私は.antMatchers(HttpMethod.POST, "/user").anonymous()の前と後にしようとしたが、私はまだ403を取得するので、それは無視されているように思える:

私はこの antMatcherを使用しました。

答えて

0

antMatchers(HttpMethod.POST).authenticated()の前にルールを追加してください。また、デフォルトではCSRFの保護が有効になっているため、POSTリクエストにのCSRFトークンを指定するか、どちらかを指定する必要があります。ここで私はちょうどCSRFを無効にしています:

// Same as before 
.authorizeRequests() 
    .antMatchers(HttpMethod.POST, "/user").permitAll() 
    // Need authentication sur POST 
    .antMatchers(HttpMethod.POST).authenticated() 
    // Allow anonymous resource requests 
    .anyRequest().permitAll() 
.and 
.csrf().disable(); 
// Same as before 

あなたはSpring Security DocumentationにCSRFについての詳細を読むことができます。