package com.fusionability.web.utils; import java.util.HashMap; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CookieTool { public static int maxAge = 7* 24 * 60 * 60; //保存7天 public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) { Cookie cookie = new Cookie(name, value); cookie.setPath("/"); if (maxAge > 0) cookie.setMaxAge(maxAge); response.addCookie(cookie); } public static Cookie getCookieByName(HttpServletRequest request, String name) { Map<String, Cookie> cookieMap = ReadCookieMap(request); if (cookieMap.containsKey(name)) { Cookie cookie = (Cookie) cookieMap.get(name); return cookie; } else { return null; } } private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) { Map<String, Cookie> cookieMap = new HashMap<String, Cookie>(); Cookie[] cookies = request.getCookies(); if (null != cookies) { for (Cookie cookie : cookies) { cookieMap.put(cookie.getName(), cookie); } } return cookieMap; } }
isChecked = getRequest().getParameterValues("isChecked"); if (isChecked != null && isChecked.length > 0 ) { CookieTool.addCookie(getResponse(), "username", user.getUsername(), CookieTool.maxAge); CookieTool.addCookie(getResponse(), "userpswd", user.getUserpswd(), CookieTool.maxAge); }
public String updateUserInfo(){ user = userService.updateUser(user); if(user != null){ getSession().setAttribute(Constants.SESSION_USER, user); CookieTool.addCookie(ServletActionContext.getResponse(), "username", user.getUsername(), CookieTool.maxAge); // 清除Cookie CookieTool.addCookie(ServletActionContext.getResponse(), "userpswd", user.getUserpswd(), CookieTool.maxAge); // 清除Cookie return SUCCESS; } return ERROR; }
package com.fusionability.web.interceptor; import javax.servlet.ServletContext; import javax.servlet.ServletRequest; import javax.servlet.http.Cookie; import org.apache.struts2.ServletActionContext; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.fusionability.user.service.UserService; import com.fusionability.web.bean.User; import com.fusionability.web.utils.Constants; import com.fusionability.web.utils.CookieTool; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; /** * 认证拦截器(可以拦截URL的跳转) * @author liuzhengyi */ public class AuthInterceptor extends AbstractInterceptor { private static final long serialVersionUID = 8514282210475728866L; @Override public String intercept(ActionInvocation invocation) throws Exception { if("/FusionAbility/user/register.action".equals(ServletActionContext.getRequest().getRequestURI()) //||"/FusionAbility/user/login.action".equals(ServletActionContext.getRequest().getRequestURI()) ||"/FusionAbility/user/doRegister.action".equals(ServletActionContext.getRequest().getRequestURI()) ||"/FusionAbility/user/doLogin.action".equals(ServletActionContext.getRequest().getRequestURI()) ||"/FusionAbility/user/isExistUser.action".equals(ServletActionContext.getRequest().getRequestURI()) ||"/FusionAbility/user/otherUserAction.action".equals(ServletActionContext.getRequest().getRequestURI()) ||"/FusionAbility/user/goUpdateRetrievepassword.action".equals(ServletActionContext.getRequest().getRequestURI())){ return invocation.invoke(); } User user = (User)ServletActionContext.getRequest().getSession().getAttribute(Constants.SESSION_USER); if (user == null) { //重定向到登录页面 Cookie cokLoginName = CookieTool.getCookieByName(ServletActionContext.getRequest(), "username"); Cookie cokLoginPwd = CookieTool.getCookieByName(ServletActionContext.getRequest(), "userpswd"); if (cokLoginName != null && cokLoginPwd != null && cokLoginName.getValue() != null && cokLoginPwd.getValue() != null) { String loginName = cokLoginName.getValue(); String loginPwd = cokLoginPwd.getValue(); User user2 = new User(); user2.setUsername(loginName); user2.setUserpswd(loginPwd); ServletContext sc = ServletActionContext.getServletContext(); ApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(sc); UserService userService = (UserService) ac.getBean("userService"); User user3 = userService.getUser(user2); if (user3 == null) { CookieTool.addCookie(ServletActionContext.getResponse(), "username", null, 0); // 清除Cookie CookieTool.addCookie(ServletActionContext.getResponse(), "userpswd", null, 0); // 清除Cookie return Action.LOGIN; }else{ ServletActionContext.getRequest().getSession().setAttribute(Constants.SESSION_USER,user3); return invocation.invoke(); } }else{ return Action.LOGIN; } } return invocation.invoke(); } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!