【发布时间】:2021-08-17 19:01:43
【问题描述】:
如何在 Spring MVC 中获取 HttpServletRequest?
在尝试获取 HttpServletRequest 时,我得到了一个异常。
留言 No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
请告诉我如何解决这个问题?
@Component
public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {
@Autowired
private HttpServletRequest request;
@Override
public void onApplicationEvent(AuthenticationSuccessEvent a) {
System.out.println(request.getRemoteAddr());
}
}
【问题讨论】:
标签: java spring spring-mvc spring-security