We use RequestHeaderAuthenticationFilter as to implement pre-authentication strategy and PreAuthenticatedAuthenticationProvider as the authentication provider. One of the requirements is to store all successful logins to the database with following information. As user IP address and other request related info is not available in UserDetailsService class, what is the best strategy to retrieve this info and store in db?
We use RequestHeaderAuthenticationFilter as to implement pre-authentication strategy and PreAuthenticatedAuthenticationProvider as the authentication provider.
Share
All the information is available through
HttpServletRequest. You can obtain it by:Dependency injection
The easiest way would be to inject servlet request directly into your
UserDetailsService:class:(as suggested by OP) Remember to add the following listener to your
web.xml:UPDATE: This works because Spring injects special scoped proxy implementing
HttpServletRequest, so you are able to access request-scoped request “bean” from singleton-scopedMyDetailsService. Under the hood every call torequest‘s parameters is routed toorg.springframework.web.context.request.RequestContextHolder#requestAttributesHolderThreadLocalwhich you can also access directly. As you can see Spring is very flexible when it comes to scoping rules. It just works.RequestContextHolderAnother approach is to use
RequestContextHolder:Further reading: