使用 spring aop 获取当前用户

Get current user with spring aop

我使用 spring 启动 2 和 spring 安全。

使用 aop,我搜索以获取调用该方法的用户。

@Aspect
@Component
public class LogAspect {

    @Before("execution(* com.moplogy.controller.*))")
    public void getAllAdvice(JoinPoint joinPoint){

        System.out.println("method called " + joinPoint.getSignature().getName());

    }
}

有什么想法吗?

尝试在你的方法中添加这一行

Authentication auth = SecurityContextHolder.getContext().getAuthentication();