问题描述
我想用Spring / AOP和注释实现声明性安全性。
正如您在下一个代码示例中看到的,我有限制注释,参数allowedRoles用于定义允许执行推荐方法的人。
I'd like to implement declarative security with Spring/AOP and annotations.As you see in the next code sample I have the Restricted Annotations with the paramter "allowedRoles" for defining who is allowed to execute an adviced method.
@Restricted(allowedRoles="jira-administrators")
public void setPassword(...) throws UserMgmtException {
// set password code
...
}
现在,问题是在我的建议中我无法访问定义的内容注释:
Now, the problem is that in my Advice I have no access to the defined Annotations:
public Object checkPermission(ProceedingJoinPoint pjp) throws Throwable {
Signature signature = pjp.getSignature();
System.out.println("Allowed:" + rolesAllowedForJoinPoint(pjp));
...
}
private Restricted rolesAllowedForJoinPoint(ProceedingJoinPoint thisJoinPoint)
{
MethodSignature methodSignature = (MethodSignature) thisJoinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
return targetMethod.getAnnotation(Restricted.class);
}
上述方法始终返回null(根本没有找到注释)。
有一个简单的解决方案吗?
The method above always returns null (there are no annotations found at all).Is there a simple solution to this?
我读了一些关于使用AspectJ代理的内容,但我不想使用这个代理。
I read something about using the AspectJ agent but I would prefer not to use this agent.
推荐答案
我假设 @Restricted
是你的注释。如果是这种情况,请确保您拥有:
I assume @Restricted
is your annotation. If that is the case, make sure you have:
@Retention(RetentionPolicy.RUNTIME)
。这意味着注释在运行时保留。
in your annotation definition. This means that the annotation is retained at runtime.
这篇关于Spring AOP:如何获取建议方法的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!