CheckLoginInterceptor.java---拦截器具体实现类:
- package com.sunhoo.hcpms.struts2.action.interceptors;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
- import com.sunhoo.hcpms.struts2.action.LoginAction;
- /**
- * 拦截器 拦截没有登录的用户
- *
- * @author lyvee AbstractInterceptor 抽象类
- */
- public class CheckLoginInterceptor extends AbstractInterceptor {
- public static ActionContext ctx;
- public static Map sessionMap = null;
- String task = "userLogin";
- @Override
- public String intercept(ActionInvocation actionInvocation) throws Exception {
- ctx = actionInvocation.getInvocationContext();
- sessionMap = actionInvocation.getInvocationContext().getSession();
- HttpServletRequest request = (HttpServletRequest) ctx
- .get(ServletActionContext.HTTP_REQUEST);
- String user_session_key = "currentUser";// 默认的key
- System.out.println("begin check login interceptor");
- // not to intercepted on loginAction
- Object action = actionInvocation.getAction();
- if (action instanceof LoginAction) {
- System.out.println("exit check login,because this is loginAction");
- actionInvocation.invoke();
- return null;
- } else {
- String returnStr = "logout";
- if (null == request.getParameter("task") || "".equals(task)) {//如果task为空 则等于userLogin
- task = "userLogin";
- }else {
- task = request.getParameter("task");
- }if (task.equals("userLogin")) {// 根据不同的登录类型赋予user_session_key不同的value
- user_session_key = "currentUser";
- } else if (task.equals("expertLogin")) {
- user_session_key = "currentExpert";
- returnStr="expertlogout";
- } else if (task.equals("adminLogin")) {
- user_session_key = "currentAdmin";
- returnStr="adminLogout";
- }
- // check session
- @SuppressWarnings("unused")
- Object okk = sessionMap.get(user_session_key);
- String userName = (String) sessionMap.get(user_session_key);
- // if userName already login
- if (userName != null) {
- System.out.println("already login!");
- actionInvocation.invoke();
- } else {// no login
- System.out.println("no login.forward login page");
- return returnStr;
- }
- }
- return null;
- }
- }
struts.xml配置:
- <package name="default" namespace="/" extends="struts-default">
- <!-- 定义一个名为 checkLogin 的拦截器 -->
- <interceptors>
- <!-- 定义权限检查拦截器 类级别-->
- <interceptor name="checkLogin"
- class="com.sunhoo.hcpms.struts2.action.interceptors.CheckLoginInterceptor" />
- <!-- 定义一个权限检查的拦截器 方法级别 -->
- <interceptor name="methodInterceptor" class="com.sunhoo.hcpms.struts2.action.interceptors.MethodInterceptor">
- <param name="excludeMethods"></param>
- <param name="includeMethods">expertTree</param>
- </interceptor>
- <!-- 定义一个包含权限检查的拦截器栈 -->
- <interceptor-stack name="myDefaultStack">
- <!-- 定义拦截器栈包含checkLogin拦截器 -->
- <interceptor-ref name="checkLogin"></interceptor-ref>
- <interceptor-ref name="defaultStack"></interceptor-ref>
- <interceptor-ref name="methodInterceptor"></interceptor-ref>
- </interceptor-stack>
- </interceptors>
- <!-- 设置全局 全局默认的拦截器栈-->
- <default-interceptor-ref name="myDefaultStack"></default-interceptor-ref>
- <global-results>
- <result name="logout" type="dispatcher">/login.jsp</result>
- <result name="succ">/client/succ.jsp</result>
- <result name="fail">/client/fail.jsp</result>
- <result name="expertlogout" type="redirect">/expert/login.jsp?errorMsg=${errorMsg}</result>
- <result name="adminLogout" type="redirect"></result>
- </global-results>
- <action name="login_*" class="LoginAction" method="{1}">
- <result name="index" type="redirect">
- /question_questionList?task=userLogin
- </result>
- <result name="expertQuestions" type="redirect">
- /expert_queryQuestions?task=expertLogin
- </result>
- <result name="go" type="redirect">
- expertList.jsp
- </result>
- <interceptor-ref name="myDefaultStack" />
- </action>
- <action name="register_*" class="RegisterAction" method="{1}">
- <interceptor-ref name="methodInterceptor" />
- </action>
- <action name="expert_*" class="ExpertAction" method="{1}">
- <result name="index">
- /expert/questionList.jsp?task=expertLogin
- </result>
- <result name="expertView">/client/expertList.jsp?task=expertLogin</result>
- </action>.............................
以上就是我的拦截器配置
我遇到的问题是 现在有三种不同类型的用户需要验证登录 1.专家 2.会员 3.专家。通过
- task = request.getParameter("task");
来判断登录用户的类型 然后取得不同的session值来进行拦截。 我不知道这种方法行不行。不是说其他的行不行 因为我已经实现了 并且成功进行了三种不同类型的用户登录来拦截 反正就是我心里觉得这么做不对,不知道JE上有没有人做过类似的 请问你们怎么做的。