黑马程序员技术交流社区

标题: 关于SpringAop的一个问题 [打印本页]

作者: 繁华终成泣    时间: 2013-11-7 17:22
标题: 关于SpringAop的一个问题
我用Struts2+Spring2.5+JPA写了一个小型电子商务网站,用SpringAop做权限拦截出现了一个小问题,以下是代码:
  1. package cn.djb.aop;


  2. import java.lang.reflect.Method;

  3. import javax.annotation.Resource;

  4. import org.apache.struts2.ServletActionContext;
  5. import org.aspectj.lang.ProceedingJoinPoint;
  6. import org.aspectj.lang.annotation.Around;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.aspectj.lang.annotation.Pointcut;
  9. import org.aspectj.lang.reflect.MethodSignature;
  10. import org.springframework.stereotype.Component;


  11. import cn.djb.annotation.Permission;
  12. import cn.djb.domain.Privilege;
  13. import cn.djb.domain.Role;
  14. import cn.djb.exception.PrivilegeException;
  15. import cn.djb.service.role.RoleService;

  16. @Aspect
  17. @Component
  18. public class MyAspect {
  19.         @Resource RoleService roleServiceImpl;
  20.         @Pointcut("execution(* cn.djb.service..*.*(..))")
  21.         private void anyMethod() {}
  22.        
  23.         @Around("anyMethod()")//环绕通知
  24.         public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
  25.                 //判断请求uri是否是后台管理系统,是则拦截,不是则放行
  26.                 if(ServletActionContext.getRequest().getRequestURI().startsWith

  27. ("/ssjbookstore/manager"))
  28.                 {       
  29.                         //如果请求uri对应的是后台管理员登陆和注销就放行
  30.                         if(ServletActionContext.getRequest().getRequestURI().startsWith

  31. ("/ssjbookstore/manager/role_log"))
  32.                         {
  33.                                 return pjp.proceed();
  34.                         }
  35.                         //如果不是则从session中获取登陆的角色
  36.                         Role r=(Role) ServletActionContext.getRequest().getSession

  37. ().getAttribute("role");
  38.                         //如果没有获取到抛出用户没有登陆的权限异常,这个异常是我自定义的

  39. 运行时异常
  40.                         if(r==null){
  41.                                 throw new PrivilegeException("您还没有登录!!!");
  42.                         }
  43.                         //如果获取到了,则获取目标对象
  44.                         Object target=pjp.getTarget();
  45.                         //得到代理对象调用的方法
  46.                         Method method=((MethodSignature)pjp.getSignature()).getMethod();
  47.                         //通过目标对象和代理对象方法的属性获取目标对象上的真实方法
  48.                         Method realMethod=target.getClass().getMethod(method.getName(),

  49. method.getParameterTypes());
  50.                         //获取真实方法上的权限注解
  51.                         Permission p=realMethod.getAnnotation(Permission.class);
  52.                         //如果方法上没有注解则放行
  53.                         if(p==null){
  54.                                 return pjp.proceed();
  55.                         }
  56.                         //用注解上权限名称创建一个权限,权限对象上的equals()方法我改了

  57. ,只要权限名相同就认为是相同的
  58.                         Privilege privilege=new Privilege(p.value());
  59.                         //根据r的用户名和密码再次从数据库中找出该角色,防止中途登陆后对用

  60. 户权限进行更改同步不到session中
  61.                         r=roleServiceImpl.getRole(r.getName(), r.getPassword());
  62.                         //判断用户身上的权限是否包含注解上的权限,是则放行,方法结束。否

  63. 则抛出没有权限的异常
  64.                         if(r.getPrivileges().contains(privilege)){
  65.                                 return pjp.proceed();
  66.                         }
  67.                         throw new PrivilegeException("你没有权限执行该操作!!!");
  68.                        
  69.                        
  70.                 }
  71.                
  72.                 return pjp.proceed();
  73.         }


  74. }
复制代码
拦截其他方法没问题,但是拦截一个用泛型写的添加分类的方法就拦截不到,为什么求解啊。。。:
  1. package cn.djb.service.category.impl;

  2. import java.util.List;
  3. import org.springframework.stereotype.Service;
  4. import cn.djb.annotation.Permission;
  5. import cn.djb.dao.base.DaoSupport;
  6. import cn.djb.domain.Category;
  7. import cn.djb.service.category.CategoryService;
  8. @Service
  9. public class CategoryServiceImpl extends DaoSupport<Category> implements CategoryService

  10. {

  11.         @Override
  12.         @Permission("添加分类")
  13.         public void add(Category entity) {
  14.                 // TODO Auto-generated method stub
  15.                 super.add(entity);
  16.         }

  17.        

  18.         @Override
  19.         @Permission("查看分类")
  20.         public List<Category> getAll() {
  21.                 // TODO Auto-generated method stub
  22.                 return super.getAll();
  23.         }



  24.        
  25. }
复制代码

作者: 狼王    时间: 2013-11-9 19:54
这玩意儿,没学,不知道怎么回答
作者: pireteMrZ    时间: 2013-11-10 11:08
高端....没学过,不懂...{:3_54:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2