Web/스프링

[스프링 시큐리티, 다국어] 시큐리티 적용시 다국어 세션, locale 유지

벨포트조던 2017. 11. 27.
반응형

https://hfontis.wordpress.com/2015/12/16/i18n-with-spring-security/


시큐리티 사용시 locale인터셉터를 타지않고 바로 시큐리티로 넘어가서 locale 세션이 유지가 안된다.


해결방안은 위 주소에 자세히 나와있고, 필터를 사용한다.





I18n with spring security

The Problem: localization works on regular JSP pages, but not when using the spring security.

Money quote from the documentation:

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. Please refer to the Spring Framework documentation for further details on using localization with Spring.

http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#localization

To understand this, first understand the basic structure of spring webapp configuration files: https://hfontis.wordpress.com/2015/12/16/spring-mvc-configuration-files/

So: we need to set the locale manually, before invoking the security.

This post assumes you already have setup the basic i18n support for your application, so the only thing you need is to add a custom filter.

Implement this Filter: http://stackoverflow.com/a/26531758

The filter assumes, that you have the locale resolver available from the applicationContext, so be sure to have something like this in the applicationContext:

1
2
3
4
5
6
7
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource"
p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application" p:fallbackToSystemLocale="false">
<property name="defaultEncoding" value="UTF-8" />
</bean>
     
<!-- store preferred language configuration in a cookie -->
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale" />

Add the following lines to your web.xml BEFORE invoking the security filter chain:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<filter>
    <filter-name>FilterI18nSpringSecurity</filter-name>
    <filter-class>com.medisanaspace.web.library.FilterI18nSpringSecurity</filter-class>
</filter>
<filter-mapping>
    <filter-name>FilterI18nSpringSecurity</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
 
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
Advertisements


반응형

댓글