2016-04-22 17 views
0

こんにちは、私は春のセキュリティに新しく、here Mkyong私はこのチュートリアルで紹介したアノテーションベースの例を使用していますが、ログインフォームに値を入力して、以下のように[送信]ボタンをクリックすると常に例外が発生します。エラーHTTPステータス403 - 無効なCSRFトークン「_csrf」リクエストパラメータまたは「X-CSRF-TOKEN」ヘッダーに「null」が見つかりました

type Status report 

message Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'. 

description Access to the specified resource has been forbidden. 

私はgoogleを試しましたが、適切な答えが見つからなかったので、この問題を解決しました。セキュリティ設定クラスは以下の通りです。

SecurityConfig.java

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    @Qualifier("authenticationProvider") 
    AuthenticationProvider authenticationProvider; 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(authenticationProvider); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

     http.authorizeRequests().antMatchers("/admin/**") 
      .access("hasRole('ROLE_USER')").and().formLogin() 
      .loginPage("/login").failureUrl("/login?error") 
       .usernameParameter("username") 
       .passwordParameter("password") 
       .and().logout().logoutSuccessUrl("/login?logout").and().csrf(); 
    } 

} 

のlogin.jspフォームが、私はこの問題から出てくるように助けてください

<form name='loginForm' 
      action="<c:url value='/login'/>" method='POST' enctype="multipart/form-data"> 

      <table> 
       <tr> 
        <td>User:</td> 
        <td><input type='text' name='username'></td> 
       </tr> 
       <tr> 
        <td>Password:</td> 
        <td><input type='password' name='password' /></td> 
       </tr>  
       <tr>     
        <td colspan='2'><input name="submit" type="submit" 
         value="submit" /></td> 
       </tr> 
      </table> 
<input type="hidden" name="${_csrf.parameterName}" 
      value="${_csrf.token}" /> 
     </form> 

を下回っています。

+1

私はCSRFを使用する質問 –

答えて

3

あなただけの代わりにあなたの元をこの

<form name='loginForm' 
      action="<c:url value='/login'/>" method='POST'> 

を使用する必要があり、セキュリティの設定に以下のコードを追加して、それが

.and() 
      .csrf().disable(); 
+0

であなたのweb.xmlを示し、このエラーは私がCSRFを無効にし、securityConfigであれば来て、行を削除する文句を言いませんログインページから

1

問題がマルチパートのフォームタイプである任意の差分を作るかどうかを確認してください以下

フォームタイプをマルチパートフォームとして使用する必要がある場合は、それは春のセキュリティ設定の前に追加されていることを保証するweb.xmlにrtFilter:

<filter> 
    <display-name>springMultipartFilter</display-name> 
    <filter-name>springMultipartFilter</filter-name> 
    <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>springMultipartFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
関連する問題