๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Today I Learned(TIL)/์ˆ˜์ค€๋ณ„ ํ•™์Šต๋ฐ˜

์ˆ˜์ค€๋ณ„ ํ•™์Šต๋ฐ˜_๋ฒ ์ด์ง๋ฐ˜ 3ํšŒ์ฐจ ์„ธ์…˜

by carrot0911 2024. 12. 16.

๋ฒ ์ด์ง๋ฐ˜

์ธ์ฆ์ธ๊ฐ€

12/13 14:00 ~ 15:40 (์•ฝ 1์‹œ๊ฐ„ 40๋ถ„ ์ง„ํ–‰)

 

์ค‘์š” ํ‚ค์›Œ๋“œ

  • ์ฟ ํ‚ค
  • ์ฟ ํ‚ค ์ธ์ฆ ๋ฐฉ์‹
  • ์„ธ์…˜
  • ์„ธ์…˜ ์ธ์ฆ ๋ฐฉ์‹
  • ํ† ํฐ
  • ํ† ํฐ ์ธ์ฆ ๋ฐฉ์‹
  • ํ•„ํ„ฐ

์ฟ ํ‚ค, ์„ธ์…˜, ํ† ํฐ ์ธ์ฆ ๋ฐฉ์‹์€ ๊ฐœ๋ฐœ์„ ํ•˜๋ ค๋ฉด ๋ฌด์กฐ๊ฑด ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•˜๋Š” ๋‚ด์šฉ์ด๋‹ค!
์ธ์ฆ ๋ฐฉ์‹์„ ๋น„๊ตํ•ด๋ณด๋Š” ๊ฒƒ์ด ์ข€ ๋” ์ดํ•ด๊ฐ€ ์‰ฝ๋‹ค.

 

์ฟ ํ‚ค ์ธ์ฆ ๋ฐฉ์‹

์ฟ ํ‚ค

  • ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ ‘์†ํ–ˆ์„ ๋•Œ, ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋„ ์‚ฌ์šฉ๋œ๋‹ค.
  • ์‚ฌ์šฉ์ž์˜ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ์ž‘์€ ๊ณต๊ฐ„์ด๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ๋ธŒ๋ผ์šฐ์ €์— ์ €์žฅ ๊ณต๊ฐ„์ด ํ•„์š”ํ• ๊นŒ?!

์‚ฌ์šฉ์„ฑ

  • ๊ฐœ์ธํ™”๋œ ๊ฒฝํ—˜ ์ œ๊ณต → ๋งž์ถคํ˜• ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์„ฑ๋Šฅ ์ตœ์ ํ™” → ๋ธŒ๋ผ์šฐ์ €, ์ฟ ํ‚ค์—์„œ ๋ฐ”๋กœ ๊บผ๋‚ด์„œ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๋‹ค.
  • ์˜คํ”„๋ผ์ธ ์ง€์›

์ฟ ํ‚ค๋ฅผ ํ™œ์šฉํ•œ ๊ธฐ๋Šฅ

๋ธŒ๋ผ์šฐ์ €์—์„œ ์š”์ฒญ์ด ๊ฐˆ ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ Request Header์— ๊ทธ ๊ฐ’๋“ค์ด ํฌํ•จ๋˜์–ด์„œ ์„œ๋ฒ„๋กœ ๋‚ ์•„๊ฐ„๋‹ค.
๋ณ„๋„์˜ ์„ค์ • ์—†์ด๋„ ๊ฐ€๋Šฅํ•˜๋‹ค!

์ฟ ํ‚ค ์ธ์ฆ ๋ฐฉ์‹

โญ ์—๋Ÿฌ ์ถ”์  ๋ฒ”์œ„๋ฅผ ์ขํžˆ๋Š” ๊ฟ€ํŒ!

๋กœ๊ทธ๋ฅผ ์ฐ์–ด๊ฐ€๋ฉด์„œ ๋ฒ”์œ„๋ฅผ ์ค„์—ฌ๋‚˜๊ฐ„๋‹ค!!
@Slf4j๋ฅผ ์‚ฌ์šฉํ•ด์„œ lod.info๋ฅผ ํ™œ์šฉํ•ด ๋กœ๊ทธ๋ฅผ ์ฐ์–ด๋ณด๋ฉด์„œ ์–ด๋””์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์ค„์—ฌ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

