Spring AOP: how to get the annotations of the adviced method
Posted
by hubertg
on Stack Overflow
See other posts from Stack Overflow
or by hubertg
Published on 2010-04-01T09:05:30Z
Indexed on
2010/04/01
9:13 UTC
Read the original article
Hit count: 618
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);
}
The method above always returns null (there are no annotations found at all). Is there a simple solution to this?
I read something about using the AspectJ agent but I would prefer not to use this agent.
© Stack Overflow or respective owner