LoginController.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.tsi.sig.server.controller;
  2. import com.tsi.sig.server.objects.UserVO;
  3. import com.tsi.sig.server.service.LoginServiceImpl;
  4. import lombok.AllArgsConstructor;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  7. import org.springframework.security.core.Authentication;
  8. import org.springframework.security.core.context.SecurityContext;
  9. import org.springframework.security.core.context.SecurityContextHolder;
  10. import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.ModelAttribute;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import javax.servlet.http.HttpSession;
  20. import java.io.IOException;
  21. @Slf4j
  22. @Controller
  23. @AllArgsConstructor
  24. public class LoginController {
  25. private final LoginServiceImpl loginServiceImpl;
  26. private final String REDIR2DENY = "redirect:/denied.do";
  27. @RequestMapping({"/showlogin.do"})
  28. public String showlogin() throws Exception {
  29. return "login";
  30. }
  31. @PostMapping({"/login.do"})
  32. public String login(UserVO userVO, RedirectAttributes redir) throws IOException {
  33. if (userVO == null) {
  34. return "redirect:/denied.do";
  35. } else {
  36. String reqId = userVO.getUserId();
  37. String reqPw = userVO.getUserPswd();
  38. UserVO userDetails = this.loginServiceImpl.loadUserByUsername(reqId);
  39. if (userDetails.getUserId() == null) {
  40. return "redirect:/denied.do";
  41. } else {
  42. redir.addFlashAttribute("userVO", userDetails);
  43. if (userDetails.getUseyn().equals("N")) {
  44. return "redirect:/denied.do";
  45. } else {
  46. this.loginServiceImpl.initFailCnt(reqId);
  47. SecurityContext context = SecurityContextHolder.createEmptyContext();
  48. UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, reqPw, userDetails.getAuthorities());
  49. context.setAuthentication(authentication);
  50. SecurityContextHolder.setContext(context);
  51. return "redirect:/login.do";
  52. }
  53. }
  54. }
  55. }
  56. @GetMapping({"/login.do"})
  57. public String showDashBoard() {
  58. return "main";
  59. }
  60. @GetMapping({"/denied.do"})
  61. public String denied(HttpServletRequest req, @ModelAttribute("userVO") UserVO userVO) {
  62. if (userVO.getUserId() != null) {
  63. String useYN = userVO.getUseyn();
  64. req.setAttribute("userId", userVO.getUserId());
  65. req.setAttribute("useYN", useYN);
  66. req.setAttribute("failCnt", userVO.getFailCnt());
  67. if (useYN.equals("Y")) {
  68. this.loginServiceImpl.loginFail(userVO.getUserId());
  69. }
  70. }
  71. return "denied";
  72. }
  73. @GetMapping({"/logout.do"})
  74. public String logout(HttpServletResponse res, HttpServletRequest req, HttpSession session) {
  75. Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  76. if (auth != null) {
  77. (new SecurityContextLogoutHandler()).logout(req, res, auth);
  78. }
  79. return "login";
  80. }
  81. }