http://blog.csdn.net/sinat_28454173/article/details/52251004
**********************************************************
最近在學習spring security與spring boot的整合,剛開始學習了登錄和注銷,想自己拓展一下,post一些數據,完成數據庫的操作。
開始長達一天的查找資料解決問題中!!!
- 首先:403錯誤,表示資源不可用。服務器理解客戶的請求,但拒絕處理它,通常由於服務器上文件或目錄的權限設置導致的WEB訪問錯誤。
- 了解了錯誤后,大概就是我用戶權限不夠吧。當我登錄以后,以admin權限去操作post還是一樣的錯誤。
- 於是去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 Protection
差點就想着把csrf的功能關掉。。。還好往下看到:
Include the CSRF Token
好了,終於找到解決辦法了:只要添加這個token,后台就會驗證這個token的正確性,如果正確,則接受post訪問。
8.但是又出現問題了。。我用的是thymeleaf模板:
Put CSRF into Headers in Spring 4.0.3 + Spring Security 3.2.3 + Thymeleaf 2.1.2
在stackoverflow上查到了。只要改一下:
這樣,就可以完成自己的delete,put,post,patch方法的訪問了
9.【springboot折騰記】以后有問題,如果網上找不到解決辦法,就去找文檔吧,雖然是英文的,但是看着看着就習慣了!!!推薦一款chrome的插件:《TransIt》簡單好用的詞典,雙擊英文單詞即可查詢。
參考
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/42626001