login : cookie
This commit is contained in:
@@ -1,15 +1,39 @@
|
||||
package hello.login.web;
|
||||
|
||||
import hello.login.domain.member.Member;
|
||||
import hello.login.domain.member.MemberRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.CookieValue;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequiredArgsConstructor
|
||||
public class HomeController {
|
||||
|
||||
@GetMapping("/")
|
||||
private final MemberRepository memberRepository;
|
||||
|
||||
// @GetMapping("/")
|
||||
public String home() {
|
||||
return "home";
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String homeLogin(@CookieValue(name = "memberId", required = false) Long memberId, Model model) {
|
||||
if (memberId == null){
|
||||
return "home";
|
||||
}
|
||||
|
||||
// 로그인
|
||||
Member loginMember = memberRepository.findById(memberId);
|
||||
if (loginMember == null) {
|
||||
return "home";
|
||||
}
|
||||
|
||||
model.addAttribute("member", loginMember);
|
||||
return "loginHome";
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
|
||||
@Controller
|
||||
@@ -23,7 +25,7 @@ public class LoginController {
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public String login(@Valid @ModelAttribute LoginForm form, BindingResult bindingResult) {
|
||||
public String login(@Valid @ModelAttribute LoginForm form, BindingResult bindingResult, HttpServletResponse response) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
return "login/loginForm";
|
||||
}
|
||||
@@ -35,8 +37,24 @@ public class LoginController {
|
||||
return "login/loginForm";
|
||||
}
|
||||
|
||||
// 로그인 성공 처리 TODO
|
||||
// 로그인 성공 처리
|
||||
|
||||
// 쿠키에 시간 정보를 주지 않으면 세션 쿠키( 브라우저 종료 시 삭제 )
|
||||
Cookie idCookie = new Cookie("memberId", String.valueOf(loginMember.getId()));
|
||||
response.addCookie(idCookie);
|
||||
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
@PostMapping("/logout")
|
||||
public String logout(HttpServletResponse response) {
|
||||
expireCookie(response, "memberId");
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
private void expireCookie(HttpServletResponse response, String cookieName) {
|
||||
Cookie cookie = new Cookie(cookieName, null);
|
||||
cookie.setMaxAge(0);
|
||||
response.addCookie(cookie);
|
||||
}
|
||||
}
|
||||
|
||||
34
login/src/main/resources/templates/loginHome.html
Normal file
34
login/src/main/resources/templates/loginHome.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<link th:href="@{/css/bootstrap.min.css}"
|
||||
href="../css/bootstrap.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container" style="max-width: 600px">
|
||||
<div class="py-5 text-center">
|
||||
<h2>홈 화면</h2>
|
||||
</div>
|
||||
<h4 class="mb-3" th:text="|로그인: ${member.name}|">로그인 사용자 이름</h4>
|
||||
<hr class="my-4">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<button class="w-100 btn btn-secondary btn-lg" type="button"
|
||||
th:onclick="|location.href='@{/items}'|">
|
||||
상품 관리
|
||||
</button>
|
||||
</div>
|
||||
<div class="col">
|
||||
<form th:action="@{/logout}" method="post">
|
||||
<button class="w-100 btn btn-dark btn-lg"
|
||||
onclick="location.href='items.html'" type="submit">
|
||||
로그아웃
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<hr class="my-4">
|
||||
</div> <!-- /container -->
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user