Change locale on login

前端 未结 3 1739
灰色年华
灰色年华 2020-12-28 08:53

I want to change the locale after login to an default locale stored in the user account Spring MVC Application (3.0) with Spring Security (3.0).

I already use the

3条回答
  •  忘掉有多难
    2020-12-28 09:29

    Use the SessionLocaleResolver, and construct it as a bean called "localeResolver". This LocaleResolver will resolve locales by first checking the default locale the resolver was constructed with. If that's null, it will check if a locale has been stored in the session, and if that's null, it will set the session locale based on the Accept-Language header in the request.

    After a user is logged in, you can call localeResolver.setLocale to store a locale to the session for you, you can do this in a servlet filter (be sure to define it in your web.xml AFTER your spring security filter).

    To gain access to your localeResolver (or other beans) from your filter, do something like this in the init method:

    @Override
    public void init(FilterConfig fc) throws ServletException {
        ServletContext servletContext = fc.getServletContext();
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        this.localeResolver = context.getBean(SessionLocaleResolver.class);
    }
    

    Then in the doFilterMethod, you should be able to cast the ServletRequest to an HttpServletRequest, call getRemoteUser, perform any business logic to define that user's locale, and call setLocale on the LocaleResolver.

    Personally, I do not care for the SessionLocaleResolver to use the default local first (I prefer last), however it is really easy to extend and override. If you are interested in checking the session, then the request, then the default, use the following:

    import org.springframework.stereotype.Component;
    import org.springframework.web.util.WebUtils;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.Locale;
    
    // The Spring SessionLocaleResolver loads the default locale prior
    // to the requests locale, we want the reverse.
    @Component("localeResolver")
    public class SessionLocaleResolver extends org.springframework.web.servlet.i18n.SessionLocaleResolver{
    
        public SessionLocaleResolver(){
            //TODO: make this configurable
            this.setDefaultLocale(new Locale("en", "US"));
        }
    
        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            Locale locale = (Locale) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
            if (locale == null) {
                locale = determineDefaultLocale(request);
            }
            return locale;
        }
    
        @Override
        protected Locale determineDefaultLocale(HttpServletRequest request) {
            Locale defaultLocale = request.getLocale();
            if (defaultLocale == null) {
                defaultLocale = getDefaultLocale();
            }
            return defaultLocale;
        }
    
    }
    

提交回复
热议问题