实现用户登录过滤
UserLoginInterceptor.java
1 package com.tcf.inter; 2 3 import java.util.List; 4 5 import com.opensymphony.xwork2.Action; 6 import com.opensymphony.xwork2.ActionContext; 7 import com.opensymphony.xwork2.ActionInvocation; 8 import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 9 import com.tcf.entity.User; 10 11 public class UserLoginInterceptor extends AbstractInterceptor { 12 private String excludeMethods; 13 14 public String getExcludeMethods() { 15 return excludeMethods; 16 } 17 18 public void setExcludeMethods(String excludeMethods) { 19 this.excludeMethods = excludeMethods; 20 } 21 22 @Override 23 public String intercept(ActionInvocation arg0) throws Exception { 24 User user = (User) ActionContext.getContext().getSession().get("userInfo"); 25 String name = ActionContext.getContext().getName(); 26 String[] actions = excludeMethods.split(","); 27 28 if(user == null && !contains(actions,name)){ 29 return Action.INPUT; 30 } 31 arg0.invoke(); 32 return Action.SUCCESS; 33 } 34 35 public boolean contains(String[] list,String str){ 36 for(String s:list){ 37 if(s.equals(str)){ 38 return true; 39 } 40 } 41 return false; 42 } 43 }
struts.xml
1 <interceptors> 2 <interceptor name="userInter" class="com.tcf.inter.UserLoginInterceptor"></interceptor> 3 <interceptor-stack name="allInter"> 4 <interceptor-ref name="userInter"> 5 <!--不进行拦截的action名称 --> 6 <param name="excludeMethods">loginUser,registerUser,upload,uploades,download</param> 7 </interceptor-ref> 8 <interceptor-ref name="defaultStack" /> 9 </interceptor-stack> 10 </interceptors> 11 <default-interceptor-ref name="allInter"></default-interceptor-ref>