Struts2的interceptor实现权限管理

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

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

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

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

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

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

Xml代码

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

 

 

 

 

 

Java代码

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

 

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

2009-07-29 09:54:34

struts2和str

2012-04-25 10:14:40

JavaStruts

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-06-25 15:59:21

Struts2教程拦截器

2009-02-04 10:51:07

2012-05-10 14:00:06

StrutsjsonJava

2009-07-14 17:10:44

struts2webwork

2009-06-04 09:20:19

struts2 if标使用

2009-02-04 14:45:06

2009-06-04 08:45:01

Struts2下载

2009-06-18 11:37:24

Struts2中ForJavaScript

2009-06-04 08:01:25

Struts2拦截器原理

2009-07-03 09:35:57

Struts2 JSP

2009-06-04 08:34:24

Struts2配置struts.xml

2009-06-08 16:44:00

Struts2文件上传

2009-06-05 10:05:50

struts menustruts2

2011-05-13 09:53:02

strutsAjax

2013-05-28 11:29:19

struts2

2009-06-08 16:44:00

2009-06-25 15:26:25

Struts2教程struts.xml常
点赞
收藏

51CTO技术栈公众号