Как регистрировать SavedRequestAwareAuthenticationSuccessHandler.onAuthenticationSuccess с помощью Spring Aspect?

Я пытаюсь войти, когда пользователь успешно вошел в систему с помощью Spring Security. Я использую Аспект ведения журнала:

@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, но в консоли ничего не записывается.

Я использую образец, описанный в Захват успешного входа в систему с помощью AspectJ и Spring Security но это не работает.

Любая идея ?

Спасибо


person Jonathan Lebrun    schedule 21.01.2014    source источник


Ответы (1)


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

Я создал новый bean-компонент SuccessHandler:

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

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

}

И второй момент - добавить его как bean-компонент в конфигурацию и установить его в formLogin:

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

http.authorizeRequests().antMatchers("/*").permitAll().and()
            .formLogin()
            .successHandler(successHandler)
            .permitAll().and().logout().permitAll();
person Jonathan Lebrun    schedule 21.01.2014