2012-04-24 49 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