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

Class LoginController

ch3/LoginController.java:16–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected