How to get httprequest and httpresponse in ApplicationListener in Spring Boot application?

时间:2017-06-15 09:46:35

标签: spring spring-boot spring-security

I would like to set browser cookie value after login successfully. I am able to detect success login event with following code.

@Component
@Slf4j
public class LoginEventListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {

    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {

        log.info("hahaha: " + event.toString());
    }
}

2 个答案:

答案 0 :(得分:2)

尝试将HttpServletRequest对象自动装配到LoginEventListener组件中,前提是您的组件在servlet容器中运行。

@Component
@Slf4j
public class LoginEventListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {

    @Autowired
    private HttpServletRequest request;    

    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
        log.info("hahaha: " + event.toString());
        log.info("Request Object: " + request); // You now have access to the HTTP request object.
    }

}

答案 1 :(得分:1)

Tks,只是分享另一种方式来获得它。

foo