Get current logged in user in Spring AOP using Spring Webflux

789 views Asked by At

I am using Spring Webflux with Kotlin Coroutine and Spring AOP. I have simple Advice which is like this.

@Around("@annotation(LogAccess)")
public Object logErrorAccess(ProceedingJoinPoint joinPoint) throws Throwable {

    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = methodSignature.getMethod();
    LogAccess logAccess = method.getAnnotation(LogAccess.class);

    try {
        var result = joinPoint.proceed();
        // Get the current user and user id and log
        return result;
    } catch (Exception e) {
        // Get the current user and user id and log
        throw e;
    }
}

I tried to use this.

ReactiveSecurityContextHolder.getContext().map(ct -> ct.getAuthentication().getPrincipal());

But I am getting null. I am using JWT for Auth, However in my controller I can access that information like this.

@GetMapping("/profile")
suspend fun profile(authentication: Authentication): Response

Is there any way to get this info in Advice somehow.

0

There are 0 answers