Skip to content

Commit 4020099

Browse files
committed
/account/auth: fix improperly localized error message after unsuccessful authentication attempt with non-Russian locale.
Fix #243
1 parent b48bd5b commit 4020099

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@
1717
*/
1818
package ru.mystamps.web.support.spring.security;
1919

20+
import java.util.Collections;
21+
22+
import javax.servlet.Filter;
23+
2024
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.beans.factory.annotation.Qualifier;
2126
import org.springframework.context.ApplicationListener;
2227
import org.springframework.context.MessageSource;
2328
import org.springframework.context.annotation.Bean;
2429
import org.springframework.http.HttpMethod;
2530

31+
import org.springframework.boot.web.filter.OrderedRequestContextFilter;
32+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
33+
2634
import org.springframework.security.authentication.AuthenticationProvider;
2735
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
2836
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
@@ -140,6 +148,33 @@ public AuthenticationProvider getAuthenticationProvider() {
140148
return provider;
141149
}
142150

151+
// By default RequestContextFilter is created. Override it with its ordered version.
152+
// Note that name is important here
153+
@Bean(name = "requestContextFilter")
154+
public Filter getOrderedRequestContextFilter() {
155+
return new OrderedRequestContextFilter();
156+
}
157+
158+
// Bean name will be shown in logs
159+
@Bean(name = "resetLocaleFilter")
160+
public FilterRegistrationBean getResetLocaleFilter(
161+
@Qualifier("requestContextFilter") Filter filter) {
162+
163+
FilterRegistrationBean bean = new FilterRegistrationBean(
164+
new SessionLocaleResolverAwareFilter()
165+
);
166+
167+
// SessionLocaleResolverAwareFilter should be invoked after RequestContextFilter
168+
// to overwrite locale in LocaleContextHolder
169+
OrderedRequestContextFilter requestContextFilter = (OrderedRequestContextFilter)filter;
170+
bean.setOrder(requestContextFilter.getOrder() + 1);
171+
172+
// url pattern should match HttpSecurity.formLogin().loginProcessingUrl()
173+
bean.setUrlPatterns(Collections.singletonList(Url.LOGIN_PAGE));
174+
175+
return bean;
176+
}
177+
143178
private UserDetailsService getUserDetailsService() {
144179
return new CustomUserDetailsService(servicesConfig.getUserService());
145180
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.support.spring.security;
19+
20+
import java.io.IOException;
21+
import java.util.Locale;
22+
23+
import javax.servlet.Filter;
24+
import javax.servlet.FilterChain;
25+
import javax.servlet.FilterConfig;
26+
import javax.servlet.ServletException;
27+
import javax.servlet.ServletRequest;
28+
import javax.servlet.ServletResponse;
29+
import javax.servlet.http.HttpServletRequest;
30+
31+
import org.slf4j.Logger;
32+
import org.slf4j.LoggerFactory;
33+
34+
import org.springframework.context.i18n.LocaleContextHolder;
35+
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
36+
import org.springframework.web.util.WebUtils;
37+
38+
/**
39+
* Filter sets locale by looking up in the session or uses English as a fallback.
40+
*
41+
* Unfortunately RequestContextFilter doesn't respect locale that may be set by
42+
* SessionLocaleResolver. This leads to improperly localized error messages from Spring Security.
43+
* This filter fixes such behavior: it looks up locale in the session first or uses default
44+
* locale (English). To be able to overwrite locale that was set by RequestContextFilter,
45+
* filter should be invoked after it.
46+
*
47+
* @author Slava Semushin
48+
*/
49+
class SessionLocaleResolverAwareFilter implements Filter {
50+
private static final Logger LOG =
51+
LoggerFactory.getLogger(SessionLocaleResolverAwareFilter.class);
52+
53+
@Override
54+
public void init(FilterConfig filterConfig) throws ServletException {
55+
// Intentionally empty: nothing to initialize
56+
}
57+
58+
@Override
59+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
60+
throws IOException, ServletException {
61+
62+
try {
63+
HttpServletRequest req = (HttpServletRequest)request;
64+
LOG.debug("Handling request {} {}", req.getMethod(), req.getRequestURI());
65+
66+
Locale locale = (Locale)WebUtils.getSessionAttribute(
67+
(HttpServletRequest)request,
68+
SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME
69+
);
70+
71+
if (locale == null) {
72+
locale = Locale.ENGLISH;
73+
LOG.debug("Locale reset to 'en' (default)");
74+
} else {
75+
LOG.debug("Locale reset to '{}' (from session)", locale);
76+
}
77+
78+
LocaleContextHolder.setLocale(locale);
79+
80+
} catch (RuntimeException ex) { // NOPMD: AvoidCatchingGenericException
81+
LOG.warn("Couldn't handle request: {}", ex);
82+
83+
} finally {
84+
chain.doFilter(request, response);
85+
}
86+
}
87+
88+
@Override
89+
public void destroy() {
90+
// Intentionally empty: nothing to do
91+
}
92+
93+
}

0 commit comments

Comments
 (0)