I’m trying to change the Locale french to english. Everyting work well with thymeleaf by changing it with my own resolver. But when I’m trying to update the Spring Boot security. Nothing work cause it got is own messageSources().
Here my MessageResouce @Bean for my Spring Boot and I was expecting that Spring Security use the same LocaleResolver. But it’s not.
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:lang/messages");
messageSource.setFallbackToSystemLocale(true);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setDefaultLocale(Locale.ENGLISH);
return messageSource;
}
And my LocaleResolver
@Bean
public LocaleResolver localeResolver() {
CustomLocaleResolver localeResolver = new CustomLocaleResolver(userService);
localeResolver.setDefaultLocale(localeResolver.getLocale());
return localeResolver;
}
And here my custom Resolver:
@Component
public class CustomLocaleResolver extends AcceptHeaderLocaleResolver {
private Log logger;
private UserService userService;
private Locale locale;
public CustomLocaleResolver(UserService userService) {
logger = LogFactory.getLog(this.getClass());
this.userService = userService;
}
@Override
public Locale resolveLocale(HttpServletRequest request) {
logger.debug("resolving for the best Locale choice");
// 1. verify if user is authenticated and get locale from user
locale = verifyUserForLocale();
if(locale != null) {
logger.debug("locale user lang: " + locale);
return locale;
}
// 2. verify sessions attributes for locale
if(request.getSession().getAttribute("locale") != null) {
locale = Locale.forLanguageTag((String)request.getSession().getAttribute("locale"));
if(locale != null) {
logger.debug("locale session: " + locale);
return locale;
}
}
// 3. verify if cookie exist and get locale from cookie
locale = verifyCookieForLocale(request);
if(locale != null) {
logger.debug("locale cookie: " + locale);
return locale;
}
logger.debug("No user preference, session attribute or cookie found: Using default locale");
// 3. verify if header exist and get locale from header
return super.resolveLocale(request);
}
private Locale verifyHeaderForLocale(HttpServletRequest request) {
String headerLang = request.getHeader("Accept-Language");
logger.debug("headerLang: " + headerLang);
return Locale.getDefault();
}
private Locale verifyCookieForLocale(HttpServletRequest request) {
if(request.getCookies() == null) {
return null;
}
Cookie[] cookies = request.getCookies();
for(Cookie c : cookies) {
//logger.info("cookie: " + c.getName() + " " + c.getValue());
if(c.getName().equals("lang")) {
logger.debug("cookie: " + c.getValue());
return new Locale(c.getValue());
}
}
return null;
}
private Locale verifyUserForLocale() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if(authentication == null) {
return null;
}
if(authentication.isAuthenticated() && authentication.getName() != "anonymousUser") {
String username = authentication.getName();
Locale locale = userService.getLocaleByUsername(username);
if(locale == null) {
logger.error("No locale found for user: " + username);
return null;
}
logger.debug("locale user: " + locale);
return locale;
}
return null;
}
public Locale getLocale() {
return locale;
}
}
New contributor
Mathieu Green is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.