From c38f0161b8b202a3a4377556bbb8f41c62f68247 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Tue, 6 Oct 2020 12:33:42 +0300 Subject: [PATCH] Un-hardcode authentication failure message: use message from localized messages.properties file Signed-off-by: Pekka Helenius --- .../bookstore/config/WebSecurityConfig.java | 6 +++- ...BookStoreAuthenticationFailureHandler.java | 34 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/config/WebSecurityConfig.java b/bookstore/src/main/java/com/fjordtek/bookstore/config/WebSecurityConfig.java index fc1c0af..597e6fa 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/config/WebSecurityConfig.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/config/WebSecurityConfig.java @@ -3,6 +3,7 @@ package com.fjordtek.bookstore.config; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.MessageSource; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.core.env.Environment; @@ -43,6 +44,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private Environment env; + @Autowired + private MessageSource msg; + @Autowired private UserDetailServiceImpl userDetailService; @@ -123,7 +127,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { .usernameParameter(env.getProperty("auth.field.username")) .passwordParameter(env.getProperty("auth.field.password")) .successHandler(new BookStoreAuthenticationSuccessHandler()) - .failureHandler(new BookStoreAuthenticationFailureHandler()) + .failureHandler(new BookStoreAuthenticationFailureHandler(env, msg)) .loginProcessingUrl(env.getProperty("page.url.login")) .loginPage(env.getProperty("page.url.list")) .defaultSuccessUrl(env.getProperty("page.url.list")) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationFailureHandler.java b/bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationFailureHandler.java index 28f6020..0856855 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationFailureHandler.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationFailureHandler.java @@ -8,6 +8,8 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.springframework.context.MessageSource; +import org.springframework.core.env.Environment; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.authentication.AuthenticationFailureHandler; @@ -29,8 +31,12 @@ import com.fjordtek.bookstore.service.HttpServerLogger; public class BookStoreAuthenticationFailureHandler implements AuthenticationFailureHandler { + private Environment env; + private MessageSource msg; + private HttpServerLogger httpServerLogger = new HttpServerLogger(); + @Override public void onAuthenticationFailure( HttpServletRequest requestData, @@ -42,14 +48,36 @@ public class BookStoreAuthenticationFailureHandler implements AuthenticationFail httpServerLogger.log(requestData, responseData); requestData - .setAttribute("username", requestData.getParameter("b_username")); + .setAttribute("username", requestData.getParameter( + env.getProperty("auth.field.username") + )); requestData - .setAttribute("authfailure", "Authentication failure!"); + .setAttribute("authfailure", msg.getMessage( + "page.auth.failure", + null, + "page.auth.failure [placeholder]", + requestData.getLocale() + )); - requestData.getRequestDispatcher("/autherror") + requestData.getRequestDispatcher(env.getProperty("page.url.autherror")) .forward(requestData, responseData); } + //////////////////// + // Class constructors + + public BookStoreAuthenticationFailureHandler() { + } + + /* + * Autowired annotation does not work. + * Therefore, pass Environment & MessageSource as parameters to this class constructor. + */ + public BookStoreAuthenticationFailureHandler(Environment env, MessageSource msg) { + this.env = env; + this.msg = msg; + } + }