struts2 之 interceptor权限管理

这是以前写过的一个利用struts2的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);
}
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;
} 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;
}();
return result;
} result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值