- package com.web.interceptors;
-
- import org.apache.log4j.Logger;
- import org.apache.struts2.ServletActionContext;
-
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
- import com.trace.web.utils.Constants;
-
- public class AuthInterceptor extends AbstractInterceptor {
-
- private static final long serialVersionUID = -2154056039548254482L;
-
- private static Logger log = Logger.getLogger(AuthInterceptor.class);
-
- @Override
- public String intercept(ActionInvocation invocation) throws Exception {
- String referer = ServletActionContext.getRequest().getHeader("Referer");
- if((referer!=null) && (referer.trim().startsWith(Constants.HTTP + Constants.domainName))){
- return invocation.invoke();
- }
- log.info("referer : " + referer);
- return "error";
- }
CSRF攻擊原理比較簡單,其中Web A爲存在CSRF漏洞的網站,Web B爲攻擊者構建的惡意網站,User C爲Web A網站的合法用戶。web
![](http://static.javashuo.com/static/loading.gif)
1. 用戶C打開瀏覽器,訪問受信任網站A,輸入用戶名和密碼請求登陸網站A;apache
2.在用戶信息經過驗證後,網站A產生Cookie信息並返回給瀏覽器,此時用戶登陸網站A成功,能夠正常發送請求到網站A;瀏覽器
3. 用戶未退出網站A以前,在同一瀏覽器中,打開一個TAB頁訪問網站B;併發
4. 網站B接收到用戶請求後,返回一些攻擊性代碼,併發出一個請求要求訪問第三方站點A;dom
5. 瀏覽器在接收到這些攻擊性代碼後,根據網站B的請求,在用戶不知情的狀況下攜帶Cookie信息,向網站A發出請求。網站A並不知道該請求實際上是由B發起的,因此會根據用戶C的Cookie信息以C的權限處理該請求,致使來自網站B的惡意代碼被執行ide