์ฟ ํ‚ค ์‹ค์Šต ์ฝ”๋“œ

๋”๋ณด๊ธฐ
@GetMapping("/cookie")
public String cookieAPI(HttpServletRequest request) {
    log.info("::: AuthController.cookieAPI()");

    // 1. Request ๊ฐ์ฒด์—์„œ cookie ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
    Cookie[] cookies = request.getCookies();

    // 2. ์ฟ ํ‚ค (UserId) ํ‚ค๊ฐ’์œผ๋กœ value ์ฐพ๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ
    String userId = this.findCookie("userId", cookies);

    // 3. ์ฐพ์•„์˜จ ์ฟ ํ‚ค์˜ ๊ฐ’ ์ถœ๋ ฅ
    if (userId != null) {
        log.info("found userId: {}", userId);
    } else {
        log.info("userId not found");
    }
    return "success";
}

@GetMapping("/cookie-login")
public ResponseEntity<String> cookieLoginAPI() {
    log.info("::: AuthController.cookieLoginAPI()");
    // 1. ๋กœ๊ทธ์ธ ๋กœ์ง ์ฒ˜๋ฆฌ => DB ์กฐํšŒํ•ด์„œ ์œ ์ € ์ฐพ๊ธฐ
    String cookieKey = "userId";
    String userId = "1";

    // 2. ์ƒ์„ฑ - ํ—ค๋” ์ƒ์„ฑ
    String headerValue = cookieKey + "=" + userId;
    HttpHeaders headers = new HttpHeaders();
    headers.set("Set-Cookie", headerValue);

    // 3. ์‘๋‹ต ๋ฐ˜ํ™˜
    return new ResponseEntity<>("๋กœ๊ทธ์ธ ์„ฑ๊ณต", headers, HttpStatus.OK);
}

