Announcement Announcement Module
Collapse
No announcement yet.
Getting LocaleResolver in the authentication-success-handler-ref Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Getting LocaleResolver in the authentication-success-handler-ref

    I'm trying to implement the functionality that the user saves the default language on the database and on login the locale is set with that data.
    I create a custom Authentication success handler in my security-context.xml
    Code:
    <http use-expressions="true">
            <intercept-url pattern="/" access="permitAll"/>
            <intercept-url pattern="/login*" access="permitAll"/>
            <intercept-url pattern="/resources/**" access="permitAll" />
            <intercept-url pattern="/**" access="isFullyAuthenticated()"/>
            <form-login
                login-page="/login"
                default-target-url="/dashboard.html"
                authentication-failure-url="/login?login_error=1"
                authentication-success-handler-ref="authenticationSuccessHandler" />
            <logout logout-success-url="/"/>
            <anonymous />
            <csrf />
        </http>
        <beans:bean id="authenticationSuccessHandler" class="web.security.CustomLoginSuccessHandler"/>
    I handle the Locale Resolvers in my servlet-context.xml
    Code:
    <interceptors>
            <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang" />
            <beans:bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
    </interceptors>
    <beans:bean class="org.springframework.ui.context.support.ResourceBundleThemeSource" id="themeSource" />
    <beans:bean class="org.springframework.web.servlet.theme.CookieThemeResolver" id="themeResolver" p:cookieName="theme" p:defaultThemeName="standard" />
    <!-- store preferred language configuration in a cookie -->
    <beans:bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale" />
    And my class is this:
    Code:
    @Configurable
    public class CustomLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    
        private static final Logger logger = LoggerFactory.getLogger(CustomLoginSuccessHandler.class);
        @Autowired
        private LocaleResolver localeResolver;
        
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
            setLocale(authentication, request, response);
            super.onAuthenticationSuccess(request, response, authentication);
            logger.debug("Authentication success");
        }
    
        protected void setLocale(Authentication authentication, HttpServletRequest request, HttpServletResponse response){
            if(authentication != null){
                MtsUserDetail principal = (MtsUserDetail) authentication.getPrincipal();
                if(principal.getUser().getLanguage() != null && !principal.getUser().getLanguage().isEmpty()){
                    Locale locale = new Locale(principal.getUser().getLanguage(), principal.getUser().getCountry());
                    localeResolver.setLocale(request, response, locale);
                }
                
            }
        }
    }
    My problem is that localeResolver is always NULL. I can't find why is not autowiring. I found this in stackoverflow: http://stackoverflow.com/questions/8...localeresolver
    LocaleResolver is exposed in request context by DispatcherServlet, whereas AuthenticationSuccessHandler is fired before request enters DispatcherServlet (actually, request that fired SavedRequestAwareAuthenticationSuccessHandler never enters DispatcherServlet, because this handler performs a redirect).

    Thus, you cannot access LocaleResolver via RequestContextUtils in this case. You can try to inject LocaleResolver into your AuthenticationSuccessHandler explicitly, for example, with autowiring.
    And I have the same error.

    Any help will be appreciated.
    Regards
Working...
X