三个步骤
一、保存并发送TOKEN
具体方法是:
<html>
<head>
<base href="<%=basePath%>">
<title>XXXXXXXX</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<script type="text/javascript">
location.href="loginuser.do?method=intoLogin";
</script>
</head>
<body>
</body>
</html>
/**
* 产生TOKEN
*/
public ActionForward intoLogin(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response){
saveToken(request);
return mapping.findForward("login");//跳转到登陆页面,并且将TOKEN发过去了
}
二、在登陆页面增加隐藏域
<form>
在这里添加<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value='<%=session.getAttribute("org.apache.struts.action.TOKEN")%>'/>
</form>
三、验证
public ActionForward loginUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
String token = request
.getParameter("org.apache.struts.taglib.html.TOKEN");
String token2 = (String) session
.getAttribute(org.apache.struts.Globals.TRANSACTION_TOKEN_KEY);
session.removeAttribute(org.apache.struts.Globals.TRANSACTION_TOKEN_KEY);
if (token != null && !("").equals(token) && token.equals(token2)){
继续执行
}else {
不用继续了
}