private String findCookie(String key, Cookie[] cookies) {
    if (cookies != null ) {
        for (Cookie cookie : cookies) {
            if (key.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
    }
    return null;
}

์ฟ ํ‚ค ์ธ์ฆ ๋ฐฉ์‹์˜ ํ•œ๊ณ„

์—ฌ๊ธฐ์„œ ๋งŒ์•ฝ.. ๋ธŒ๋ผ์šฐ์ €์˜ ์ •๋ณด๊ฐ€ ํƒˆ์ทจ๋œ๋‹ค๋ฉด?!

  • ๊ทธ๋ž˜์„œ ์ฟ ํ‚ค์—๋Š” ์œ ์ถœ๋˜๋ฉด ์•ˆ๋˜๋Š” ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜๋ฉด ์•ˆ๋œ๋‹ค. 

 

์„ธ์…˜ ์ธ์ฆ ๋ฐฉ์‹

์„ธ์…˜

  • ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„์˜ ์—ฐ๊ฒฐ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋‚˜์˜ ์ƒํƒœ์ด๋‹ค.
  • ํ•˜๋‚˜์˜ ์—ฐ๊ฒฐ ์ƒํƒœ์ด๋‹ค.

์„ธ์…˜ ์ธ์ฆ ๋ฐฉ์‹

  1. [ ํด๋ผ์ด์–ธํŠธ ] ์„ธ์…˜ ์ •๋ณด๋ฅผ ๋ณด๋‚ธ๋‹ค.
  2. ์„œ๋ฒ„ ] ์„ธ์…˜ ์ •๋ณด๋ฅผ ๋ฐ›๋Š”๋‹ค.
  3. [ ์„œ๋ฒ„ ] ์„ธ์…˜ ์ •๋ณด๋ฅผ ์„ธ์…˜ ์ €์žฅ์†Œ๋ฅผ ํ†ตํ•ด์„œ ์กฐํšŒํ•œ๋‹ค.
  4. [ ์„œ๋ฒ„ ] ์„ธ์…˜ ์ €์žฅ์†Œ์— ์„ธ์…˜ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด ์ธ์ฆ๋œ ์œ ์ €์ด๋‹ค.
  5. [ ์„œ๋ฒ„ ] ์„ธ์…˜ ์ €์žฅ์†Œ์— ์„ธ์…˜ ์ •๋ณด๊ฐ€ ์—†๋‹ค๋ฉด ์ธ์ฆ๋˜์ง€ ์•Š์€ ์œ ์ €์ด๋‹ค.

์„ธ์…˜์€ ์•”ํ˜ธํ™”๊ฐ€ ์•„๋‹ˆ๋‹ค!!!
์„ธ์…˜ ์ €์žฅ์†Œ์—์„œ ์ฃผ๊ธฐ์ ์œผ๋กœ key ๊ฐ’์„ ๋ฐ”๊ฟ”์ค€๋‹ค๋ฉด ํ›จ์”ฌ ์•ˆ์ „ํ•ด์ง„๋‹ค.

์„œ๋ฒ„ ์ชฝ๋งŒ ์—…๋ฐ์ดํŠธ ํ•˜๋ฉด ๋œ๋‹ค. ์„œ๋ฒ„์—์„œ๋Š” ์œ ์ €์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€๊ฒฝ๋˜๋”๋ผ๋„ ์š”์ฒญ์ด ์™”์„ ๋•Œ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
ํ•ด์ปค๊ฐ€ ๊ฐ’์„ ๊ฐ€์ ธ๊ฐ€๋”๋ผ๋„ ์—…๋ฐ์ดํŠธ๋˜๋ฉด ๊ทธ ์ „์— ๊ฐ€์ ธ๊ฐ„ ๊ฐ’์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค.
์„œ๋ฒ„์—์„œ ์œ ์ €์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค. ์ฑ…์ž„์ด ์„œ๋ฒ„์—๊ฒŒ ์žˆ๋‹ค.

ํ๋ฆ„๋„

์„ธ์…˜ ํ๋ฆ„๋„

์‹ค์Šต

๋”๋ณด๊ธฐ
// ์„ธ์…˜ ์ €์žฅ์†Œ
private final Map<String, String> sessionStorage = new HashMap<>();


@GetMapping("/session")
public String sessionAPI(HttpServletRequest request) {
    log.info("::: AuthController.sessionAPI()");
    Cookie[] cookies = request.getCookies();
    String sessionId = this.findCookie("sessionId", cookies);
    if (sessionId != null) {
        // ์„ธ์…˜์ €์žฅ์†Œ์—์„œ ์œ ์ € ๋ฐ์ดํ„ฐ ์กฐํšŒ & ํ™œ์šฉ
        String userData = sessionStorage.get(sessionId);
        log.info("found session: {}", userData);
    } else {
        log.info("session not found");
    }
    return "success";
}

@GetMapping("/session-login")
public ResponseEntity<String> sessionLoginAPI() {
    log.info("::: AuthController.sessionLogin()");
    // 1. ๋กœ๊ทธ์ธ ๋กœ์ง ์ฒ˜๋ฆฌ

    // 2. ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ
    String sessionId = "xxxx";
    String sessionData = "userId: 1";

    // 3. ์„ธ์…˜ ์ €์žฅ์†Œ์— ๋ฐ์ดํ„ฐ ์ €์žฅ
    sessionStorage.put(sessionId, sessionData);

    // 4. ์ƒ์„ฑ - ํ—ค๋” ์ƒ์„ฑ
    String headerValue = "sessionId" + "=" + sessionId;
    HttpHeaders headers = new HttpHeaders();
    headers.set("Set-Cookie", headerValue);

    // 5. ์‘๋‹ต ๋ฐ˜ํ™˜
    return new ResponseEntity<>("๋กœ๊ทธ์ธ ์„ฑ๊ณต", headers, HttpStatus.OK);
}

 

ํ† ํฐ ์ธ์ฆ ๋ฐฉ์‹

ํ† ํฐ

  • ์‚ฌ์šฉ์ž์™€ ์„œ๋ฒ„์˜ ์—ฐ๊ฒฐ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ•˜๋‚˜์˜ ํ† ํฐ์ด๋‹ค.

ํ† ํฐ ์ธ์ฆ ๋ฐฉ์‹

์ฟ ํ‚ค์— ์ €์žฅ๋œ ๋ฌธ์ž์—ด์—๋Š” ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค.
์˜๋ฏธ๊ฐ€ ์žˆ๋Š” Value ๊ฐ’์ด์ง€๋งŒ ํ•œ ๋ฒˆ ๊ผฌ์•„๋†“์€ ๊ฒƒ์ด๋‹ค. (Base64 ์ธ์ฝ”๋”ฉ)
์ €์žฅ์„ ํด๋ผ์ด์–ธํŠธ์—์„œ ํ•˜๊ณ  ์žˆ๋‹ค!

์‹ค์Šต

๋”๋ณด๊ธฐ
@GetMapping("/token")
public String tokenAPI(HttpServletRequest request) {
    log.info("::: AuthController.tokenAPI()");
    Cookie[] cookies = request.getCookies();
    String token = this.findCookie("token", cookies);
    if (token != null) {
        // ํ† ํฐ์„ ํ•ด๋…ํ•ด์„œ ์œ ์ € ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉ
        String decodedToken = parseToken(token);
        log.info("found token: {}", token);
        log.info(decodedToken);

    } else {
        log.info("token not found");
    }
    return "success";
}


@GetMapping("/token-login")
public ResponseEntity<String> tokenLoginAPI() {
    log.info("::: AuthController.tokenLoginAPI()");
    // 1. ๋กœ๊ทธ์ธ ๋กœ์ง ์ฒ˜๋ฆฌ

    // 2. ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ
    String userData = "userId: 1";

    // 3. ํ† ํฐ ์ƒ์„ฑ
    String encodedData = encodeToBase64(userData);

    // 4. ์ƒ์„ฑ - ํ—ค๋” ์ƒ์„ฑ
    String headerValue = "token" + "=" + encodedData;
    HttpHeaders headers = new HttpHeaders();
    headers.set("Set-Cookie", headerValue);

    // 5. ์‘๋‹ต ๋ฐ˜ํ™˜
    return new ResponseEntity<>("๋กœ๊ทธ์ธ ์„ฑ๊ณต", headers, HttpStatus.OK);
}

private String encodeToBase64(String input) {
    byte[] encodedBytes = Base64.getEncoder().encode(input.getBytes());
    String encodedToken = new String(encodedBytes);
    return encodedToken;
}

private String parseToken(String token) {
    byte[] decodedBytes = Base64.getDecoder().decode(token);
    String decodedToken = new String(decodedBytes);
    return decodedToken;
}

private String findCookie(String key, Cookie[] cookies) {
    if (cookies != null ) {
        for (Cookie cookie : cookies) {
            if (key.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
    }
    return null;
}

 

์ธ์ฆ ๋ฐฉ์‹ ์ •๋ฆฌ โญโญโญโญโญ

  ๋‚ด์šฉ ํ™•์ธ ๊ฐ€๋Šฅ ๋‚ด์šฉ ๋ณ€์กฐ ๊ฐ€๋Šฅ ์œ ์ € ์ƒํƒœ ์ €์žฅ ์œ„์น˜ ์„œ๋ฒ„ ๋ถ€ํ•˜
์ฟ ํ‚ค โœ… โœ… ํด๋ผ์ด์–ธํŠธ ๋‚ฎ์Œ
์„ธ์…˜ โŒ โŒ ์„œ๋ฒ„(์„ธ์…˜ ์ €์žฅ์†Œ) ๋†’์Œ
ํ† ํฐ โœ…(๋””์ฝ”๋”ฉ์‹œ) โœ…(์ธ์ฝ”๋”ฉ์‹œ) ํด๋ผ์ด์–ธํŠธ ๋‚ฎ์Œ

 

HttpSession - ์Šคํ”„๋ง์„ ํ™œ์šฉํ•œ ์„ธ์…˜ ํ™œ์šฉ

๋”๋ณด๊ธฐ
HttpSession session = request.getSession();

// ์„ธ์…˜ ์•„์ด๋”” ์กฐํšŒ
String sessionid = session.getId();

// ์„ธ์…˜ ๊ฐ’ ์„ค์ •
session.setAttribute("memberId", memberId);

// ์„ธ์…˜ ๊ฐ’ ์กฐํšŒ
Long foundMemberId = (Long) session.getAttribute("memberId");

// ์„ธ์…˜ ๊ฐ’ ์ œ๊ฑฐ
session.removeAttribute("memberId");

// ์„ธ์…˜ ์‚ญ์ œ
session.invalidate();

// ์„ธ์…˜ ์ƒ์„ฑ ์‹œ๊ฐ„ ์กฐํšŒ
long creationTime = session.getCreationTime();

// ์„ธ์…˜ ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ ์‹œ๊ฐ„ ์กฐํšŒ
long lastAccessedTime = session.getLastAccessedTime();

// ์„ธ์…˜ ์œ ํšจ ์‹œ๊ฐ„ ์„ค์ •(์ดˆ๋‹จ์œ„๋กœ ์„ค์ •: 10์ดˆ)
session.setMaxInactiveInterval(10);

// ์„ธ์…˜ ์œ ํšจ ์‹œ๊ฐ„ ์กฐํšŒ
int maxInactiveInterval = session.getMaxInactiveInterval();

// ์‹ ๊ทœ ์„ธ์…˜ ์—ฌ๋ถ€ ํ™•์ธ
boolean aNew = session.isNew();

 

ํ•„ํ„ฐ ์„ค๋ช…

์š”์ฒญ ํ๋ฆ„๋„

MVC ๊ตฌ์กฐ ํ™•์žฅ ์‚ฌ์šฉ๋ฒ•

โญ ์Šคํ”„๋ง ์š”์ฒญ ํ๋ฆ„๋„ โญ

 

์ „์ฒด ์‹ค์Šต ์ฝ”๋“œ

๋”๋ณด๊ธฐ
@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {

    // ์„ธ์…˜ ์ €์žฅ์†Œ
    private final Map<String, String> sessionStorage = new HashMap<>();

    @GetMapping("/cookie")
    public String cookieAPI(HttpServletRequest request) {
        log.info("::: AuthController.cookieAPI()");
        Cookie[] cookies = request.getCookies();
        String userId = this.findCookie("userId", cookies);
        if (userId != null) {
            log.info("found userId: {}", userId);
        } else {
            log.info("userId not found");
        }
        return "success";
    }

    @GetMapping("/cookie-login")
    public ResponseEntity<String> cookieLoginAPI() {
        log.info("::: AuthController.cookieLoginAPI()");
        // 1. ๋กœ๊ทธ์ธ ๋กœ์ง ์ฒ˜๋ฆฌ

        String cookieKey = "userId";
        String userId = "1";

        // 4. ์ƒ์„ฑ - ํ—ค๋” ์ƒ์„ฑ
        String headerValue = cookieKey + "=" + userId;
        HttpHeaders headers = new HttpHeaders();
        headers.set("Set-Cookie", headerValue);

        // 5. ์‘๋‹ต ๋ฐ˜ํ™˜
        return new ResponseEntity<>("๋กœ๊ทธ์ธ ์„ฑ๊ณต", headers, HttpStatus.OK);
    }

    @GetMapping("/session")
    public String sessionAPI(HttpServletRequest request) {
        log.info("::: AuthController.sessionAPI()");
        Cookie[] cookies = request.getCookies();
        String sessionId = this.findCookie("sessionId", cookies);
        if (sessionId != null) {
            // ์„ธ์…˜์ €์žฅ์†Œ์—์„œ ์œ ์ € ๋ฐ์ดํ„ฐ ์กฐํšŒ & ํ™œ์šฉ
            String userData = sessionStorage.get(sessionId);
            log.info("found session: {}", userData);
        } else {
            log.info("session not found");
        }
        return "success";
    }

    @GetMapping("/session-login")
    public ResponseEntity<String> sessionLoginAPI() {
        log.info("::: AuthController.sessionLogin()");
        // 1. ๋กœ๊ทธ์ธ ๋กœ์ง ์ฒ˜๋ฆฌ

        // 2. ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ
        String sessionId = "xxxx";
        String sessionData = "userId: 1";

        // 3. ์„ธ์…˜ ์ €์žฅ์†Œ์— ๋ฐ์ดํ„ฐ ์ €์žฅ
        sessionStorage.put(sessionId, sessionData);

        // 4. ์ƒ์„ฑ - ํ—ค๋” ์ƒ์„ฑ
        String headerValue = "sessionId" + "=" + sessionId;
        HttpHeaders headers = new HttpHeaders();
        headers.set("Set-Cookie", headerValue);

        // 5. ์‘๋‹ต ๋ฐ˜ํ™˜
        return new ResponseEntity<>("๋กœ๊ทธ์ธ ์„ฑ๊ณต", headers, HttpStatus.OK);
    }


    @GetMapping("/token")
    public String tokenAPI(HttpServletRequest request) {
        log.info("::: AuthController.tokenAPI()");
        Cookie[] cookies = request.getCookies();
        String token = this.findCookie("token", cookies);
        if (token != null) {
            // ํ† ํฐ์„ ํ•ด๋…ํ•ด์„œ ์œ ์ € ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉ
            String decodedToken = parseToken(token);
            log.info("found token: {}", token);
            log.info(decodedToken);

        } else {
            log.info("token not found");
        }
        return "success";
    }


    @GetMapping("/token-login")
    public ResponseEntity<String> tokenLoginAPI() {
        log.info("::: AuthController.tokenLoginAPI()");
        // 1. ๋กœ๊ทธ์ธ ๋กœ์ง ์ฒ˜๋ฆฌ

        // 2. ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ
        String userData = "userId: 1";

        // 3. ํ† ํฐ ์ƒ์„ฑ
        String encodedData = encodeToBase64(userData);

        // 4. ์ƒ์„ฑ - ํ—ค๋” ์ƒ์„ฑ
        String headerValue = "token" + "=" + encodedData;
        HttpHeaders headers = new HttpHeaders();
        headers.set("Set-Cookie", headerValue);

        // 5. ์‘๋‹ต ๋ฐ˜ํ™˜
        return new ResponseEntity<>("๋กœ๊ทธ์ธ ์„ฑ๊ณต", headers, HttpStatus.OK);
    }

    @GetMapping("/filter")
    public String filterAPI(HttpServletRequest request) {
        log.info("::: AuthController.filterAPI()");
        HttpSession session = request.getSession(false);
        if (session == null) {
            log.info("session not found");
            return "session not found";
        }

        String userId = (String) session.getAttribute("user");
        log.info("found userSession: {}", userId);
        return "found user session:  " + userId;
    }

    @GetMapping("/filter-login")
    public String loginAPI(HttpServletRequest request) {
        log.info("::: AuthController.loginAPI()");

        // ์„ธ์…˜ ์ƒ์„ฑ
        HttpSession session = request.getSession(true);

        // ์„ธ์…˜์ €์žฅ์†Œ์— ์œ ์ € ๋ฐ์ดํ„ฐ ์ €์žฅ
        String userId = "1";
        session.setAttribute("user", userId);
        return "Login successful";
    }

    private String encodeToBase64(String input) {
        byte[] encodedBytes = Base64.getEncoder().encode(input.getBytes());
        String encodedToken = new String(encodedBytes);
        return encodedToken;
    }

    private String parseToken(String token) {
        byte[] decodedBytes = Base64.getDecoder().decode(token);
        String decodedToken = new String(decodedBytes);
        return decodedToken;
    }

    private String findCookie(String key, Cookie[] cookies) {
        if (cookies != null ) {
            for (Cookie cookie : cookies) {
                if (key.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}