[转]struct2 拦截所有没有登录的用户,强行转到登录界面AuthorizationInterceptor
package com.sise.action; import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class AuthorizationInterceptor extends AbstractInterceptor { private String ignoreActions; // ignoreActions属性的getter方法 public String getIgnoreActios() { return ignoreActions; } // ignoreActions属性的setter方法 public void setIgnoreActions(String ignoreActions) { this .ignoreActions = ignoreActions; } @Override public String intercept(ActionInvocation invocation) throws Exception { ActionContext ctx = invocation.getInvocationContext(); Map session = ctx.getSession(); Object user = session.get( "currentUserId" ); boolean ignore = false ; if (user != null || ignore == true ) { return invocation.invoke(); } else { return Action.LOGIN; } } } |
struct.xml配置文件
<?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" > <struts> < package name= "default" extends = "struts-default" > <interceptors> <interceptor name= "loginInterceptor" class = "com.sise.action.AuthorizationInterceptor" ></interceptor> <interceptor-stack name= "loginDefaultStack" > <interceptor-ref name= "loginInterceptor" > <param name= "ignoreActions" > validate_code,register.*,.*login.*,upload,connector</param> </interceptor-ref> <interceptor-ref name= "defaultStack" ></interceptor-ref> </interceptor-stack> </interceptors> < default -interceptor-ref name= "loginDefaultStack" ></ default -interceptor-ref> <global-results> <result name= "login" type= "redirect" >/login.html</result> </global-results> <action name= "index" class = "com.sise.action.IndexAction" method= "index" > <result name= "success" >/index.html</result> </action> <action name= "login" class = "com.sise.action.LoginAction" method= "login" > <result type= "redirectAction" >index</result> <result name= "login" >/login.html</result> <interceptor-ref name= "defaultStack" ></interceptor-ref> </action> </ package > </struts> |