我也碰到类似问题,还是给段代码吧
Java类
public class TokenUtils {
private static String cons = "@_____@token@________@"; public static String setToken(javax.servlet.http.HttpServletRequest request) {
javax.servlet.http.HttpSession session = request.getSession();
String token = String.valueOf(java.util.UUID.randomUUID()) + String.valueOf(System.currentTimeMillis());
session.setAttribute("@_____@token@________@", token);
String html="<input type=\"hidden\" name=\"@_____@token@________@\" value='"+token+"'/>";
return html;
}
public static boolean isToken(javax.servlet.http.HttpServletRequest request) {
javax.servlet.http.HttpSession session = request.getSession();
String token = (String)session.getAttribute(cons);
if (token == null) token = "";
String tv = request.getParameter(cons);
session.removeAttribute(cons);
return token.equals(tv);
}
}
jsp页面
form中
<%=TokenUtils.setToken(request) %>
提交处理的Action中
每次提交时判断即可
if (TokenUtils.isToken(request)){
//业务逻辑处理
}