问题描述
验证成功后,我需要更改语言环境设置.
i need to change locale settings after successful Authentication.
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);
}
}
当我尝试通过RequestContextUtils
设置区域设置时,出现NullPointer异常.
When i try to set locale by RequestContextUtils
i get NullPointer Exception.
推荐答案
LocaleResolver
在DispatcherServlet
的请求上下文中公开,而AuthenticationSuccessHandler
在请求进入DispatcherServlet
之前被触发(实际上,触发了永远不会输入DispatcherServlet
,因为此处理程序会执行重定向).
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).
因此,在这种情况下,您无法通过RequestContextUtils
访问LocaleResolver
.您可以尝试例如通过自动装配将LocaleResolver
显式注入到AuthenticationSuccessHandler
中.
Thus, you cannot access LocaleResolver
via RequestContextUtils
in this case. You can try to inject LocaleResolver
into your AuthenticationSuccessHandler
explicitly, for example, with autowiring.
这篇关于Spring Security + LocaleResolver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!