我的第一個問題在這裏,我會盡量具體。我對Spring很陌生,我試圖創建一個相當簡單的預訂系統(但這實際上並不重要)。重要的是,我正在創建一些基本模板,然後由真正的網頁填寫。應用程序在hibernate,mysql上工作,我還設置了i18n和spring安全。問題是我無法更改我的語言環境。唯一可行的是改變默認的一個。 首先我瀏覽Web A LOT,並且發現使用i18n和spring安全性通常比較複雜。我發現了什麼是我需要有額外的過濾器:Spring security + i18n =如何讓它一起工作?
<filter>
<filter-name>localizationFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>localizationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我發現了什麼是這個過濾器的一個安全但它不解析的形式請求之前的確處理:http://someserver.com/bla/home?locale=en
。我調試了它,它似乎不是爲此目的而創建的(而這正是我需要的)。 這是從春季樣本「聯繫人」中提取的,但在本例中,我找不到任何實際上以更改語言爲目標的代碼。效果是,它根本不起作用。它總是試圖將區域設置更改爲我的默認設置。好消息是,如果在調試模式下,我手動將locale-to-set更改爲其他一個工作正常,所以我在我的心中感受到了希望...... ;-)
然後我找到了其他一些方法 - 通過創建我們自己的過濾器。我所做的就是將創建的示例(不要記住作者)與創建方法RequestContextFilter
合併在一起。畢竟RequestContextFilter
工作正常 - 只要最好解析我的請求。這是新過濾器的代碼:
public class InternationalizationFilter extends OncePerRequestFilter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
protected void doFilterInternal(final HttpServletRequest request,
final HttpServletResponse response, final FilterChain filterChain)
throws ServletException, IOException {
final String newLocale = request.getParameter("locale");
if (newLocale != null) {
final Locale locale = StringUtils.parseLocaleString(newLocale
.toLowerCase());
LocaleContextHolder.setLocale(locale);
}
try {
filterChain.doFilter(request, response);
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
}
正如您所看到的,請求參數區域設置被解析並設置了區域設置。有兩個問題: 1.發送請求xxxxx?locale=en
後,它創建沒有「國家」屬性的語言環境(僅設置語言)。說實話,我不知道這是否有問題 - 也許不是。 2.更嚴重的問題是,它不起作用...我的意思是它在過濾器鏈中的正確位置(在安全之前),它產生正確的語言環境並將其設置爲像RequestContextFilter
一樣的方式。 ..但它根本無法工作。
我將非常高興,如果有人可以讓我知道如何讓我給出的例子或任何其他帶有彈簧安全築底國際化的工作...
謝謝!
其他信息: 我做了一些實驗,看起來來自請求的Locale實例是某種特定的。
看看這段代碼(修改RequestContextFilter
類):
@Override
protected void doFilterInternal(final HttpServletRequest request,
final HttpServletResponse response, final FilterChain filterChain)
throws ServletException, IOException {
final ServletRequestAttributes attributes = new ServletRequestAttributes(
request);
final Locale l = Locale.GERMAN;
final Locale l2 = request.getLocale();
LocaleContextHolder.setLocale(l,
this.threadContextInheritable);
RequestContextHolder.setRequestAttributes(attributes,
this.threadContextInheritable);
if (logger.isDebugEnabled()) {
logger.debug("Bound request context to thread: " + request);
}
(...)
如果這種方法:LocaleContextHolder.setLocale(l, this.threadContextInheritable);
我通過語言環境「L」它不能在所有的工作。我的意思是即使你明確改變了語言環境也不會改變。 另一方面,如果我通過那裏修改爲德語的locale'l2'(在調試模式下),它工作正常!
這意味着,由於某種原因,從request.getLocale()
的Locale實例莫名其妙的青睞,或許真的是後來事情的代碼,我不知道/ understant ...
請讓我知道應該怎麼我將這個i18n與安全原因一起使用,我必須承認我不知道發生了什麼......
- ==== - ====== - ====== - ====== - ====
最終解決方案/ ANSWER(但仍與小問題) 感謝拉爾夫我設法解決我的問題。以前我走錯了方向,但roo生成的項目推動了我前進。 看來,我不斷添加在一個錯誤的/不準確的方法攔截器(以前的代碼):
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="pl"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
這樣的攔截器從未援引出於某種原因。
改變攔截器高清後:
<mvc:interceptors>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
</bean>
</mvc:interceptors>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="pl"/>
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
...它啓動時沒有任何其他更改安全/ web.xml中正常工作。
現在問題沒有了,但我不知道發生了什麼。根據我在第二個例子(工作的那個)中的理解,我製作了攔截器「全局」。但是爲什麼在第一個例子中定義的攔截器不起作用?任何提示?
再次感謝您的幫助! N.
嗨,我已經嘗試過你的配置攔截器,但它不工作。我的語言環境在登錄頁面上沒有變化,但成功登錄後它的工作正常。在登錄頁面上,它只會觸發系統區域設置。 –