MCPcopy Create free account
hub / github.com/castello/spring_basic / LoginController

Class LoginController

ch4/LoginController.java:19–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17import org.springframework.web.bind.annotation.RequestMapping;
18
19@Controller
20@RequestMapping("/login")
21public class LoginController {
22 @Autowired
23 UserDao userDao;
24
25 @GetMapping("/login")
26 public String loginForm() {
27 return "loginForm";
28 }
29
30 @GetMapping("/logout")
31 public String logout(HttpSession session) {
32 // 1. 세션을 종료
33 session.invalidate();
34 // 2. 홈으로 이동
35 return "redirect:/";
36 }
37
38 @PostMapping("/login")
39 public String login(String id, String pwd, String toURL, boolean rememberId,
40 HttpServletRequest request, HttpServletResponse response) throws Exception {
41
42 // 1. id와 pwd를 확인
43 if(!loginCheck(id, pwd)) {
44 // 2-1 일치하지 않으면, loginForm으로 이동
45 String msg = URLEncoder.encode("id 또는 pwd가 일치하지 않습니다.", "utf-8");
46
47 return "redirect:/login/login?msg="+msg;
48 }
49 // 2-2. id와 pwd가 일치하면,
50 // 세션 객체를 얻어오기
51 HttpSession session = request.getSession();
52 // 세션 객체에 id를 저장
53 session.setAttribute("id", id);
54
55 if(rememberId) {
56 // 1. 쿠키를 생성
57 Cookie cookie = new Cookie("id", id); // ctrl+shift+o 자동 import
58// 2. 응답에 저장
59 response.addCookie(cookie);
60 } else {
61 // 1. 쿠키를 삭제
62 Cookie cookie = new Cookie("id", id); // ctrl+shift+o 자동 import
63 cookie.setMaxAge(0); // 쿠키를 삭제
64// 2. 응답에 저장
65 response.addCookie(cookie);
66 }
67// 3. 홈으로 이동
68 toURL = toURL==null || toURL.equals("") ? "/" : toURL;
69
70 return "redirect:"+toURL;
71 }
72
73 private boolean loginCheck(String id, String pwd) {
74 User user = null;
75
76 try {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected