本文介绍了如何在Spring安全性登录页面中启用LocaleInterceptor来更改语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问我以前是否曾问过这个问题,但是我没有得到直接的答案来帮助我解决问题.我有一个已使用spring-security保护的gwt应用程序. Spring Security只是对用户进行身份验证,然后重定向到gwt应用程序.现在,我希望用户能够从登录页面上的链接更改语言环境,之后该语言环境将存储在Cookie中并在应用程序中使用.

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 spring-security. Spring security just authenticates the user and redirects to the gwt application. Now i want the user to be able to change the locale from a link on the login page after which the locale will be stored on a cookie and used in the app.

在我的applicationContext.xml中,我具有以下配置

I have the following configurations, in my applicationContext.xml

<http auto-config="true">
    <intercept-url pattern="/mywebapp/**" access="ROLE_USER"/>
    <intercept-url pattern="/gwt/**" access="ROLE_USER"/>
    <intercept-url pattern="/**/*.html" access="ROLE_USER"/>
    <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
    <form-login login-page="/login.jsp"/>
</http>
<beans:bean id="userDetailsService"
    class="com.kibet.mywebapp.server.auth.UserDetailsServiceImpl">
</beans:bean>

...
<!-- Application Message Bundle -->
<beans:bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basename" value="classpath:messages" />
    <beans:property name="defaultEncoding" value="UTF-8"/>
</beans:bean>

<beans:bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <beans:property name="paramName" value="lang" />
</beans:bean>

<beans:bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <beans:property name="defaultLocale" value="pt"/>
</beans:bean>

<beans:bean id="urlMapping"
     class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <beans:property name="interceptors">
    <beans:list>
        <beans:ref bean="localeChangeInterceptor"/>
    </beans:list>
    </beans:property>
    <beans:property name="mappings">
    <beans:value>/login.jsp=userDetailsService</beans:value>
    </beans:property>
</beans:bean>

这似乎不起作用.我的类路径中有语言环境属性文件messages_en.properties,messages_es.properties和messages_pt.properties.它唯一有效的时间是当我更改浏览器的默认语言环境时. 据我所知,登录页面是由spring-security生成的,并且处理程序映射无法拦截语言环境更改请求.如果这是原因,我将如何处理?非常感谢您的帮助.

This doesn't seem to work. I have the locale properties files messages_en.properties, messages_es.properties and messages_pt.properties in my classpath. The only time it works is when I change the browsers default locale. As far as I can tell, the login page is generated by spring-security and the handler mapping cannot intercept the locale change request. If this is the reason how would I go about this? Help is highly appreciated.

这是我的自定义过滤器代码.

Here's my Custom filter code.

public class InternationalizationFilter implements Filter {

@Override
public void destroy() {
    // TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain filterChain) throws IOException, ServletException {
    String newLocale = request.getParameter("lang");
    if (newLocale != null) {
        Locale locale = StringUtils.parseLocaleString(newLocale
                .toLowerCase());
        LocaleContextHolder.setLocale(locale);
    }
    try {
        filterChain.doFilter(request, response);
    } finally {

        LocaleContextHolder.resetLocaleContext();
    }
}

@Override
public void init(FilterConfig arg0) throws ServletException {

}}

推荐答案

LocaleChangeInterceptor是Spring MVC的一部分,这意味着它们不会出现在Spring安全过滤器中.您将必须在过滤器链中自行设置语言环境.另请参见 Spring Security/SEC-1527:国际化示例应用程序之一

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

这篇关于如何在Spring安全性登录页面中启用LocaleInterceptor来更改语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 19:59