struts2 拦截器控制用户登录权限

1.拦截器类 UserPerssionInterceptor.java
  1. package com.test.interceptor;
  2. import com.opensymphony.xwork2.ActionInvocation;
  3. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  4. import com.opensymphony.xwork2.ActionContext;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.apache.struts2.ServletActionContext;
  7. import java.util.Map;
  8. import java.util.Enumeration;
  9. import javax.servlet.http.HttpServletRequest;
  10. public class UserPerssionInterceptor extends AbstractInterceptor {
  11.     
  12.     private String useURL;
  13.     public static final String MEMBER_KEY="MEMBER";   
  14.     public static final String URL_KEY="GOING_TO";   
  15.            
  16.     public UserPerssionInterceptor() {
  17.         super();
  18.     }
  19.     public void init() {
  20.         super.init();
  21.     }
  22.     public void destroy() {
  23.         super.destroy();
  24.     }
  25.     
  26.     public String intercept(ActionInvocation actionInvocation) throws Exception {
  27.         String name = actionInvocation.getInvocationContext().getName(); 
  28.         String userLoginActionName = "memberlogin";
  29.         if (name.equals(userLoginActionName)) {
  30.             return actionInvocation.invoke();
  31.         } else {
  32.             ActionContext ac = actionInvocation.getInvocationContext();    
  33.              Map session = ac.getSession();
  34.              setGoingToURL(session, actionInvocation);   
  35.             if (session.get(MEMBER_KEY) != null) {
  36.                         return actionInvocation.invoke();
  37.             } else {
  38.                          return userLoginActionName;
  39.             }
  40.         }       
  41.     }
  42.     
  43.       
  44.     private void setGoingToURL(Map session, ActionInvocation invocation){   
  45.         String url = "";   
  46.         String namespace = invocation.getProxy().getNamespace();   
  47.         if (StringUtils.isNotBlank(namespace) && !namespace.equals("/")){   
  48.             url = url + namespace;   
  49.         }   
  50.         String actionName = invocation.getProxy().getActionName();   
  51.         if (StringUtils.isNotBlank(actionName)){   
  52.             url = url + "/" + actionName + ".action";   
  53.         }   
  54.        
  55.         session.put(URL_KEY, url);   
  56.     }  
  57.     
  58.     public String getUseURL() {
  59.         return useURL;
  60.     }
  61.     public void setUseURL(String useURL) {
  62.         this.useURL = useURL;
  63.     }
  64. }

2.struts.xml代码片段

  1.      <interceptors> 
  2.           <interceptor name ="userPerssion" class ="com.test.interceptor.UserPerssionInterceptor" />
  3.           <interceptor-stack name="shopdefault">
  4.               <interceptor-ref name="defaultStack"/>   
  5.           </interceptor-stack>
  6.       </interceptors>
  7.       <global-results>
  8.          <result name="memberlogin" type="tiles">page.member.login</result>              
  9.       </global-results>
在需要拦截的方法中加入 <interceptor-ref   name  = "shopdefault"   />   
  1.      <action name ="payProduct" method="payProduct" class ="orderAction">
  2.           <interceptor-ref name ="shopdefault" />  
  3.           <result type="tiles">page.order.payproduct</result>
  4.       </action>

 登录xml

  1.      <action name ="login!*" method="{1}" class ="loginAction"> 
  2.           <result name="input" type="tiles">page.product.entry</result>
  3.           <result type ="chain">loginAuthor</result>
  4.           <result type="redirect">${goingToURL}</result> 
  5.           <result name="error" type="tiles">page.product.entry</result>
  6.       </action>  

3.Login.java登录类

  1. package com.test.action;
  2. import java.util.Map;
  3. import javax.servlet.http.HttpSession;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. import org.apache.struts2.ServletActionContext;
  8. import org.apache.struts2.interceptor.SessionAware;
  9. import com.test.service.MemberService;
  10. import com.test.interceptor.UserPerssionInterceptor;
  11. import com.opensymphony.xwork2.ActionSupport;
  12. public class LoginAction extends ActionSupport implements SessionAware
  13. {
  14.     private MemberService memberService ;
  15.     private String memberId ;
  16.     private String password ;
  17.     public Map session;
  18.     private String goingToURL;   
  19.     public String execute(){
  20.         String url = null;
  21.         boolean flag  = this.memberService.isValidLogon(memberId, password);    
  22.         HttpSession se = ServletActionContext.getRequest().getSession();
  23.         String goingToURL = (String) session.get(UserPerssionInterceptor.URL_KEY);   
  24.         if (StringUtils.isNotBlank(goingToURL)){  
  25.             setGoingToURL(goingToURL);   
  26.             session.remove(UserPerssionInterceptor.URL_KEY);   
  27.         }else{   
  28.             setGoingToURL("index.jhtml");   
  29.         }
  30.         if(flag == true){   
  31.             session.put("MEMBER",memberId);
  32.             return SUCCESS ;    
  33.         }
  34.         else{
  35.             this.memberId = memberId;
  36.             return ERROR;
  37.         }       
  38.     }
  39.     
  40.     public MemberService getMemberService() {
  41.         return memberService;
  42.     }
  43.     public void setMemberService(MemberService memberService) {
  44.         this.memberService = memberService;
  45.     }
  46.     public String getMemberId() {
  47.         return memberId;
  48.     }
  49.     public void setMemberId(String memberId) {
  50.         this.memberId = memberId;
  51.     }
  52.     public String getPassword() {
  53.         return password;
  54.     }
  55.     
  56.     public void setPassword(String password) {
  57.         this.password = password;
  58.     }
  59.     
  60.     public void setSession(Map session) {
  61.         this.session = session;
  62.     }
  63.     
  64.     public Map getSession() {
  65.         return session;
  66.     }
  67.     public String getGoingToURL() {
  68.         return goingToURL;
  69.     }
  70.     public void setGoingToURL(String goingToURL) {
  71.         this.goingToURL = goingToURL;
  72.     }
  73.     
  74. }

登录成功以后根据之前保存好的url 跳转。

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值