使用Spring Webflux在Spring AOP中获取当前登录用户

问题描述

我正在将Spring Webflux与Kotlin Coroutine和Spring AOP一起使用。我有这样的简单建议。

@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;
    }
}

我尝试使用它。

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

但是我得到null。我正在使用JWT进行身份验证,但是在我的控制器中,我可以像这样访问该信息。

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

是否可以通过某种方式在“建议”中获取此信息。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...