新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Struts2的interceptor實(shí)現(xiàn)權(quán)限管理
這是以前寫過的一個利用struts2的interceptor進(jìn)行權(quán)限管理的筆記,以前是放電腦上的,今天偶然看到了,就貼出來,希望能對有需要的人有點(diǎn)幫助,同時(shí)自己以后需要看的時(shí)候也會更加方便點(diǎn)!

說明一點(diǎn):這個interceptor里面的代碼是根據(jù)我特定的項(xiàng)目寫的,所以請有需要的人不要盲目的照搬!
自己寫一個interceptor,該interceptor繼承interceptor接口,實(shí)現(xiàn)其中的intercept方法;然后在struts.xml
中進(jìn)行配置,并把該interceptor置于默認(rèn)的interceptor中,注意,這里在設(shè)置默認(rèn)的intercept的時(shí)候
一定要加上原來的intercept,否則原來的就不可以用了,就不能用struts2了,具體來說是這樣:
Xml代碼
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;
- }
本文名稱:Struts2的interceptor實(shí)現(xiàn)權(quán)限管理
標(biāo)題路徑:http://fisionsoft.com.cn/article/djecioc.html


咨詢
建站咨詢
