Struts2 自定义拦截器实现权限验证

java代码
package com.zhy.framework.core.server.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;

/**
* 该拦截器是校验用户是否登录
* @author zhy
*
*/
public class ValidateUserIsLoginInterceptor extends BaseInterceptor {

private static final long serialVersionUID = -424883620258961724L;

/**
*  1.通过invocation获取getInvocationContext,返回的是{@1}ActionContext实例
*    然后通过ActionContext就可以获取对应的session
*  2.invocation.getAction()获取的是request请求action实例,因此可以通过该实例
*  是那个action类的,来判断是否需要权限验证
*   
*/
public String intercept(ActionInvocation invocation) throws Exception {
//因为用户的信息都保存在session中,因此首先获取服务器端对应的session
ActionContext actionContext = invocation.getInvocationContext();
Map session = actionContext.getSession();

//对于登陆请求的action不需要进行sesion的校验,因此获取action
Object action = invocation.getAction();
boolean flag = validateSessionAction(action);
if(!flag){
return invocation.invoke();
}
//需要校验是否登录
Object user = session.get(USER);
if(user != null){
return invocation.invoke();
}else{
return LOGIN;
}
}

}


/**
* {@1}ActionContext介绍
*  前言: struts2 帮我们完成了将请求参数直接配置到action的字段中
*      但是有时我们需要直接获取请求的参数或是会话中的数据,或是直接对httpRequest和httpResponse操作
*      因此ActionContext的出现是必要的
*   首先看看ActionContext如何获取httpRequest中的参数
*   ActionContext context = ActionContext.getContext();
*   (注意:上面的context 是通过 return (ActionContext)actionContext.get();返回的
*   actionContext是一个static ThreadLocal actionContext = new ThreadLocal();)
*   Map params = context.getParameters();//获取请求参数,返回的是一个map
*   String paramValue = (String)params.get("paramName");//返回参数的值
*   (ActionContext内部维护了一个Map<String, Object> context,所有的session,parameters都是放到这个map里面的
*    这个context是在请求时生成的,并放到静态的额ThreadLocal中进行维护,ThreadLocal可以保证线程安全
*   )
*   接下来获取session
*   Map session = ActionContext.getContext().getSession();//返回请求的session,这个session是map类型的。
*   (因为struts2的所有请求(request),session参数都是放到一个context的map中维护的,所以需要依靠actionContext返回对应的paramter和session
*   )
*   如果想要获取原生的httpRequest怎么办?
*   使用ServletActionContext,该context是继承至ActionContext的
*   获取原生的HttpServletRequest
*   HttpServletRequest request = ServletActionContext.getRequest();
*   获取原生的HttpSession
*   HttpSession httpSession = request.getSession();
*  
*   实际开发中的选择(使用ActionContext还是ServletActionContext?)
*   对于这个问题应尽量使用ActionContext,降低对servlet的依赖。可以根据实际需要进行选择,
*   如果方便使用IOC的方式(通过struts2完成属性的注入),则使用ActionContext。如果没有使用IOC,那么只能
*   通过获取原生的HttpServletRequest   
*
* */

package com.zhy.framework.core.server.interceptor;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
* 自定义拦截器的基类
* @author zhy
*
*/
public abstract class BaseInterceptor extends AbstractInterceptor {

public static String[] notNeedValidateActions = {"LoginAction"};
//用户登录的session Key
public static final String USER="user";

//返回结果
public static final String LOGIN = "login";//表示需要登录

/**
* 校验action是否需要session验证
* @return true 需要
*         false 不需要
*/
public boolean validateSessionAction(Object action){
if(action instanceof Object){//真正环境时需要把Object替换成具体的action类
return false;
}else{
return true;
}
}

}

struts.xml中需要配置的
<interceptors> 
       <interceptor name="permission" class="com.zhy.framework.core.server.interceptor.ValidateUserIsLoginInterceptor" /> //声明自定义拦截器
       <interceptor-stack name="permissionStack"> 
              <interceptor-ref name="permission" /> 
              <interceptor-ref name="defaultStack" /> 
       </interceptor-stack> //定义拦截器栈
</interceptors>
指定默认拦截器使用自定义的拦截器栈
<default-interceptor-ref name="permissinStack"/> 

你可能感兴趣的:(Struts2 自定义拦截器实现权限验证)