[Java] 登录拦截 →→→→→进入此内容的聊天室

来自 , 2019-08-12, 写在 Java, 查看 167 次.
URL http://www.code666.cn/view/86e8f7ab
  1. package com.app.common;
  2.  
  3. import org.apache.log4j.Logger;
  4.  
  5. import com.app.model.User;
  6. import com.opensymphony.xwork2.ActionContext;
  7. import com.opensymphony.xwork2.ActionInvocation;
  8. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  9.  
  10. public class PrivilegeInterceptor extends AbstractInterceptor{
  11.  
  12.         /**
  13.          *
  14.          */
  15.         private static final long serialVersionUID = 5851995979071627976L;
  16.  
  17.         private static final Logger logger = Logger.getLogger(PrivilegeInterceptor.class);
  18.        
  19.         @Override
  20.         public String intercept(ActionInvocation invocation) throws Exception {
  21.                 logger.info("PrivilegeInterceptor:Action方法拦截");
  22.  
  23.                 //获取当前用户
  24.                 User user = (User) ActionContext.getContext().getSession().get("loginUser");
  25.                
  26.                 //获取当前访问的URL,并去掉当前应用程序的前缀(也就是 namespaceName + actionName )
  27.                 String namespace = invocation.getProxy().getNamespace();
  28.                 String actionName = invocation.getProxy().getActionName();
  29.                
  30.                 logger.info("namespace:" + namespace +" , "+"actionName:" + actionName);
  31.                
  32.                 String privilegeUrl = null;
  33.                 if(namespace.endsWith("/")){
  34.                         privilegeUrl = namespace + actionName;
  35.                 }else{
  36.                         privilegeUrl = namespace + "/" + actionName;
  37.                 }
  38.                
  39.                 //要去掉开头的'/'
  40.                 if(privilegeUrl.startsWith("/")){
  41.                         privilegeUrl = privilegeUrl.substring(1);//从1开始截取
  42.                 }
  43.                
  44.                 //String result = null;
  45.                
  46.                 //如果未登录用户
  47.                 if(user==null){
  48. //                      if(privilegeUrl.endsWith("/login_index" + Constant.S2_ACTION_EXT)
  49. //                                      || privilegeUrl.endsWith("/login_login" + Constant.S2_ACTION_EXT )){
  50. //                              //如果是正在使用登录功能,就放行
  51. //                              result = invocation.invoke();
  52. //                      }else{
  53.                                 //如果不是去登录,就转到登录页面
  54.                                 return "login";
  55. //                      }
  56.                 }
  57.                 //如果已经登录,就判断权限
  58.                 else{
  59.                         //if(user.hasPrivilegeByUrl(privilegeUrl)){
  60.                                 //如果有权限就放行
  61.                                 return invocation.invoke();
  62.                         //}else{
  63.                                 //如果没有权限,返回无权限信息
  64.                         //      return "noPrivilegeError";
  65.                         //}
  66.                 }
  67.                
  68.                 //return result;
  69.                
  70.         }
  71.  
  72. }
  73.  

回复 "登录拦截"

这儿你可以回复上面这条便签

captcha