Skip to content

Commit 114b1ed

Browse files
author
Eugen
committed
Merge pull request eugenp#307 from Doha2012/master
fix error message
2 parents 50cd700 + bbcacc0 commit 114b1ed

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.baeldung.security;
2+
3+
import java.io.IOException;
4+
import java.util.Locale;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.context.MessageSource;
12+
import org.springframework.security.core.AuthenticationException;
13+
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
14+
import org.springframework.stereotype.Component;
15+
import org.springframework.web.servlet.LocaleResolver;
16+
17+
@Component
18+
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
19+
20+
@Autowired
21+
private MessageSource messages;
22+
23+
@Autowired
24+
private LocaleResolver localeResolver;
25+
26+
@Override
27+
public void onAuthenticationFailure(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException exception) throws IOException, ServletException {
28+
setDefaultFailureUrl("/login.html?error=true");
29+
30+
super.onAuthenticationFailure(request, response, exception);
31+
32+
final Locale locale = localeResolver.resolveLocale(request);
33+
34+
if (exception.getMessage().equalsIgnoreCase("User is disabled")) {
35+
request.getSession().setAttribute("SPRING_SECURITY_LAST_EXCEPTION", messages.getMessage("auth.message.disabled", null, locale));
36+
} else if (exception.getMessage().equalsIgnoreCase("User account has expired")) {
37+
request.getSession().setAttribute("SPRING_SECURITY_LAST_EXCEPTION", messages.getMessage("auth.message.expired", null, locale));
38+
} else if (exception.getMessage().equalsIgnoreCase("blocked")) {
39+
request.getSession().setAttribute("SPRING_SECURITY_LAST_EXCEPTION", messages.getMessage("auth.message.blocked", null, locale));
40+
} else {
41+
request.getSession().setAttribute("SPRING_SECURITY_LAST_EXCEPTION", messages.getMessage("message.badCredentials", null, locale));
42+
}
43+
}
44+
}

spring-security-login-and-registration/src/main/java/org/baeldung/spring/SecSecurityConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.security.core.userdetails.UserDetailsService;
1414
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1515
import org.springframework.security.crypto.password.PasswordEncoder;
16+
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
1617
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
1718

1819
@Configuration
@@ -26,6 +27,9 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
2627
@Autowired
2728
private AuthenticationSuccessHandler myAuthenticationSuccessHandler;
2829

30+
@Autowired
31+
private AuthenticationFailureHandler authenticationFailureHandler;
32+
2933
public SecSecurityConfig() {
3034
super();
3135
}
@@ -59,6 +63,7 @@ protected void configure(final HttpSecurity http) throws Exception {
5963
.defaultSuccessUrl("/homepage.html")
6064
.failureUrl("/login.html?error=true")
6165
.successHandler(myAuthenticationSuccessHandler)
66+
.failureHandler(authenticationFailureHandler)
6267
.usernameParameter("j_username")
6368
.passwordParameter("j_password")
6469
.permitAll()

spring-security-login-and-registration/src/main/webapp/WEB-INF/view/login.jsp

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,7 @@
77
<%@ page session="true"%>
88
<fmt:message key="message.password" var="noPass" />
99
<fmt:message key="message.username" var="noUser" />
10-
<c:if test="${param.error != null}">
11-
<c:choose>
12-
<c:when
13-
test="${SPRING_SECURITY_LAST_EXCEPTION.message == 'User is disabled'}">
14-
<div class="alert alert-danger">
15-
<spring:message code="auth.message.disabled"></spring:message>
16-
</div>
17-
</c:when>
18-
<c:when
19-
test="${SPRING_SECURITY_LAST_EXCEPTION.message == 'User account has expired'}">
20-
<div class="alert alert-danger">
21-
<spring:message code="auth.message.expired"></spring:message>
22-
</div>
23-
</c:when>
24-
<c:when
25-
test="${SPRING_SECURITY_LAST_EXCEPTION.message == 'blocked'}">
26-
<div class="alert alert-danger">
27-
<spring:message code="auth.message.blocked"></spring:message>
28-
</div>
29-
</c:when>
30-
<c:otherwise>
31-
<div class="alert alert-danger">
32-
<!-- <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/> -->
33-
<spring:message code="message.badCredentials"></spring:message>
34-
</div>
35-
</c:otherwise>
36-
</c:choose>
37-
</c:if>
10+
3811
<html>
3912

4013
<head>
@@ -72,6 +45,13 @@ ${param.message}
7245
</div>
7346
</c:if>
7447

48+
49+
<c:if test="${param.error != null}">
50+
<div class="alert alert-danger">
51+
${SPRING_SECURITY_LAST_EXCEPTION}
52+
</div>
53+
</c:if>
54+
7555
<div class="container">
7656
<div class="row wrapper">
7757
<h1>

0 commit comments

Comments
 (0)