Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current user with spring aop

Tags:

spring-aop

I use spring boot 2 and spring security.

With aop, I search to get user who called the method.

@Aspect
@Component
public class LogAspect {

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

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

    }
}

any idea?

like image 830
robert trudel Avatar asked Apr 23 '26 15:04

robert trudel


1 Answers

try to add in you method this line

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
like image 89
Marc Collin Avatar answered Apr 30 '26 13:04

Marc Collin