身份验证成功后,我需要更改区域设置。
语言环境解析器:
<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 异常。 最佳答案
LocaleResolver
由 DispatcherServlet
在请求上下文中公开,而 AuthenticationSuccessHandler
在请求进入 DispatcherServlet
之前被触发(实际上,触发 SavedRequestAwareAuthenticationSuccessHandler
的请求永远不会进入 DispatcherServlet
,因为该处理程序执行重定向)。
因此,在这种情况下,您无法通过 LocaleResolver
访问 RequestContextUtils
。您可以尝试将 LocaleResolver
显式注入(inject) AuthenticationSuccessHandler
,例如,使用 Autowiring 。
关于java - Spring 安全性 + LocaleResolver,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8026320/