Spring boot with Spring Security – How to change locale message error

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật