inblog logo
|
jay0628
    SpringBoot

    [Spring Boot] 23. 스프링부트 뱅크 v1 (5) Cookie & Session

    김주희's avatar
    김주희
    Mar 24, 2025
    [Spring Boot] 23. 스프링부트 뱅크 v1 (5) Cookie & Session
    Contents
    1. 동작 과정
    notion image

    1. 동작 과정

    1. “/login-form”으로 들어가면 response header에 “Set-Cookie”라는 key에 JSESSION이
    notion image
     
    notion image
    notion image
    notion image
    notion image
    notion image
    notion image
    notion image
     
     
     
     
     
     
     
     
     
     
     

    login-form에 먼저 들어가지 않고 join-form에 들어간다면

    1. 코드상에서 session을 건드리면(접근 등) 일단 JSESSIONID를 준다
    notion image
     
     
     
    notion image
     
     
     
    package com.metacoding.bankv1.user; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @RequiredArgsConstructor @Controller public class UserController { private final UserService userService; private final HttpSession session; @GetMapping("/login-form") public String loginForm() { session.setAttribute("metacoding", "apple"); // 세션 id(랜덤) 안의 key-value(해시맵) return "user/login-form"; } @GetMapping("/join-form") public String joinForm() { String value = (String) session.getAttribute("metacoding"); //Object type이라서 다운캐스팅 System.out.println("value: " + value); return "user/join-form"; } @PostMapping("/join") public String join(UserRequest.JoinDTO joinDTO) { // System.out.println(joinDTO); // toString()이 실행됨 userService.회원가입(joinDTO); // joinDTO 안에 username, password, fullname return "redirect:/login-form"; } }
     
    Share article

    jay0628

    RSS·Powered by Inblog