微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用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 举报,一经查实,本站将立刻删除。