2012-04-24 88 views
3

我使用Spring Security 3.1.0和Spring MVC 3.1.1。 我希望能够基于URL即更改地区:LocaleResolver in filter null yet shows it has autowired! Spring MVC 3.1.1/Spring Security 3.1.0

http://localhost:8080/abc?lang=fr 

现在,这个工作“正常”的情况下,即从一个页面到另一个页面然而,在我的应用程序,如果我去从一个不安全的页面到一个安全的页面,它首先碰到我的登录页面,这是Spring Security在打开我想要的页面之前提供的。

这是正常的春季安全特性(拦截安全资源),所以有这个behavuour没有问题。

问题是,当我抵达受保护的页面时,语言环境不会改变!它保持为默认语言环境。我.. lang = fr没有解析出来。

我已经打了定义语言环境相关的豆类调度-servlet.xml的内部和外部,在应用程序上下文文件执行以下操作:

<mvc:interceptors> 
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang" /> 
</mvc:interceptors> 

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="en" /> 

我也试图分裂上述2在应用上下文配置中只有localResolver

我做这个研究吨,基本上,我明白我需要手动更改语言环境。

即使春节文档为春季安全3.1.0说,你需要自己的“过滤器”,或者您可以使用RequestContextFilter两个。但RequestContextFilter不会解析查询字符串中的区域设置参数。

Spring Security relies on Spring's localization support in order to actually lookup 
the appropriate message. In order for this to work, you have to make sure that the 
locale from the incoming request is stored in Spring's 
org.springframework.context.i18n.LocaleContextHolder. Spring MVC's DispatcherServlet 
does this for your application automatically, but since Spring Security's filters are 
invoked before this, the LocaleContextHolder needs to be set up to contain the correct 
Locale before the filters are called. You can either do this in a filter yourself 
(which must come before the Spring Security filters in web.xml) or you can use 
Spring's RequestContextFilter. 

我想截取请求之前,它击中控制器,所以,我写了我自己的过滤器。

我的解决方案的基础上,别人怎么做,以及,是自动装配的LocaleResolver中。当tomcat启动时,它会在我的日志中显示“localeResolver”已被自动装配(否则应用程序将在那里失败),但是在运行时localeResolver为NULL。

此外,已经有职位,说在应用程序上下文中定义的LocaleResolver ......我已经这样做了,但我还是最终得到一个空的LocaleResolver当一个请求发生。

任何想法??非常感激。

p.s.我定义的过滤器在Spring Security过滤器之前。我可以调试它,它首先点击它,但是因为LocaleResolver上的NPE而死。

欣赏这一点,谢谢。

+0

你看过这个答案:http://stackoverflow.com/questions/8026320/spring-security-localeresolver – Gren 2012-04-24 15:48:33

回答

4

您是否在web.xml中定义了过滤器?如果是这样,那么过滤器类不会被Spring实例化,它会被servlet容器实例化。 Spring不能自动装载它不知道的东西。

这里的一般解决方法是申报网站的<filter-class>。XML作为org.springframework.web.filter.DelegatingFilterProxy和点targetBeanName在您的上下文中的豆,如:

<filter> 
    <filter-name>My Locale Filter</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    <init-param> 
     <param-name>targetBeanName</param-name> 
     <param-value>myLocaleFilter</param-value> 
    </init-param> 
</filter> 

而在你的Spring上下文,<bean id="myLocaleFilter">应该在你的过滤器类别点。

您可能还会发现方便您的自定义过滤器类扩展GenericFilterBean,而不是直接实现Filter接口。

+0

它的工作!然而,我回去了,并再次直接打电话给我的豆子,奇怪的是这次它也工作得很好。我认为这与扫描有关。我在我的过滤器定义的上下文中使用了(我使用通过maven管理的多个上下文)。我从GenericFilterBean扩展。谢谢。 – logixplayer 2012-04-25 16:20:06

+0

尽管我很想知道你在说什么:“你是否在web.xml中定义了过滤器?如果是这样,那么过滤器类不是被Spring实例化的,它是由servlet容器实例化的,Spring不能自动调用它的功能不知道。“我确实用了:DelegatingFilterProxy,所以我认为这是Spring知道它的方式。我可以看到其余的接线可以通过多种方式完成。 – logixplayer 2012-04-25 16:24:46

+0

是的 - 如果您使用的是DelegatingFilterProxy,则不适用我的解决方案。但是,如果你之前没有注释配置,那么我认为它不会默认自动装配类。 – 2012-04-25 17:50:41