代码Demo
@Aspect
@Component
@Order(10)
public class BidAuthorityProxy {
/**
* 扫描指定包下的类中使用@EnableRoleAuthority注解修饰的类
*/
@Around("@within(com.core.annotation.EnableRoleAuthority) && within(com.bid..*)")
public Object verifyRoleExecuteCommand(ProceedingJoinPoint pjp) throws Throwable {
// 获取当前拦截方法的对象
MethodSignature msig = (MethodSignature) pjp.getSignature();
Method targetMethod = pjp.getTarget().getClass().getDeclaredMethod(msig.getName(), msig.getMethod().getParameterTypes());
// 获取当前方法注解中的值
VerifyRoleAuthority annotation = targetMethod.getAnnotation(VerifyRoleAuthority.class);
// 如果类上面没有注解,则获取接口上此方法的注解
if (annotation == null) {
Class<?>[] inters = pjp.getTarget().getClass().getInterfaces();
for (Class<?> inter : inters) {
Method targetInterMethod = inter.getDeclaredMethod(msig.getName(), msig.getMethod().getParameterTypes());
annotation = targetInterMethod.getAnnotation(VerifyRoleAuthority.class);
if (annotation != null) {
break;
}
}
}
// 获取到注解中的值后进行后续自定义逻辑操作
return pjp.proceed();// 执行方法
}
}
相关说明:
AOP中扫描指定注解相关说明
(1)@annotation:用来拦截所有被某个注解修饰的方法
(2)@within:用来拦截所有被某个注解修饰的类
(3)within:用来指定扫描的包的范围

本文详细介绍了一种基于Spring AOP的注解扫描技术,通过@Aspect和@Within结合使用,实现在指定包下对使用@EnableRoleAuthority注解的类进行拦截。并展示了如何在@Around注解的方法内获取目标方法的注解信息,进行权限验证逻辑的实现。
2412

被折叠的 条评论
为什么被折叠?



