我希望在Spring MVC中创建的会话上将一些客户端信息(IP地址等)保存到数据库中。
我创建了一个实现HttpSessionListener的类,并在web.xml中对其进行了配置。但是,我不确定之后该去哪里。
还希望能够注入一个bean(Spring Data JPA存储库)。
我已经看到了创建会话时如何获取IP地址?,但是,如果我尝试访问RequestContextHolder.currentRequestAttributes(),则会收到以下异常:
SEVERE: Session event listener threw exception
java.lang.IllegalStateException: 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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
您可以使用Spring java config这样创建一个bean:
@Bean
@Named ("IP")
@Scope ("session")
public String ip (HttpServletRequest request) {
return request.getRemoteAddr ();
}
如果您只想记录日志,则应使用HttpSessionListener,请提供您的源代码和完整的堆栈跟踪。如有必要,请使用pastebin.com。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句