๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ ๋ฐฑ์—”๋“œ/SpringBoot

[๋ณด์•ˆ] ์ฟ ํ‚ค์™€ ์„ธ์…˜ ๊ทธ๋ฆฌ๊ณ  JWT

by OR15A 2023. 11. 23.

์„ธ์…˜, ์ฟ ํ‚ค, JWT๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ธ์ฆ ๋ฐ ์„ธ์…˜ ๊ด€๋ฆฌ์— ์‚ฌ์šฉ๋˜๋Š” ์ค‘์š”ํ•œ ๊ธฐ์ˆ 

์ฟ ํ‚ค (Cookies)

์ฟ ํ‚ค๋Š” ํด๋ผ์ด์–ธํŠธ ์ธก(๋ธŒ๋ผ์šฐ์ €)์— ์ €์žฅ๋˜๋Š” ์ž‘์€ ๋ฐ์ดํ„ฐ ์กฐ๊ฐ๋“ค

์žฅ์ :
  • ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ ์ €์žฅ ๋ฐ ์ถ”์ ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
  • ์„ธ์…˜ ๊ด€๋ฆฌ, ์‚ฌ์šฉ์ž ์ธ์ฆ, ์‚ฌ์šฉ์ž ์„ ํ˜ธ๋„ ๋“ฑ์„ ์œ ์ง€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
๋‹จ์ :
  • ์šฉ๋Ÿ‰ ์ œํ•œ(๋Œ€๋žต 4KB)์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋งค HTTP ์š”์ฒญ๋งˆ๋‹ค ์ฟ ํ‚ค ๋ฐ์ดํ„ฐ๊ฐ€ ์„œ๋ฒ„๋กœ ์ „์†ก๋˜๋ฏ€๋กœ, ๋„คํŠธ์›Œํฌ ํŠธ๋ž˜ํ”ฝ์— ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • XSS(Cross-Site Scripting)์™€ ๊ฐ™์€ ๋ณด์•ˆ ์ทจ์•ฝ์ ์— ๋…ธ์ถœ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์‚ฌ์šฉ ๋ฐฉ๋ฒ•:
  • ์ฟ ํ‚ค๋Š” Set-Cookie ํ—ค๋”๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋˜๋ฉฐ, ๋ธŒ๋ผ์šฐ์ €๋Š” ์ดํ›„ ์š”์ฒญ์— Cookie ํ—ค๋”๋ฅผ ์ž๋™์œผ๋กœ ํฌํ•จ์‹œํ‚ต๋‹ˆ๋‹ค.
  • HttpOnly, Secure, SameSite ๋“ฑ์˜ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
// ์ฟ ํ‚ค ์ƒ์„ฑ ๋ฐ ์„ค์ •
Cookie cookie = new Cookie("session_id", "123456789");
cookie.setHttpOnly(true);
cookie.setMaxAge(7 * 24 * 60 * 60); // 1์ฃผ์ผ
response.addCookie(cookie);

// ์ฟ ํ‚ค ๊ฐ€์ ธ์˜ค๊ธฐ
Cookie[] cookies = request.getCookies();
if (cookies != null) {
    for (Cookie c : cookies) {
        if ("session_id".equals(c.getName())) {
            // ์„ธ์…˜ ID ์ฒ˜๋ฆฌ
        }
    }
}

 

์„ธ์…˜ (Sessions)

์„ธ์…˜์€ ์„œ๋ฒ„ ์ธก์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•

์žฅ์ :
  • ๋ณด์•ˆ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ€ ์„œ๋ฒ„์— ์ €์žฅ๋˜๋ฏ€๋กœ ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ํฐ ๋ฐ์ดํ„ฐ ์ €์žฅ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
๋‹จ์ :
  • ์„œ๋ฒ„์— ๋ถ€๋‹ด์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŽ์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘์†ํ•  ๊ฒฝ์šฐ ์„œ๋ฒ„ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋งŽ์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋Š” ์„œ๋ฒ„ ํ™˜๊ฒฝ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
์‚ฌ์šฉ ๋ฐฉ๋ฒ•:
  • ์„ธ์…˜ ID๋Š” ์ฟ ํ‚ค๋ฅผ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅ๋˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„๋Š” ๊ฐ ์š”์ฒญ์„ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ฒ„๋Š” ์„ธ์…˜ ID์™€ ์—ฐ๊ฒฐ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
// ์„ธ์…˜์— ์‚ฌ์šฉ์ž ์ •๋ณด ์ €์žฅ
HttpSession session = request.getSession();
session.setAttribute("user", userObject);

