Struts2的interceptor实现权限管理

开发 后端
这是作者以前写过的一个利用struts2的interceptor进行权限管理的笔记,以前是放电脑上的,今天偶然看到了,就贴出来,希望能对有需要的人有点帮助

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

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

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

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

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

Xml代码

  1. <interceptors>    
  2.     <interceptor name="authentication" class="com.tiantian.tiantian.web.interceptor.AuthenticationInterceptor"></interceptor>    
  3.     <interceptor-stack name="myInterceptorStack">    
  4.         <interceptor-ref name="authentication"></interceptor-ref>    
  5.         <interceptor-ref name="defaultStack"></interceptor-ref>    
  6.     </interceptor-stack>    
  7. </interceptors>    
  8. <default-interceptor-ref name="myInterceptorStack"/>    

 

 

 

 

 

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.     }  

 

责任编辑:金贺 来源: ITEYE博客
相关推荐

2009-07-29 09:54:34

struts2和str

2012-04-25 10:14:40

JavaStruts

2009-06-25 15:59:21

Struts2教程拦截器

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-02-04 10:51:07

2009-07-14 17:10:44

struts2webwork

2009-06-04 09:20:19

struts2 if标使用

2012-05-10 14:00:06

StrutsjsonJava

2009-06-18 11:37:24

Struts2中ForJavaScript

2009-06-04 08:01:25

Struts2拦截器原理

2009-06-04 08:45:01

Struts2下载

2009-02-04 14:45:06

2009-07-03 09:35:57

Struts2 JSP

2009-06-04 08:34:24

Struts2配置struts.xml

2009-06-05 10:05:50

struts menustruts2

2013-05-28 11:29:19

struts2

2009-06-08 16:44:00

Struts2文件上传

2011-05-13 09:53:02

strutsAjax

2009-06-08 16:44:00

2009-06-03 14:19:34

Struts2Guice
点赞
收藏

51CTO技术栈公众号