功能:java
·UserLogin做爲控制登陸的Action,校驗密碼成功後記錄session,能夠選擇記住登錄狀態,登錄成功後自動跳轉到登錄前的URL;web
·UserLogout做爲控制登陸推出的Action,移除session,刪除cookie;cookie
·MainInfo和HeadInfo模擬了兩個相對獨立的Action用於展現頁面內容;session
·LoginInterceptor做爲檢查登陸狀態的攔截器,先檢查session,後檢查本地cookie;jsp
·mainInfo.action和headInfo.action被配置經過LoginInterceptor攔截器檢查。ide
struts.xml配置文件url
<struts> <package name="common-web" extends="struts-default"> <interceptors> <interceptor name="loginInterceptor" class="loginInterceptor" /> <interceptor-stack name="loginDefaultStack"> <interceptor-ref name="loginInterceptor" /> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <default-interceptor-ref name="loginDefaultStack" /> <global-results> <result name="login" type="redirect">userLogin.action</result> </global-results> <action name="userLogin" class="userLoginAction"> <result type="redirect">${goingToURL}</result> <result name="input">/page/user_login.jsp</result> <interceptor-ref name="defaultStack" /> </action> <action name="userLogout" class="userLogoutAction"></action> <action name="mainInfo" class="mainInfoAction"> <result name="success">/page/main.jsp</result> </action> <action name="headInfo" class="headInfoAction"> <result name="success">/page/head.jsp</result> </action> </package> </struts>
struts.xml遇到的問題:spa
一、攔截器與Action必須配置在一個package下,不然攔截器不會對其餘package下的Action生效。.net
二、暫無。 debug
UserLogin.java主要源碼
public class UserLogin extends ActionSupport implements ServletResponseAware, SessionAware { private String name; private String password; private boolean rememberMe; private HttpServletResponse response; private Map<String, Object> session; private String goingToURL;//登陸前的URL public String execute() throws Exception { //... if (isLoginSucc) { //成功登陸後記錄session和cookie if (rememberMe) { String t = name + "," + password; Cookie cookie = new Cookie(CommonConstants.COOKIE_KEY_REMEMBER_LOGIN, t); cookie.setMaxAge(CommonConstants.COOKIE_AGE);//設置cookie存活時間 response.addCookie(cookie); } //設置session中的登陸用戶信息 session.put(CommonConstants.SESSION_KEY_USER_NAME, name); //從session中獲取登錄前URL,獲取後移除session中的這個值 String goingToURL = (String) session.get(CommonConstants.SESSION_KEY_URL_BEFORE_LOGIN); setGoingToURL(goingToURL); session.remove(CommonConstants.SESSION_KEY_URL_BEFORE_LOGIN); logger.info("登陸成功[" + name + "]"); return SUCCESS; } else { logger.error("登陸失敗[" + name + "][" + password + "]"); return INPUT; } } //... getter & setter methods }
UserLogin.java遇到的問題:
一、cookie.setDomain(),cookie.setPath()設置錯誤會致使cookie寫入失敗;
二、cookie.Value中有分號「;」時,會致使cookie寫入失敗,改成逗號解決;
LoginInterceptor.java主要源碼
public class LoginInterceptor extends AbstractInterceptor { /* (non-Javadoc) * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation) */ @Override public String intercept(ActionInvocation invocation) throws Exception { ActionContext actionContext = invocation.getInvocationContext(); HttpServletRequest request = (HttpServletRequest) actionContext .get(StrutsStatics.HTTP_REQUEST); Map<String, Object> session = actionContext.getSession(); //首先判斷session,查找是否登陸成功,經過攔截器 if (session != null && session.get(CommonConstants.SESSION_KEY_USER_NAME) != null) { logger.info("經過攔截器,session中有記錄[" + session.get(CommonConstants.SESSION_KEY_USER_NAME) + "]"); return invocation.invoke(); } //其次cookie驗證,是否有記住的登陸狀態 Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (logger.isDebugEnabled()) logger.debug("讀取cookie項[" + cookie.getName() + "]"); if (CommonConstants.COOKIE_KEY_REMEMBER_LOGIN.equals(cookie.getName())) { String value = cookie.getValue(); if (StringUtils.isNotBlank(value)) { String[] split = value.split(","); String name = split[0]; String password = split[1]; if (userLoginManager.checkLogin(name, password)) { //check name/password from cookie success logger.info("經過攔截器,cookie中有記錄[" + name + "]"); session.put(CommonConstants.SESSION_KEY_USER_NAME, name); return invocation.invoke(); } else { //check name/password from cookie failure setGoingToURL(session, invocation); return Action.LOGIN; } } else { setGoingToURL(session, invocation); return Action.LOGIN; } } } } setGoingToURL(session, invocation); return Action.LOGIN; } private void setGoingToURL(Map<String, Object> session, ActionInvocation invocation) { String url = ""; String namespace = invocation.getProxy().getNamespace(); if (StringUtils.isNotBlank(namespace) && !namespace.equals("/")) { url = url + namespace; } String actionName = invocation.getProxy().getActionName(); if (StringUtils.isNotBlank(actionName)) { url = url + "/" + actionName + ".action"; } if (logger.isDebugEnabled()) logger.debug("拼接登陸前URL,結果:" + CommonConstants.SESSION_KEY_URL_BEFORE_LOGIN + "[" + url + "]"); session.put(CommonConstants.SESSION_KEY_URL_BEFORE_LOGIN, url); } //... getter & setter methods }
LoginInterceptor.java遇到的問題:
轉載:http://blog.csdn.net/welken/article/details/5587068