From 491df00b1a7f43f89b908f04b9cbd2443b8fe11d Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Sun, 4 Oct 2020 14:14:23 +0300 Subject: [PATCH] Implement /autherror POST end point; update list page method name Signed-off-by: Pekka Helenius --- .../bookstore/web/BookController.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java index d5be729..7a5dd3e 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -27,6 +27,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.fjordtek.bookstore.model.book.AuthorRepository; import com.fjordtek.bookstore.model.book.Book; @@ -127,17 +128,52 @@ public class BookController { value = bookListPageView, method = { RequestMethod.GET, RequestMethod.POST } ) - public String defaultWebFormGet( + public String defaultWebFormGetPost( HttpServletRequest requestData, HttpServletResponse responseData, Model dataModel ) { dataModel.addAttribute("books", bookRepository.findAll()); - httpServerLogger.log(requestData, responseData); return bookListPageView; + } + + ////////////////////////////// + // AUTHENTICATION ERROR + + /** + * @see com.fjordtek.bookstore.service.session.BookStoreAuthenticationFailureHandler + * @see com.fjordtek.bookstore.config.WebSecurityConfig + */ + @RequestMapping( + value = "/autherror", + method = RequestMethod.POST + ) + public String authErrorWebFormPost( + HttpServletRequest requestData, + HttpServletResponse responseData, + RedirectAttributes redirectAttributes + ) { + + /* + * We get these parameters from BookStoreAuthenticationFailureHandler + */ + String authfailure = (String) requestData.getAttribute("authfailure"); + String username = (String) requestData.getAttribute("username"); + + if (!username.trim().isEmpty()) { + authfailure = authfailure + " (" + username + ")"; + } + + /* + * Add authfailure attribute to the model + * This attribute is referred in file templates/fragments/loginout.html + */ + redirectAttributes.addFlashAttribute("authfailure", authfailure); + + return "redirect:/" + bookListPageView; }