2014-01-21 3 views
0

Я пытаюсь войти, когда пользователь успешно зарегистрирован в Spring Security. Я использую Logging Aspect:Как зарегистрировать SavedRequestAwareAuthenticationSuccessHandler.onAuthenticationSuccess с Spring Aspect?

@Aspect 
@Component 
public class LoggingAspect { 
static Logger log = Logger.getLogger(LoggingAspect.class); 

@Before("execution(* com.jle.athleges.web.controller.MemberController.*(..))") 
public void logBefore(JoinPoint joinPoint) { 
    log.info("INFO - logBefore() is running!"); 
    log.info(joinPoint.getSignature().getName()); 
} 

@AfterReturning(pointcut = "execution(* org.springframework.security.authentication.AuthenticationManager.authenticate(..))", returning = "result") 
public void after(JoinPoint joinPoint, Object result) throws Throwable { 
    log.info(">>> user: " + ((Authentication) result).getName()); 
} 

@Around("execution(* org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler.onAuthenticationSuccess(..))") 
public void onAuthenticationSuccess(){ 
    log.info(">>> user " + (SecurityContextHolder.getContext().getAuthentication().getName()) + " is now connected"); 
} 
} 

Метод после работает отлично, но журнал дважды. Я пытаюсь использовать onAuthenticationSuccess, но в консоли ничего не нажимается.

Я использую образец, объясненный в Capture successful login with AspectJ and Spring Security, но он не работает.

Любая идея?

Thanks

ответ

0

Я нашел решение!

Я создал новый SuccessHandler боб:

public class SecurityAuthenticationSuccessHandler extends 
    SimpleUrlAuthenticationSuccessHandler { 
@Override 
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 
    Authentication authentication) throws IOException, ServletException { 

    super.onAuthenticationSuccess(request, response, authentication); 
} 

}

И вторая точка является, чтобы добавить его в качестве компонента в области конфигурации и установить его в formLogin:

@Bean 
public SecurityAuthenticationSuccessHandler getSuccessHandler(){ 
    return new SecurityAuthenticationSuccessHandler(); 
} 

http.authorizeRequests().antMatchers("/*").permitAll().and() 
      .formLogin() 
      .successHandler(successHandler) 
      .permitAll().and().logout().permitAll(); 
Смежные вопросы