Browse Source

Un-hardcode authentication failure message: use message from localized

messages.properties file
Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.3-alpha
Pekka Helenius 4 years ago
parent
commit
c38f0161b8
2 changed files with 36 additions and 4 deletions
  1. +5
    -1
      bookstore/src/main/java/com/fjordtek/bookstore/config/WebSecurityConfig.java
  2. +31
    -3
      bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationFailureHandler.java

+ 5
- 1
bookstore/src/main/java/com/fjordtek/bookstore/config/WebSecurityConfig.java View File

@ -3,6 +3,7 @@
package com.fjordtek.bookstore.config; package com.fjordtek.bookstore.config;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -43,6 +44,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired @Autowired
private Environment env; private Environment env;
@Autowired
private MessageSource msg;
@Autowired @Autowired
private UserDetailServiceImpl userDetailService; private UserDetailServiceImpl userDetailService;
@ -123,7 +127,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.usernameParameter(env.getProperty("auth.field.username")) .usernameParameter(env.getProperty("auth.field.username"))
.passwordParameter(env.getProperty("auth.field.password")) .passwordParameter(env.getProperty("auth.field.password"))
.successHandler(new BookStoreAuthenticationSuccessHandler()) .successHandler(new BookStoreAuthenticationSuccessHandler())
.failureHandler(new BookStoreAuthenticationFailureHandler())
.failureHandler(new BookStoreAuthenticationFailureHandler(env, msg))
.loginProcessingUrl(env.getProperty("page.url.login")) .loginProcessingUrl(env.getProperty("page.url.login"))
.loginPage(env.getProperty("page.url.list")) .loginPage(env.getProperty("page.url.list"))
.defaultSuccessUrl(env.getProperty("page.url.list")) .defaultSuccessUrl(env.getProperty("page.url.list"))


+ 31
- 3
bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationFailureHandler.java View File

@ -8,6 +8,8 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 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.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler; import org.springframework.security.web.authentication.AuthenticationFailureHandler;
@ -29,8 +31,12 @@ import com.fjordtek.bookstore.service.HttpServerLogger;
public class BookStoreAuthenticationFailureHandler implements AuthenticationFailureHandler { public class BookStoreAuthenticationFailureHandler implements AuthenticationFailureHandler {
private Environment env;
private MessageSource msg;
private HttpServerLogger httpServerLogger = new HttpServerLogger(); private HttpServerLogger httpServerLogger = new HttpServerLogger();
@Override @Override
public void onAuthenticationFailure( public void onAuthenticationFailure(
HttpServletRequest requestData, HttpServletRequest requestData,
@ -42,14 +48,36 @@ public class BookStoreAuthenticationFailureHandler implements AuthenticationFail
httpServerLogger.log(requestData, responseData); httpServerLogger.log(requestData, responseData);
requestData requestData
.setAttribute("username", requestData.getParameter("b_username"));
.setAttribute("username", requestData.getParameter(
env.getProperty("auth.field.username")
));
requestData 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); .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;
}
} }

Loading…
Cancel
Save