How do you enable LocaleInterceptor to change the locale in spring-security login page?

前端 未结 3 1158
慢半拍i
慢半拍i 2021-02-09 14:55

Pardon me if this question has been asked before, but I haven\'t gotten a straight answer that helped me solve my problem. I have a gwt application that I have secured using sp

3条回答
  •  攒了一身酷
    2021-02-09 15:17

    Thanks @Ritesh for give the answer. You give me a idea to handle this problem. I worked around it on a further way:

    public class InternationalizationFilter extends OncePerRequestFilter {
      private Logger log=Logger.getLogger(InternationalizationFilter.class);
      private String localeParam="lang";
      private LocaleResolver localeResolver;
    
      public InternationalizationFilter(String localeParam, LocaleResolver localeResolver) {
        this.localeParam = localeParam;
        this.localeResolver = localeResolver;
      }
    
      @Override
      public void destroy() {
        // TODO Auto-generated method stub
    
      }
    
      @Override
      protected void doFilterInternal(
          final HttpServletRequest request,
          final HttpServletResponse response,
          final FilterChain filterChain)
          throws ServletException, IOException {
        if (localeResolver == null) {
          throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
        }
        else{
          final String newLocale = request.getParameter(localeParam);
          if (newLocale != null) {
            final Locale locale = StringUtils.parseLocaleString(newLocale.toLowerCase());
            LocaleContextHolder.setLocale(locale);
            LocaleEditor localeEditor = new LocaleEditor();
            localeEditor.setAsText(newLocale);
            localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
            log.debug("change locale to "+
                locale.getLanguage()+"_"+locale.getCountry()+
                " at Thread"+Thread.currentThread().getId());
          }
          else{
            final Locale locale=localeResolver.resolveLocale(request);
            LocaleContextHolder.setLocale(locale);
            log.debug("restore locale to "+
                locale.getLanguage()+"_"+locale.getCountry()+
                " at Thread"+Thread.currentThread().getId());
          }
          try {
            filterChain.doFilter(request, response);
          } finally {
            LocaleContextHolder.resetLocaleContext();
          }
        }
      }
    
    }
    

    With declared the localChangeInterceptor and localeResolver in mvc config file. So we can change locale with request param "lang":

      
        
          
        
      
    
    
      
      
        
        
      
    

    In the security config file,I made a i18nFilter and added it to the Filter Chain:

      
        
          
          
          
          
          
          
          
          
          
          
    
        
       
    
    
    
    
        
        
      
    

    with localChangeInterceptor & localeResolver,I can change locale and save locale into session or cookie.

    under localeResolver's help,InternationalizationFilter could store/restore locale and make change to LocaleContextHolder's locale.

提交回复
热议问题