In Kotlin language, I configured a Spring AOP annotation like this:
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Authenticated(val roles: Array<String>)
... and the aspect class like this:
@Aspect
@Component
class AuthenticationAspect {
@Around("@annotation(Authenticated) && args(roles)", argNames = "roles")
@Throws(Throwable::class)
fun authenticate(joinPoint: ProceedingJoinPoint, roles: Array<String>):Any? {
//.. do stuff
return proceed
}
}
And in my methods I add the annotation like this:
@Authenticated(roles = ["read", "write"])
fun someMethod(msg: Pair) {
// do stuff...
}
The annotation works well without arguments, i.e., the annotated method gets intercepted. But with the argument "roles" it never gets matched and I have no clue why. Any help would be much appreciated.
When you use "&& args(roles)" you are looking for parameters named "roles" in the target method, not in the annotation.
You can try and change your aspect to something like this:
@Around("@annotation(authenticated))
@Throws(Throwable::class)
fun authenticate(joinPoint: ProceedingJoinPoint, authenticated: Authenticated):Any? {
val roles = authenticated.roles
//.. do stuff
return proceed
}