2013-07-03 3 views
1

나는 LDAP를 통해 Active Directory 사용자를 인증하는 스프링 보안 설정을 사용하여 스프링 MVC 애플리케이션을 개발 중이다. AOP를 호출하여 각 컨트롤러 메소드를 기록하려고합니다. 모든 것이 작동하고 있고 메소드를 가로 챌 수 있습니다. 그러나 메소드를 실행하는 로그인 한 사용자의 사용자 이름을 얻기 위해 SecurityContext에 액세스 할 수 없습니다.MethodInterceptor에서 SecurityContext에 어떻게 액세스합니까?

UserInvokedMethodLogger.java

public class UserInvokedMethodLogger implements MethodInterceptor 
{ 
    private SecurityContext security = SecurityContextHolder.getContext(); 

    @Override 
    public Object invoke(MethodInvocation interceptedMethod) throws Throwable 
    { 
     long start = System.currentTimeMillis(); 
     Object result = interceptedMethod.proceed(); 
     long end = System.currentTimeMillis(); 

     String dbgMessage = 
       (security.getAuthentication() != null) 
        ? "User '" + security.getAuthentication().getName() + "' called method '" 
        : "Unauthenticated user called method '" 
       + interceptedMethod.getMethod().getName() 
       + "' which executed in " + (end - start) + "ms."; 

     System.out.println(dbgMessage); 
     return result; 
    } 
} 

내가 출력에 "인증되지 않은 사용자가"항상 기록하고있어 경우에도.

답변

2

각 메서드 호출 중에 SecurityContextHolder.getContext()를 호출 해보십시오. private SecurityContext security 속성을 제거하고 SecurityContextHolder.getContext()를 직접 사용하십시오.

+0

Perfect! 고맙습니다! – JDiPierro

+0

당신은 환영합니다! –

관련 문제