如何在 Spring Boot 应用程序中的 ApplicationListener 中获取 httprequest 和



我想在成功登录后设置浏览器cookie值。我能够使用以下代码检测成功的登录事件。

@Component
@Slf4j
public class LoginEventListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
        log.info("hahaha: " + event.toString());
    }
}
尝试将

对象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.
    }
}

Tks,只是分享另一种获得它的方法。

HttpServletRequest request =
                ((ServletRequestAttributes) RequestContextHolder.
                        currentRequestAttributes()).
                        getRequest();

最新更新