我使用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而死。
欣賞這一點,謝謝。
你看過這個答案:http://stackoverflow.com/questions/8026320/spring-security-localeresolver – Gren 2012-04-24 15:48:33