Struts2的interceptor实现权限管理

来源:java认证发布时间:2012-11-12 13:15:27java认证视频

    这是以前写过的一个利用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"/>
    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);
    }

    编辑特别推荐:

    Servlet中的八大Listener

    40个轻量级JavaScript库介绍

    Java网络编程:实现HTTP模拟器

上一页12下一页

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答