Reputation: 1187
I'm working with spring security and I have implemented my costume UserDetailsService.loadUserByUsername that returns a costume UserDetails. My problem is that I want in my UserDetailsService implementation to get information from the HttpServletRequest, more specific I would like to get request.getLocale();
How can i do this?
Upvotes: 0
Views: 915
Reputation: 1
I'm leaving this for others as I could not find the answer elsewhere. After some tinkering, I found that you can just add the request to the bean as such:
@Configuration
public class SecurityConfig {
@Bean
public UserDetailsService userDetailsService(MyRepository repository, HttpServletRequest request) {
return username -> {
UserDetails userDetails = repository.findByEmail(username);
if (userDetails != null) {
log.info("login." + userDetails);
return userDetails;
}
String locale = request.getLocale().getDisplayName();
String message = "not.found[" + username + "].locale[" + locale + "]";
log.error(message);
throw new UsernameNotFoundException(message);
};
}
}
Upvotes: 0
Reputation: 7568
You can pass it from the controller to your Service , adding a new parameter to your Service
Upvotes: 0
Reputation: 279970
You should be able (depending on your config) to use
LocaleContextHolder.getLocaleContext().getLocale();
LocaleContextHolder
stores a static
ThreadLocal
object that stores the Locale
for your request.
You might have to register a RequestContextListener
with your Servlet container.
Upvotes: 1