// ์„ธ์…˜์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
User user = (User) session.getAttribute("user");

 

 

JWT (JSON Web Tokens)

JWT๋Š” JSON ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๋ฅผ ํ† ํฐ ํ˜•ํƒœ๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ์ „์†กํ•˜๋Š” ๋ฐฉ๋ฒ•

์žฅ์ :
  • ํ™•์žฅ์„ฑ: ๋ถ„์‚ฐ ์‹œ์Šคํ…œ ๋˜๋Š” ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์•„ํ‚คํ…์ฒ˜์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
  • ์„ธ์…˜ ์ƒํƒœ๋ฅผ ์„œ๋ฒ„์—์„œ ์œ ์ง€ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์„œ๋ฒ„์˜ ๋ถ€๋‹ด์ด ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋ฐ”์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ์˜ ํ†ตํ•ฉ์ด ์šฉ์ดํ•ฉ๋‹ˆ๋‹ค.
๋‹จ์ :
  • ํ† ํฐ์ด ํƒˆ์ทจ๋˜๋ฉด ์‚ฌ์šฉ์ž์˜ ์ •๋ณด๊ฐ€ ๋…ธ์ถœ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ† ํฐ ํฌ๊ธฐ๊ฐ€ ์ปค์งˆ ์ˆ˜ ์žˆ์–ด ๋„คํŠธ์›Œํฌ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ† ํฐ์ด ๋งŒ๋ฃŒ๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” ๋ฌดํšจํ™”ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
์‚ฌ์šฉ ๋ฐฉ๋ฒ•:
  • JWT๋Š” header, payload, signature ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ฒ„๋Š” ์ธ์ฆ ํ›„ JWT๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ํด๋ผ์ด์–ธํŠธ๋Š” ์ดํ›„ ์š”์ฒญ์— JWT๋ฅผ ํฌํ•จ์‹œ์ผœ ์„œ๋ฒ„์— ์ „์†กํ•ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ฒ„๋Š” JWT๋ฅผ ๊ฒ€์ฆํ•˜๊ณ  ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
// JWT ์ƒ์„ฑ
String token = Jwts.builder()
    .setSubject(user.getUsername())
    .setExpiration(new Date(System.currentTimeMillis() + 864000000))
    .signWith(SignatureAlgorithm.HS512, "secretKey")
    .compact();

// ์š”์ฒญ์—์„œ JWT ๊ฒ€์ฆ
String token = request.getHeader("Authorization");
if (token != null) {
    // ํ† ํฐ ๊ฒ€์ฆ ๋กœ์ง
}

 

๊ฒฐ๋ก 

์ฟ ํ‚ค: ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์ €์žฅํ•  ๋•Œ ์œ ์šฉํ•˜์ง€๋งŒ, ๋ณด์•ˆ ๋ฐ ์šฉ๋Ÿ‰ ์ œํ•œ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์„ธ์…˜: ์„œ๋ฒ„ ์ธก์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋‚˜, ์„œ๋ฒ„์— ๋ถ€๋‹ด์„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

JWT: ๋ถ„์‚ฐ ํ™˜๊ฒฝ์— ์ ํ•ฉํ•˜๊ณ  ์„œ๋ฒ„ ๋ถ€๋‹ด์ด ์ ์ง€๋งŒ, ๋ณด์•ˆ๊ณผ ํ† ํฐ ๊ด€๋ฆฌ์— ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

์ฟ ํ‚ค, ์„ธ์…˜, JWT์˜ ์‚ฌ์šฉ ์‹œ์ 

  • ๋กœ๊ทธ์ธ: ์‚ฌ์šฉ์ž ์ธ์ฆ ํ›„ ์„ธ์…˜ ID ๋˜๋Š” JWT๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์„ธ์…˜ ID๋Š” ์ฟ ํ‚ค์— ์ €์žฅ๋˜๊ฑฐ๋‚˜ JWT๋Š” ๋ณดํ†ต Authorization ํ—ค๋”์— ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.
  • ํšŒ์›๊ฐ€์ž…: ํšŒ์›๊ฐ€์ž… ์‹œ์—๋Š” ๋ณดํ†ต ์ฟ ํ‚ค๋‚˜ ์„ธ์…˜, JWT๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Œ€์‹  ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ธ์ฆ ๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉ์ž ์ƒํƒœ ์œ ์ง€: ๋กœ๊ทธ์ธ ํ›„ ์ฟ ํ‚ค, ์„ธ์…˜ ๋˜๋Š” JWT๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•  ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž๋ฅผ ์ธ์ฆํ•ฉ๋‹ˆ๋‹ค.