当前位置: 代码迷 >> 综合 >> struts2中使用拦截器(Interceptor)控制登录和权限
  详细解决方案

struts2中使用拦截器(Interceptor)控制登录和权限

热度:60   发布时间:2023-12-08 16:33:13.0
struts2中使用拦截器(Interceptor)控制登录和权限
转载:http://www.itpub.net/thread-966462-1-1.html

在jsp servlet中我们通常使用Servlet Filter控制用户是否登入, 是否有权限转到某个页面。在struts2中我们应该会想到他的拦截器(Interceptor), Interceptor在struts2中起着非常重要的作用。 很多struts2中的功能都是使用Interceptor实现的。

需求:简单的登入界面,让用户输入用户名、密码、记住密码(remember me)。 如果用户选中remember me的话, 下次就不需要再登入了(使用cookie实现, 用需要点击logout取消remeber me功能)。 如果用户起始输入的地址不是登入页面的话,在用户登入之后需要转到用户输入的起始地址。

我们先看看LoginInterceptor.java
Java代码
Java代码  
  1. package com.javaeye.dengyin2000.wallet.interceptor;     
  2.     
  3. import java.util.Map;     
  4.     
  5. import javax.servlet.http.Cookie;     
  6. import javax.servlet.http.HttpServletRequest;     
  7.     
  8. import org.apache.commons.lang.StringUtils;     
  9. import org.apache.struts2.StrutsStatics;     
  10.     
  11. import com.javaeye.dengyin2000.wallet.dao.UserDAO;     
  12. import com.javaeye.dengyin2000.wallet.dao.UserNotFoundException;     
  13. import com.javaeye.dengyin2000.wallet.domains.User;     
  14. import com.opensymphony.xwork2.ActionContext;     
  15. import com.opensymphony.xwork2.ActionInvocation;     
  16. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;     
  17.     
  18. public class LoginInterceptor extends AbstractInterceptor {     
  19.     public static final String USER_SESSION_KEY="wallet.session.user";     
  20.     public static final String COOKIE_REMEMBERME_KEY="wallet.cookie.rememberme";     
  21.     public static final String GOING_TO_URL_KEY="GOING_TO";     
  22.          
  23.     private UserDAO userDao;     
  24.     
  25.     @Override    
  26.     public String intercept(ActionInvocation invocation) throws Exception {     
  27.              
  28.         ActionContext actionContext = invocation.getInvocationContext();     
  29.         HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);     
  30.              
  31.         Map session = actionContext.getSession();     
  32.         if (session != null && session.get(USER_SESSION_KEY) != null){     
  33.             return invocation.invoke();     
  34.         }     
  35.              
  36.         Cookie[] cookies = request.getCookies();     
  37.         if (cookies!=null) {     
  38.             for (Cookie cookie : cookies) {     
  39.                 if (COOKIE_REMEMBERME_KEY.equals(cookie.getName())) {     
  40.                     String value = cookie.getValue();     
  41.                     if (StringUtils.isNotBlank(value)) {     
  42.                         String[] split = value.split("==";     
  43.                         String userName = split[0];     
  44.                         String password = split[1];     
  45.                         try {     
  46.                             User user = userDao     
  47.                                     .attemptLogin(userName, password);     
  48.                             session.put(USER_SESSION_KEY, user);     
  49.                         } catch (UserNotFoundException e) {     
  50.                             setGoingToURL(session, invocation);     
  51.                             return "login";     
  52.                         }     
  53.                     } else {     
  54.                         setGoingToURL(session, invocation);     
  55.                         return "login";     
  56.                     }     
  57.                     return invocation.invoke();     
  58.                 }     
  59.             }     
  60.         }     
  61.         setGoingToURL(session, invocation);     
  62.         return "login";     
  63.     }     
  64.     
  65.     private void setGoingToURL(Map session, ActionInvocation invocation){     
  66.         String url = "";     
  67.         String namespace = invocation.getProxy().getNamespace();     
  68.         if (StringUtils.isNotBlank(namespace) && !namespace.equals("/"){     
  69.             url = url + namespace;     
  70.         }     
  71.         String actionName = invocation.getProxy().getActionName();     
  72.         if (StringUtils.isNotBlank(actionName)){     
  73.             url = url + "/" + actionName + ".action";     
  74.         }     
  75.         session.put(GOING_TO_URL_KEY, url);     
  76.     }     
  77.          
  78.     public UserDAO getUserDao() {     
  79.         return userDao;     
  80.     }     
  81.     
  82.     public void setUserDao(UserDAO userDao) {     
  83.         this.userDao = userDao;     
  84.     }     
  85.     
  86. }    
  87.   
  88. package com.javaeye.dengyin2000.wallet.interceptor;  
  89.   
  90. import java.util.Map;  
  91.   
  92. import javax.servlet.http.Cookie;  
  93. import javax.servlet.http.HttpServletRequest;  
  94.   
  95. import org.apache.commons.lang.StringUtils;  
  96. import org.apache.struts2.StrutsStatics;  
  97.   
  98. import com.javaeye.dengyin2000.wallet.dao.UserDAO;  
  99. import com.javaeye.dengyin2000.wallet.dao.UserNotFoundException;  
  100. import com.javaeye.dengyin2000.wallet.domains.User;  
  101. import com.opensymphony.xwork2.ActionContext;  
  102. import com.opensymphony.xwork2.ActionInvocation;  
  103. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;  
  104.   
  105. public class LoginInterceptor extends AbstractInterceptor {  
  106.         public static final String USER_SESSION_KEY="wallet.session.user";  
  107.         public static final String COOKIE_REMEMBERME_KEY="wallet.cookie.rememberme";  
  108.         public static final String GOING_TO_URL_KEY="GOING_TO";  
  109.           
  110.         private UserDAO userDao;  
  111.   
  112.         @Override  
  113.         public String intercept(ActionInvocation invocation) throws Exception {  
  114.                   
  115.                 ActionContext actionContext = invocation.getInvocationContext();  
  116.                 HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);  
  117.                   
  118.                 Map session = actionContext.getSession();  
  119.                 if (session != null && session.get(USER_SESSION_KEY) != null){  
  120.                         return invocation.invoke();  
  121.                 }  
  122.                   
  123.                 Cookie[] cookies = request.getCookies();  
  124.                 if (cookies!=null) {  
  125.                         for (Cookie cookie : cookies) {  
  126.                                 if (COOKIE_REMEMBERME_KEY.equals(cookie.getName())) {  
  127.                                         String value = cookie.getValue();  
  128.                                         if (StringUtils.isNotBlank(value)) {  
  129.                                                 String[] split = value.split("==";  
  130.                                                 String userName = split[0];  
  131.                                                 String password = split[1];  
  132.                                                 try {  
  133.                                                         User user = userDao  
  134.                                                                         .attemptLogin(userName, password);  
  135.                                                         session.put(USER_SESSION_KEY, user);  
  136.                                                 } catch (UserNotFoundException e) {  
  137.                                                         setGoingToURL(session, invocation);  
  138.                                                         return "login";  
  139.                                                 }  
  140.                                         } else {  
  141.                                                 setGoingToURL(session, invocation);  
  142.                                                 return "login";  
  143.                                         }  
  144.                                         return invocation.invoke();  
  145.                                 }  
  146.                         }  
  147.                 }  
  148.                 setGoingToURL(session, invocation);  
  149.                 return "login";  
  150.         }  
  151.   
  152.         private void setGoingToURL(Map session, ActionInvocation invocation){  
  153.                 String url = "";  
  154.                 String namespace = invocation.getProxy().getNamespace();  
  155.                 if (StringUtils.isNotBlank(namespace) && !namespace.equals("/"){  
  156.                         url = url + namespace;  
  157.                 }  
  158.                 String actionName = invocation.getProxy().getActionName();  
  159.                 if (StringUtils.isNotBlank(actionName)){  
  160.                         url = url + "/" + actionName + ".action";  
  161.                 }  
  162.                 session.put(GOING_TO_URL_KEY, url);  
  163.         }  
  164.           
  165.         public UserDAO getUserDao() {  
  166.                 return userDao;  
  167.         }  
  168.   
  169.         public void setUserDao(UserDAO userDao) {  
  170.                 this.userDao = userDao;  
  171.         }  
  172.   
  173. }  

首先判断session中有没有用户信息, 如果有的话继续, 如果没有的话,检查cookie中有没有rememberme的值,如果有的话,用==分割, 取得用户名密码进行登入。如果没有这个用户的话,记录下request的action地址然后转到登入页面。如果验证有这个用户,则继续下面的interceptor。 如果cookie中没有信息的话,则记录request的action地址然后转到登入页面。 以上就是LoginInterceptor的全部代码。

下面我们看看struts.xml

Java代码
Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>     
  2.     
  3. <!DOCTYPE struts PUBLIC      
  4.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
  5.     "http://struts.apache.org/dtds/struts-2.0.dtd">     
  6.     
  7. <struts>     
  8.     <package name="default" extends="struts-default">     
  9.         <interceptors>     
  10.             <interceptor name="loginInterceptor" class="loginInterceptor"></interceptor>     
  11.             <interceptor-stack name="loginDefaultStack">     
  12.                 <interceptor-ref name="loginInterceptor"></interceptor-ref>     
  13.                 <interceptor-ref name="defaultStack"></interceptor-ref>     
  14.             </interceptor-stack>     
  15.         </interceptors>     
  16.         <default-interceptor-ref name="loginDefaultStack"></default-interceptor-ref>     
  17.         <global-results>     
  18.             <result name="login" type="redirect">/login.jsp</result>     
  19.         </global-results>          
  20.         <action name="index" class="indexAction">     
  21.             <result>/index.jsp</result>     
  22.         </action>     
  23.         <action name="logout" class="logoutAction"></action>     
  24.              
  25.         <action name="login" class="loginAction" method="login">     
  26.             <result type="redirect">${goingToURL}</result>     
  27.             <result name="input">/login.jsp</result>     
  28.             <interceptor-ref name="defaultStack"></interceptor-ref>     
  29.         </action>     
  30.              
  31.         <action name="register" class="registerAction">     
  32.             <result type="redirect">/login.jsp</result>     
  33.             <result name="input">/register.jsp</result>     
  34.             <interceptor-ref name="defaultStack"></interceptor-ref>     
  35.         </action>     
  36.     </package>     
  37. </struts>    
  38.   
  39. <?xml version="1.0" encoding="UTF-8"?>  
  40.   
  41. <!DOCTYPE struts PUBLIC   
  42.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  43.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  44.   
  45. <struts>  
  46.         <package name="default" extends="struts-default">  
  47.                 <interceptors>  
  48.                         <interceptor name="loginInterceptor" class="loginInterceptor"></interceptor>  
  49.                         <interceptor-stack name="loginDefaultStack">  
  50.                                 <interceptor-ref name="loginInterceptor"></interceptor-ref>  
  51.                                 <interceptor-ref name="defaultStack"></interceptor-ref>  
  52.                         </interceptor-stack>  
  53.                 </interceptors>  
  54.                 <default-interceptor-ref name="loginDefaultStack"></default-interceptor-ref>  
  55.                 <global-results>  
  56.                         <result name="login" type="redirect">/login.jsp</result>  
  57.                 </global-results>                  
  58.                 <action name="index" class="indexAction">  
  59.                         <result>/index.jsp</result>  
  60.                 </action>  
  61.                 <action name="logout" class="logoutAction"></action>  
  62.                   
  63.                 <action name="login" class="loginAction" method="login">  
  64.                         <result type="redirect">${goingToURL}</result>  
  65.                         <result name="input">/login.jsp</result>  
  66.                         <interceptor-ref name="defaultStack"></interceptor-ref>  
  67.                 </action>  
  68.                   
  69.                 <action name="register" class="registerAction">  
  70.                         <result type="redirect">/login.jsp</result>  
  71.                         <result name="input">/register.jsp</result>  
  72.                         <interceptor-ref name="defaultStack"></interceptor-ref>  
  73.                 </action>  
  74.         </package>  
  75. </struts>  

我们是使用的默认的interceptor stack是loginInterceptor, 如果你需要让不登入的用户也能访问的话,你需要配置你的action使用defaultStack。 我们这里的login, register使用的就是defaultStack。 这里要注意的是success的result是我们用LoginInterceptor设过来的值。 这样我们就能够转到用户输入的起始页面。 下面我们再来看看login.jsp 和 loginAction

Java代码
Jsp代码  
  1. <%@taglib prefix="s" uri="/struts-tags" %>     
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"    
  3.     pageEncoding="UTF-8"%>     
  4. <html>     
  5. <head>     
  6.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">     
  7.     <title>Wallet-Login</title>     
  8. </head>     
  9. <body>     
  10. <h2>Login</h2>      
  11. <s:actionmessage/>     
  12. <s:actionerror/>     
  13. <s:form action="login" method="post" validate="false" theme="xhtml">     
  14. <s:textfield name="loginName" label="Username"></s:textfield><br/>     
  15. <sassword name="password" label="Password"></sassword><br/>     
  16. <s:checkbox label="Remember Me" name="rememberMe"></s:checkbox>     
  17. <s:submit value="%{'Login'}"></s:submit>      
  18. </s:form>     
  19. <a href="register.jsp">Register</a>     
  20. </body>     
  21. </html>    
  22.   
  23. <%@taglib prefix="s" uri="/struts-tags" %>  
  24. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  25.     pageEncoding="UTF-8"%>  
  26. <html>  
  27. <head>  
  28.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  29.         <title>Wallet-Login</title>  
  30. </head>  
  31. <body>  
  32. <h2>Login</h2>   
  33. <s:actionmessage/>  
  34. <s:actionerror/>  
  35. <s:form action="login" method="post" validate="false" theme="xhtml">  
  36. <s:textfield name="loginName" label="Username"></s:textfield><br/>  
  37. <sassword name="password" label="Password"></sassword><br/>  
  38. <s:checkbox label="Remember Me" name="rememberMe"></s:checkbox>  
  39. <s:submit value="%{'Login'}"></s:submit>   
  40. </s:form>  
  41. <a href="register.jsp">Register</a>  
  42. </body>  
  43. </html>  

Java代码
Java代码  
  1. package com.javaeye.dengyin2000.wallet.actions;     
  2.     
  3. import java.util.Map;     
  4.     
  5. import javax.servlet.http.Cookie;     
  6. import javax.servlet.http.HttpServletRequest;     
  7. import javax.servlet.http.HttpServletResponse;     
  8.     
  9. import org.apache.commons.lang.StringUtils;     
  10. import org.apache.struts2.interceptor.CookiesAware;     
  11. import org.apache.struts2.interceptor.ServletRequestAware;     
  12. import org.apache.struts2.interceptor.ServletResponseAware;     
  13. import org.apache.struts2.interceptor.SessionAware;     
  14.     
  15. import com.javaeye.dengyin2000.wallet.dao.UserDAO;     
  16. import com.javaeye.dengyin2000.wallet.dao.UserNotFoundException;     
  17. import com.javaeye.dengyin2000.wallet.domains.User;     
  18. import com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor;     
  19. import com.opensymphony.xwork2.ActionSupport;     
  20.     
  21. public class LoginAction extends ActionSupport implements ServletResponseAware, ServletRequestAware, SessionAware, CookiesAware{     
  22.     
  23.     private UserDAO userDao;     
  24.     private String loginName;     
  25.     private String password;     
  26.     private boolean rememberMe;     
  27.     private HttpServletResponse response;     
  28.     private HttpServletRequest request;     
  29.     private Map session;     
  30.     private Map cookies;     
  31.     private String goingToURL;     
  32.     public String getGoingToURL() {     
  33.         return goingToURL;     
  34.     }     
  35.     public void setGoingToURL(String goingToURL) {     
  36.         this.goingToURL = goingToURL;     
  37.     }     
  38.     public boolean isRememberMe() {     
  39.         return rememberMe;     
  40.     }     
  41.     public void setRememberMe(boolean rememberMe) {     
  42.         this.rememberMe = rememberMe;     
  43.     }     
  44.     public String getLoginName() {     
  45.         return loginName;     
  46.     }     
  47.     public void setLoginName(String loginName) {     
  48.         this.loginName = loginName;     
  49.     }     
  50.     public String getPassword() {     
  51.         return password;     
  52.     }     
  53.     public void setPassword(String password) {     
  54.         this.password = password;     
  55.     }     
  56.          
  57.          
  58.     public String login()throws Exception{     
  59.         try {     
  60.             User user = userDao.attemptLogin(loginName, password);     
  61.             if (rememberMe){     
  62.                 Cookie cookie = new Cookie(LoginInterceptor.COOKIE_REMEMBERME_KEY, user.getLoginName() + "==" + user.getPassword());     
  63.                 cookie.setMaxAge(60 * 60 * 24 * 14);     
  64.                 response.addCookie(cookie);     
  65.             }     
  66.             session.put(LoginInterceptor.USER_SESSION_KEY, user);     
  67.             String goingToURL = (String) session.get(LoginInterceptor.GOING_TO_URL_KEY);     
  68.             if (StringUtils.isNotBlank(goingToURL)){     
  69.                 setGoingToURL(goingToURL);     
  70.                 session.remove(LoginInterceptor.GOING_TO_URL_KEY);     
  71.             }else{     
  72.                 setGoingToURL("index.action";     
  73.             }     
  74.             return SUCCESS;     
  75.         } catch (UserNotFoundException e) {     
  76.             addActionMessage("user name or password is not corrected.";     
  77.             return INPUT;     
  78.         }     
  79.     }     
  80.     public UserDAO getUserDao() {     
  81.         return userDao;     
  82.     }     
  83.     public void setUserDao(UserDAO userDao) {     
  84.         this.userDao = userDao;     
  85.     }     
  86.     public void setServletResponse(HttpServletResponse response) {     
  87.         this.response = response;     
  88.     }     
  89.     public void setServletRequest(HttpServletRequest request) {     
  90.         this.request = request;     
  91.     }     
  92.     public void setSession(Map session) {     
  93.         this.session = session;     
  94.     }     
  95.     public void setCookiesMap(Map cookies) {     
  96.         this.cookies = cookies;     
  97.     }     
  98. }    
  99.   
  100. package com.javaeye.dengyin2000.wallet.actions;  
  101.   
  102. import java.util.Map;  
  103.   
  104. import javax.servlet.http.Cookie;  
  105. import javax.servlet.http.HttpServletRequest;  
  106. import javax.servlet.http.HttpServletResponse;  
  107.   
  108. import org.apache.commons.lang.StringUtils;  
  109. import org.apache.struts2.interceptor.CookiesAware;  
  110. import org.apache.struts2.interceptor.ServletRequestAware;  
  111. import org.apache.struts2.interceptor.ServletResponseAware;  
  112. import org.apache.struts2.interceptor.SessionAware;  
  113.   
  114. import com.javaeye.dengyin2000.wallet.dao.UserDAO;  
  115. import com.javaeye.dengyin2000.wallet.dao.UserNotFoundException;  
  116. import com.javaeye.dengyin2000.wallet.domains.User;  
  117. import com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor;  
  118. import com.opensymphony.xwork2.ActionSupport;  
  119.   
  120. public class LoginAction extends ActionSupport implements ServletResponseAware, ServletRequestAware, SessionAware, CookiesAware{  
  121.   
  122.         private UserDAO userDao;  
  123.         private String loginName;  
  124.         private String password;  
  125.         private boolean rememberMe;  
  126.         private HttpServletResponse response;  
  127.         private HttpServletRequest request;  
  128.         private Map session;  
  129.         private Map cookies;  
  130.         private String goingToURL;  
  131.         public String getGoingToURL() {  
  132.                 return goingToURL;  
  133.         }  
  134.         public void setGoingToURL(String goingToURL) {  
  135.                 this.goingToURL = goingToURL;  
  136.         }  
  137.         public boolean isRememberMe() {  
  138.                 return rememberMe;  
  139.         }  
  140.         public void setRememberMe(boolean rememberMe) {  
  141.                 this.rememberMe = rememberMe;  
  142.         }  
  143.         public String getLoginName() {  
  144.                 return loginName;  
  145.         }  
  146.         public void setLoginName(String loginName) {  
  147.                 this.loginName = loginName;  
  148.         }  
  149.         public String getPassword() {  
  150.                 return password;  
  151.         }  
  152.         public void setPassword(String password) {  
  153.                 this.password = password;  
  154.         }  
  155.           
  156.           
  157.         public String login()throws Exception{  
  158.                 try {  
  159.                         User user = userDao.attemptLogin(loginName, password);  
  160.                         if (rememberMe){  
  161.                                 Cookie cookie = new Cookie(LoginInterceptor.COOKIE_REMEMBERME_KEY, user.getLoginName() + "==" + user.getPassword());  
  162.                                 cookie.setMaxAge(60 * 60 * 24 * 14);  
  163.                                 response.addCookie(cookie);  
  164.                         }  
  165.                         session.put(LoginInterceptor.USER_SESSION_KEY, user);  
  166.                         String goingToURL = (String) session.get(LoginInterceptor.GOING_TO_URL_KEY);  
  167.                         if (StringUtils.isNotBlank(goingToURL)){  
  168.                                 setGoingToURL(goingToURL);  
  169.                                 session.remove(LoginInterceptor.GOING_TO_URL_KEY);  
  170.                         }else{  
  171.                                 setGoingToURL("index.action";  
  172.                         }  
  173.                         return SUCCESS;  
  174.                 } catch (UserNotFoundException e) {  
  175.                         addActionMessage("user name or password is not corrected.";  
  176.                         return INPUT;  
  177.                 }  
  178.         }  
  179.         public UserDAO getUserDao() {  
  180.                 return userDao;  
  181.         }  
  182.         public void setUserDao(UserDAO userDao) {  
  183.                 this.userDao = userDao;  
  184.         }  
  185.         public void setServletResponse(HttpServletResponse response) {  
  186.                 this.response = response;  
  187.         }  
  188.         public void setServletRequest(HttpServletRequest request) {  
  189.                 this.request = request;  
  190.         }  
  191.         public void setSession(Map session) {  
  192.                 this.session = session;  
  193.         }  
  194.         public void setCookiesMap(Map cookies) {  
  195.                 this.cookies = cookies;  
  196.         }  
  197. }  
  198.   
  199.   
  200. 差不多就是这么多代码了。 最后看看logoutAction   
  201.   
  202. Java代码   
  203. package com.javaeye.dengyin2000.wallet.actions;     
  204.     
  205. import javax.servlet.http.Cookie;     
  206. import javax.servlet.http.HttpServletRequest;     
  207. import javax.servlet.http.HttpServletResponse;     
  208. import javax.servlet.http.HttpSession;     
  209.     
  210. import org.apache.struts2.interceptor.ServletRequestAware;     
  211. import org.apache.struts2.interceptor.ServletResponseAware;     
  212.     
  213. import com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor;     
  214. import com.opensymphony.xwork2.ActionSupport;     
  215.     
  216. public class LogoutAction extends ActionSupport implements ServletRequestAware , ServletResponseAware{     
  217.     
  218.     private HttpServletRequest request;     
  219.     private HttpServletResponse response;     
  220.     
  221.     public String execute() throws Exception{     
  222.         HttpSession session = request.getSession(false);     
  223.         if (session!=null)     
  224.             session.removeAttribute(LoginInterceptor.USER_SESSION_KEY);     
  225.              
  226.         Cookie[] cookies = request.getCookies();     
  227.         if (cookies!=null) {     
  228.             for (Cookie cookie : cookies) {     
  229.                 if (LoginInterceptor.COOKIE_REMEMBERME_KEY.equals(cookie     
  230.                         .getName())) {     
  231.                     cookie.setValue("";     
  232.                     cookie.setMaxAge(0);     
  233.                     response.addCookie(cookie);     
  234.                     return "login";     
  235.                 }     
  236.             }     
  237.         }     
  238.         return "login";     
  239.     }     
  240.     
  241.     public void setServletRequest(HttpServletRequest request) {     
  242.         this.request = request;     
  243.     }     
  244.     
  245.     public void setServletResponse(HttpServletResponse response) {     
  246.         this.response = response;     
  247.     }     
  248.     
  249. }    
  250.   
  251. package com.javaeye.dengyin2000.wallet.actions;  
  252.   
  253. import javax.servlet.http.Cookie;  
  254. import javax.servlet.http.HttpServletRequest;  
  255. import javax.servlet.http.HttpServletResponse;  
  256. import javax.servlet.http.HttpSession;  
  257.   
  258. import org.apache.struts2.interceptor.ServletRequestAware;  
  259. import org.apache.struts2.interceptor.ServletResponseAware;  
  260.   
  261. import com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor;  
  262. import com.opensymphony.xwork2.ActionSupport;  
  263.   
  264. public class LogoutAction extends ActionSupport implements ServletRequestAware , ServletResponseAware{  
  265.   
  266.         private HttpServletRequest request;  
  267.         private HttpServletResponse response;  
  268.   
  269.         public String execute() throws Exception{  
  270.                 HttpSession session = request.getSession(false);  
  271.                 if (session!=null)  
  272.                         session.removeAttribute(LoginInterceptor.USER_SESSION_KEY);  
  273.                   
  274.                 Cookie[] cookies = request.getCookies();  
  275.                 if (cookies!=null) {  
  276.                         for (Cookie cookie : cookies) {  
  277.                                 if (LoginInterceptor.COOKIE_REMEMBERME_KEY.equals(cookie  
  278.                                                 .getName())) {  
  279.                                         cookie.setValue("";  
  280.                                         cookie.setMaxAge(0);  
  281.                                         response.addCookie(cookie);  
  282.                                         return "login";  
  283.                                 }  
  284.                         }  
  285.                 }  
  286.                 return "login";  
  287.         }  
  288.   
  289.         public void setServletRequest(HttpServletRequest request) {  
  290.                 this.request = request;  
  291.         }  
  292.   
  293.         public void setServletResponse(HttpServletResponse response) {  
  294.                 this.response = response;  
  295.         }  
  296.   
  297. }  
  298.   
  299. 这里需要注意的是需要把cookie也清理下。   
  300.   
  301. applicationContext-struts.xml   
  302. Java代码   
  303. <?xml version="1.0" encoding="UTF-8"?>     
  304. <!DOCTYPE beans PUBLIC      
  305.     "-//SPRING//DTD BEAN//EN"      
  306.     "http://www.springframework.org/dtd/spring-beans.dtd">     
  307.          
  308. <beans>     
  309.     <!-- Example of SAF2 action instantiated by Spring -->     
  310.     <!-- bean id="helloWorldAction" class="tutorial.HelloWorldAction" singleton="false" />     
  311.      -->     
  312.      <bean id="indexAction" class="com.javaeye.dengyin2000.wallet.actions.IndexAction" singleton="false"></bean>     
  313.      <bean id="loginAction" class="com.javaeye.dengyin2000.wallet.actions.LoginAction" singleton="false">     
  314.         <property name="userDao" ref="userDao" />     
  315.      </bean>     
  316.           
  317.      <bean id="logoutAction" class="com.javaeye.dengyin2000.wallet.actions.LogoutAction" singleton="false"></bean>     
  318.           
  319.      <bean id="registerAction" class="com.javaeye.dengyin2000.wallet.actions.RegisterAction" singleton="false"></bean>     
  320.           
  321.      <!-- the following is struts2 interceptors -->     
  322.      <bean id="loginInterceptor" class="com.javaeye.dengyin2000.wallet.interceptor.LoginInterceptor">     
  323.         <property name="userDao" ref="userDao" />     
  324.      </bean>     
  325.           
  326.      <bean id="userDao" class="com.javaeye.dengyin2000.wallet.dao.UserDAOImpl">     
  327.      </bean>     
  328. </beans> 
  相关解决方案