2016-08-30 72 views
0

我使用spring-boot和spring-security構建項目,使用自定義登錄頁面404次錯誤,我想要做的是分離登錄頁面和項目,並驗證在登錄頁面上使用Ajax URL發送的用戶信息,但總是出錯403,我更改了spring boot security configue的配置信息,但沒有具體的效果。下面的代碼:彈簧引導,自定義登錄頁面,錯誤403

@Override 
public void configure(HttpSecurity http) throws Exception{ 
    http.authorizeRequests() 
      .anyRequest().authenticated() 
      .antMatchers("/index.html").permitAll() 
      .and() 
      .formLogin().usernameParameter("username") 
      .passwordParameter("password") 
      .loginProcessingUrl("/login") 
      .loginPage("/login") 
      .failureUrl("/login?error") 
      .permitAll() 
      .and() 
      .logout().permitAll(); 
} 

發送POST網址:本地主機:8888 /登錄
PARAMS:用戶名,密碼
響應:403

錯誤消息:

此應用程序沒有明確的映射爲/錯誤,所以您將此視爲後備。
有一個意外的錯誤(type = Forbidden,status = 403)。
未找到預期的csrf標記。你的會話過期了嗎?

回答

0

你必須配置post來包含Spring Security的csrf標記。您必須在POST請求中設置標題參數「X-CSRF-TOKEN」。示例POST請求:

$.ajax({ 
    type: "POST", 
    beforeSend: function (request) 
    { 
     request.setRequestHeader("X-CSRF-TOKEN", "${_csrf.token}"); 
    }, 

    ///... 

});