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

前端 未结 3 1159
慢半拍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:06

    First the key is the LocaleResolver and forget the interceptor.

    Just create a bean that implement this interface to resolve the locale from where you save it, as example from an attribute of the request.

    public class LocaleResolverImpl implements LocaleResolver {
    
        public LocaleResolverImpl() {
        }
    
        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            Locale r = (Locale) request.getAttribute("localeObject");
            return r == null ? request.getLocale() : r;
        }
    
        @Override
        public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    
    0 讨论(0)
  • 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":

      <mvc:interceptors>
        <bean id="localChangeInterceptor"
              class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
          <property name="paramName" value="lang"/>
        </bean>
      </mvc:interceptors>
    
    
      <!--localeResolver-->
      <bean id="localeResolver"
            class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <!--<property name="cookieDomain" value=""/>-->
        <property name="defaultLocale" value="zh"/>
      </bean>
    

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

      <bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
        <security:filter-chain-map path-type="ant">
          <security:filter-chain pattern="/**/*.css" filters="
              securityContextPersistenceFilter,
              exceptionTranslationFilter"/>
          <security:filter-chain pattern="/**/*.jpg" filters="
              securityContextPersistenceFilter,
              exceptionTranslationFilter"/>
          <security:filter-chain pattern="/**/*.png" filters="
              securityContextPersistenceFilter,
              exceptionTranslationFilter"/>
          <security:filter-chain pattern="/**/*.gif" filters="
              securityContextPersistenceFilter,
              exceptionTranslationFilter"/>
          <security:filter-chain pattern="/**/*.js" filters="
              securityContextPersistenceFilter,
              exceptionTranslationFilter"/>
          <security:filter-chain pattern="/**/*.cur" filters="
              securityContextPersistenceFilter,
              exceptionTranslationFilter"/>
          <security:filter-chain pattern="/**/*.swf" filters="
              securityContextPersistenceFilter,
              exceptionTranslationFilter"/>
          <security:filter-chain pattern="/login" filters="
              i18nFilter,
              securityContextPersistenceFilter,
              exceptionTranslationFilter"/>
          <security:filter-chain pattern="/checkin" filters="
              i18nFilter,
              securityContextPersistenceFilter,
              authenticationFilter"/>
          <security:filter-chain pattern="/**" filters="
               i18nFilter,
               securityContextPersistenceFilter,
               authenticationFilter,
               logoutFilter,
               anonymousAuthenticationFilter,
               exceptionTranslationFilter,
               filterSecurityInterceptor" />
    
        </security:filter-chain-map>
      </bean> 
    
    
    
    <bean id="i18nFilter" class="com.bjinfotech.filter.InternationalizationFilter">
        <constructor-arg name="localeParam" value="lang"/>
        <constructor-arg name="localeResolver" ref="localeResolver"/>
      </bean>
    

    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.

    0 讨论(0)
  • 2021-02-09 15:21

    The LocaleChangeInterceptor is a part of Spring MVC and that means they don't come in picture in spring security filters. You will have to set locale yourself within the filter chain. Please also see Spring Security/SEC-1527: Internationalize one of the sample applications

    0 讨论(0)
提交回复
热议问题