Struts2的interceptor实现权限管理
创始人
2024-07-27 04:30:51
0

这是以前写过的一个利用struts2的interceptor进行权限管理的笔记,以前是放电脑上的,今天偶然看到了,就贴出来,希望能对有需要的人有点帮助,同时自己以后需要看的时候也会更加方便点!

说明一点:这个interceptor里面的代码是根据我特定的项目写的,所以请有需要的人不要盲目的照搬!

自己写一个interceptor,该interceptor继承interceptor接口,实现其中的intercept方法;然后在struts.xml

中进行配置,并把该interceptor置于默认的interceptor中,注意,这里在设置默认的intercept的时候

一定要加上原来的intercept,否则原来的就不可以用了,就不能用struts2了,具体来说是这样:

Xml代码

  1.     
  2.         
  3.         
  4.             
  5.             
  6.         
  7.     
  8.     

 

 

 

 

 

Java代码

  1. @Override    
  2.     public String intercept(ActionInvocation invoke) throws Exception {     
  3.         // TODO Auto-generated method stub     
  4.         HttpSession session  = ServletActionContext.getRequest().getSession();     
  5.         ApplicationContext context = Util.getContext(ServletActionContext.getServletContext());     
  6.         PriorityService priorityService = context.getBean(PriorityService.class);     
  7.              
  8.         String actionName = invoke.getProxy().getActionName();     
  9.         String methodName = invoke.getProxy().getMethod();     
  10.         if ("execute".equals(methodName))      
  11.             methodName = "index";     
  12.         int index = actionName.indexOf("/");     
  13.         String name = actionName.substring(0, index);     
  14.              
  15.         Priority priority = priorityService.find(name, methodName);     
  16.         Object obj = session.getAttribute("user");     
  17.         if (obj != null) {     
  18.             User currentUser = (User) obj;     
  19.                  
  20.             ModuleService moduleService = context.getBean(ModuleService.class);     
  21.             Module module = moduleService.findByUrl(name+"/"+methodName);     
  22.             if (module != null) {     
  23.                 SystemDiaryService sdService = context.getBean(SystemDiaryService.class);     
  24.                 SystemDiary diary = new SystemDiary();     
  25.                 diary.setOperator(currentUser);     
  26.                 diary.setOperateModule(module.getName());     
  27.                 sdService.add(diary);     
  28.             }     
  29.                  
  30.             if (priority != null) {     
  31.                 boolean hasPermission = currentUser.hasPermission(priority);     
  32.                      
  33.                 if (!hasPermission) {     
  34.                     return "forbidden";     
  35.                 }     
  36.             }     
  37.         }     
  38. //      System.out.println("name = "+name + "**actionName = "+actionName+"*methodName = "+methodName);     
  39.     
  40.         String result = invoke.invoke();     
  41.         return result;     
  42.     }  

 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...