Spring Boot與Spring Security整合後post數據不了,403拒絕訪問

http://blog.csdn.net/sinat_28454173/article/details/52251004html

**********************************************************java

 最近在學習spring security與spring boot的整合,剛開始學習了登陸和註銷,想本身拓展一下,post一些數據,完成數據庫的操做。mysql

開始長達一天的查找資料解決問題中!!!git

  1. 首先:403錯誤,表示資源不可用。服務器理解客戶的請求,但拒絕處理它,一般因爲服務器上文件或目錄的權限設置致使的WEB訪問錯誤。
  2. 瞭解了錯誤後,大概就是我用戶權限不夠吧。當我登陸之後,以admin權限去操做post仍是同樣的錯誤。
  3. 因而去configure方法中找,看看是否是能夠設置接收post操做
@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/test","/test1").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                    .loginPage("/login")
                    .failureUrl("/login?error")
                    .permitAll()
                .and()
                .logout().permitAll();
    }

 

最終沒有任何頭緒。
4. 因而懷疑框架自身的抑制,果真被我找到了:
解決CsrfFilter與Rest服務Post方式的矛盾
具體就是框架內部防止CSRF(Cross-site request forgery跨站請求僞造)的發生,限制了除了get之外的大多數方法。
5. 可是上述的解決方法是直接過濾掉指定的url,使其可使用post以及其餘被限制的方法,是一個漏洞!!!何況講的是spring mvc 的配置文件方法。可是在Spring boot上還不知道怎麼解決。。
6. 因而想到了去Spring Boot的文檔看看與security相關的操做:
github上的文檔:security例子裏的security
簡約到本身都不相信了。。
7.因而想到直接去看Spring security文檔好了:
Using Spring Security CSRF Protectiongithub

 

差點就想着把csrf的功能關掉。。。還好往下看到:
Include the CSRF Tokenspring

 

好了,終於找到解決辦法了:只要添加這個token,後臺就會驗證這個token的正確性,若是正確,則接受post訪問。
8.可是又出現問題了。。我用的是thymeleaf模板:
Put CSRF into Headers in Spring 4.0.3 + Spring Security 3.2.3 + Thymeleaf 2.1.2
在stackoverflow上查到了。只要改一下:sql

這樣,就能夠完成本身的delete,put,post,patch方法的訪問了

9.【springboot折騰記】之後有問題,若是網上找不到解決辦法,就去找文檔吧,雖然是英文的,可是看着看着就習慣了!!!推薦一款chrome的插件:《TransIt》簡單好用的詞典,雙擊英文單詞便可查詢。chrome

 

參考數據庫

springboot security CSRF問題
http://blog.csdn.net/a517858177/article/details/52414181

Spring Security筆記:解決CsrfFilter與Rest服務Post方式的矛盾
http://www.cnblogs.com/yjmyzz/p/customize-CsrfFilter-to-ignore-certain-post-http-request.html

 基於java config的springSecurity(三)--加入RememberMe,啓用CSRF和加強密碼
http://blog.csdn.net/xiejx618/article/details/42626001springboot

相關文章
相關標籤/搜索