黑马程序员技术交流社区
标题:
关于SpringAop的一个问题
[打印本页]
作者:
繁华终成泣
时间:
2013-11-7 17:22
标题:
关于SpringAop的一个问题
我用Struts2+Spring2.5+JPA写了一个小型电子商务网站,用SpringAop做权限拦截出现了一个小问题,以下是代码:
package cn.djb.aop;
import java.lang.reflect.Method;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import cn.djb.annotation.Permission;
import cn.djb.domain.Privilege;
import cn.djb.domain.Role;
import cn.djb.exception.PrivilegeException;
import cn.djb.service.role.RoleService;
@Aspect
@Component
public class MyAspect {
@Resource RoleService roleServiceImpl;
@Pointcut("execution(* cn.djb.service..*.*(..))")
private void anyMethod() {}
@Around("anyMethod()")//环绕通知
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
//判断请求uri是否是后台管理系统,是则拦截,不是则放行
if(ServletActionContext.getRequest().getRequestURI().startsWith
("/ssjbookstore/manager"))
{
//如果请求uri对应的是后台管理员登陆和注销就放行
if(ServletActionContext.getRequest().getRequestURI().startsWith
("/ssjbookstore/manager/role_log"))
{
return pjp.proceed();
}
//如果不是则从session中获取登陆的角色
Role r=(Role) ServletActionContext.getRequest().getSession
().getAttribute("role");
//如果没有获取到抛出用户没有登陆的权限异常,这个异常是我自定义的
运行时异常
if(r==null){
throw new PrivilegeException("您还没有登录!!!");
}
//如果获取到了,则获取目标对象
Object target=pjp.getTarget();
//得到代理对象调用的方法
Method method=((MethodSignature)pjp.getSignature()).getMethod();
//通过目标对象和代理对象方法的属性获取目标对象上的真实方法
Method realMethod=target.getClass().getMethod(method.getName(),
method.getParameterTypes());
//获取真实方法上的权限注解
Permission p=realMethod.getAnnotation(Permission.class);
//如果方法上没有注解则放行
if(p==null){
return pjp.proceed();
}
//用注解上权限名称创建一个权限,权限对象上的equals()方法我改了
,只要权限名相同就认为是相同的
Privilege privilege=new Privilege(p.value());
//根据r的用户名和密码再次从数据库中找出该角色,防止中途登陆后对用
户权限进行更改同步不到session中
r=roleServiceImpl.getRole(r.getName(), r.getPassword());
//判断用户身上的权限是否包含注解上的权限,是则放行,方法结束。否
则抛出没有权限的异常
if(r.getPrivileges().contains(privilege)){
return pjp.proceed();
}
throw new PrivilegeException("你没有权限执行该操作!!!");
}
return pjp.proceed();
}
}
复制代码
拦截其他方法没问题,但是拦截一个用泛型写的添加分类的方法就拦截不到,为什么求解啊。。。:
package cn.djb.service.category.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import cn.djb.annotation.Permission;
import cn.djb.dao.base.DaoSupport;
import cn.djb.domain.Category;
import cn.djb.service.category.CategoryService;
@Service
public class CategoryServiceImpl extends DaoSupport<Category> implements CategoryService
{
@Override
@Permission("添加分类")
public void add(Category entity) {
// TODO Auto-generated method stub
super.add(entity);
}
@Override
@Permission("查看分类")
public List<Category> getAll() {
// TODO Auto-generated method stub
return super.getAll();
}
}
复制代码
作者:
狼王
时间:
2013-11-9 19:54
这玩意儿,没学,不知道怎么回答
作者:
pireteMrZ
时间:
2013-11-10 11:08
高端....没学过,不懂...{:3_54:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2