Spring Security + LocaleResolver

成功进行身份验证后,我需要更改语言环境设置。

LocaleResolver:

    <bean id="localeChangeInterceptor"

class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">

<property name="paramName" value="lng" />

</bean>

<bean id="localeResolver"

class="web.MyLocaleResolver">

</bean>


   public class MyLocaleResolver extends AbstractLocaleResolver {

private Locale default = Locale.ENGLISH;

@Override

public Locale resolveLocale(HttpServletRequest hsr) {

return this.default;

}

@Override

public void setLocale(HttpServletRequest hsr, HttpServletResponse hsr1, Locale default) {

this.default = default;

}

}

安全:

     <form-login login-page="/login" 

authentication-success-handler- ref="MySuccessAuthHandler"/>

<beans:bean id="MySuccessAuthHandler" class="web.MySuccessfulAuthenticationHandler">

<beans:property name="defaultTargetUrl" value="/index.htm"></beans:property>

</beans:bean>


public class MySuccessfulAuthenticationHandler extends  SavedRequestAwareAuthenticationSuccessHandler  {

@Override

public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,

Authentication authentication) throws ServletException, IOException {

super.onAuthenticationSuccess(request, response, authentication);

RequestContextUtils.getLocaleResolver(request).setLocale(request, response, Locale.ENGLISH);

}

}

当我尝试通过设置语言环境时,出现RequestContextUtilsNullPointer异常。

回答:

LocaleResolver在中被暴露在请求上下文中DispatcherServlet,而AuthenticationSuccessHandler在请求进入之前被触发DispatcherServlet(实际上,被触发的请求SavedRequestAwareAuthenticationSuccessHandler从不进入DispatcherServlet,因为此处理程序执行重定向)。

因此,在这种情况下,您将无法LocaleResolver通过RequestContextUtils进行访问。您可以尝试使用自动装配等方式明确地LocaleResolver插入您的设备AuthenticationSuccessHandler

以上是 Spring Security + LocaleResolver 的全部内容, 来源链接: utcz.com/qa/399878.html

回到顶部