Browse Source

Implement Bookstore Authentication Handler classes

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.3-alpha
Pekka Helenius 4 years ago
parent
commit
0a572c73a2
2 changed files with 100 additions and 0 deletions
  1. +55
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationFailureHandler.java
  2. +45
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationSuccessHandler.java

+ 55
- 0
bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationFailureHandler.java View File

@ -0,0 +1,55 @@
//Pekka Helenius <fincer89@hotmail.com>, Fjordtek 2020
package com.fjordtek.bookstore.service.session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import com.fjordtek.bookstore.service.HttpServerLogger;
/**
*
* This class implements Spring Framework security AuthenticationFailureHandler
* interface with specific method overrides.
* <p>
* Main purpose is to properly handle invalid authentication requests.
* <p>
* Additional request attributes are being delivered to /autherror POST end point.
*
* @see com.fjordtek.bookstore.web.BookController
*
* @author Pekka Helenius
*/
public class BookStoreAuthenticationFailureHandler implements AuthenticationFailureHandler {
private HttpServerLogger httpServerLogger = new HttpServerLogger();
@Override
public void onAuthenticationFailure(
HttpServletRequest requestData,
HttpServletResponse responseData,
AuthenticationException exception
) throws IOException, ServletException {
responseData.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
httpServerLogger.log(requestData, responseData);
requestData
.setAttribute("username", requestData.getParameter("b_username"));
requestData
.setAttribute("authfailure", "Authentication failure!");
requestData.getRequestDispatcher("/autherror")
.forward(requestData, responseData);
}
}

+ 45
- 0
bookstore/src/main/java/com/fjordtek/bookstore/service/session/BookStoreAuthenticationSuccessHandler.java View File

@ -0,0 +1,45 @@
//Pekka Helenius <fincer89@hotmail.com>, Fjordtek 2020
package com.fjordtek.bookstore.service.session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
/**
*
* This class implements Spring Framework security AuthenticationSuccessHandler
* interface with specific method overrides.
* <p>
* Main purpose is to properly handle valid authentication requests.
*
* @author Pekka Helenius
*/
public class BookStoreAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Override
public void onAuthenticationSuccess(
HttpServletRequest requestData,
HttpServletResponse responseData,
Authentication authentication
) throws IOException, ServletException {
// Nothing special here
// TODO add proper server logging for auditing purposes
redirectStrategy.sendRedirect(requestData, responseData, "/");
//responseData.sendRedirect("/");
}
}

Loading…
Cancel
Save