一、什麼是CSRF?
已經有不少博文講解其過程和攻擊手段,在此就不重複了。 O(∩_∩)O 不清楚的同窗,請自行搜索或按連接去了解: http://blog.csdn.net/Flaght/article/details/3873590php
二、CSRFGuard_Project
開源項目 CSRFGuard,介紹瞭如何使用在 HTTP 請求中加入 token 並驗證的方法來抵禦 CSRF。 https://www.owasp.org/index.php/Category:OWASP_CSRFGuard_Projecthtml
三、檢測CSRF方法?
OWASP上面有一個叫作CSRFTester的工具,能夠構建進行測試。
下載連接:https://www.owasp.org/index.php/CSRFTester
教程指引:http://www.zyiqibook.com/article216.htmlnginx
四、如何防護CSRF?
咱們採用CSRFGuard_Project方案 JAVA DOM方式。
配置以下:web
1)引入csrfguard-3.1.0.jar到你的工程: 2)配置web.xml:安全
<!-- 基於複雜方案OWASP CsrfGuard的CSRF安全過濾 --> <servlet> <servlet-name>JavaScriptServlet</servlet-name> <servlet-class>org.owasp.csrfguard.servlet.JavaScriptServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>JavaScriptServlet</servlet-name> <url-pattern>/JavaScriptServlet</url-pattern> </servlet-mapping> <context-param> <param-name>Owasp.CsrfGuard.Config</param-name> <param-value>WEB-INF/csrfguard.properties</param-value><!-- 配置文件 --> </context-param> <context-param> <param-name>Owasp.CsrfGuard.Config.Print</param-name> <param-value>true</param-value> </context-param> <!-- session監聽器 --><!-- 基於複雜方案CsrfGuard的session監聽器 --> <listener> <listener-class>org.owasp.csrfguard.CsrfGuardHttpSessionListener</listener-class> </listener> <listener> <listener-class>org.owasp.csrfguard.CsrfGuardServletContextListener</listener-class> </listener> <filter> <filter-name>CSRFGuard</filter-name> <filter-class>org.owasp.csrfguard.CsrfGuardFilter</filter-class> </filter> <filter-mapping> <filter-name>CSRFGuard</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3)引入配置文件csrfguard.js csrfguard.properties(jar包META-INF裏面有) csrfguard.properties配置文件中有幾點須要注意:session
能夠指定加載的js,爲方便管理,我放在WEB-INF目錄下:app
org.owasp.csrfguard.JavascriptServlet.sourceFile = WEB-INF/csrfguard.js
org.owasp.csrfguard.TokenName 對於nginx分發多servers的狀況,nginx默認過濾帶下劃線的header,要麼改掉nginx的配置,要麼改這個參數的值。 參考:http://www.ttlsa.com/nginx/nginx-proxy_set_header工具
#>>>>>>>>>>>>>--author by caizhengluan e-mail: SvenAugustus@outlook.com #Nginx ignores the underlined header variables by default. # Please do not set "TokenName" has any underlines if you can't unkonwn how to change nginx's configuration. org.owasp.csrfguard.TokenName=PAYCSRFTOKEN #<<<<<<<<<<<<<--author by caizhengluan e-mail: SvenAugustus@outlook.com
org.owasp.csrfguard.JavascriptServlet.refererMatchDomain 對於nginx分發多servers的狀況,須要設置爲false測試
#>>>>>>>>>>>>>--author by caizhengluan e-mail: SvenAugustus@outlook.com # If you have cluster and nginx, please set this value to false.Otherwise, you will can't visit into the servers when you access nginx address. org.owasp.csrfguard.JavascriptServlet.refererMatchDomain = false #<<<<<<<<<<<<<--author by caizhengluan e-mail: SvenAugustus@outlook.com
四、如何具體防護DWR和咱們的頁面? 在你的頁面最後加上這麼一句:this
<!-- OWASP CSRFGuard JavaScript Support --> <script src="<%=request.getContextPath()%>/JavaScriptServlet"></script>
轉載自:https://my.oschina.net/langxSpirit/blog/678901