#java #spring #kakao-tech-campus

์ฟ ํ‚ค~๐Ÿช

1jeongg 1jeongg Follow 2024๋…„ 04์›” 14์ผ ยท 5 mins read
Share this

ํ•ด๋‹น ๋‚ด์šฉ์€ ์นด์นด์˜ค ํ…Œํฌ ์บ ํผ์Šค์˜ 1๋‹จ๊ณ„ 1์ฃผ์ฐจ ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉฐ ์ž‘์„ฑํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

์ฐธ๊ณ ์ž๋ฃŒ

์ฟ ํ‚ค๋ž€?

์ด๋ฆ„๊ณผ ๊ฐ’์˜ ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ž‘์€ ์ •๋ณด (์•„์Šคํ‚ค ๋ฌธ์ž๋งŒ ๊ฐ€๋Šฅ)

์„œ๋ฒ„์—์„œ ์ƒ์„ฑ ํ›„ ์ „์†ก, ๋ธŒ๋ผ์šฐ์ €์— ์ €์žฅ. ์œ ํšจ๊ธฐ๊ฐ„ ์ดํ›„ ์ž๋™ ์‚ญ์ œ

์„œ๋ฒ„์—์„œ ์š”์ฒญ์‹œ domain, path๊ฐ€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ž๋™ ์ „์†ก

๋™์ž‘๋ฐฉ์‹

Untitled

  1. ํด๋ผ์ด์–ธํŠธ โ†’ ์„œ๋ฒ„ : ๋กœ๊ทธ์ธ ์š”์ฒญ
  2. ์„œ๋ฒ„: ํด๋ผ์ด์–ธํŠธ์˜ ๋กœ๊ทธ์ธ ์š”์ฒญ ์œ ํšจ์„ฑ ํ™•์ธ, ์‘๋‹ต header์— set-cookieL user=chrisjune ์ถ”๊ฐ€ํ•˜์—ฌ ์‘๋‹ต
  3. ํด๋ผ์ด์–ธํŠธ: ์ดํ›„ ์„œ๋ฒ„์— ์š”์ฒญํ•  ๋•Œ ์ฟ ํ‚ค๋ฅผ ์ž๋™์œผ๋กœ ์š”์ฒญํ—ค๋”์— ์ถ”๊ฐ€ํ•˜์—ฌ ์š”์ฒญ

์ฟ ํ‚ค, ์„ธ์…˜, JWT(ํ† ํฐ) ์ฐจ์ด์ 

์ฟ ํ‚ค

์„œ๋ฒ„๊ฐ€ ์šฐ๋ฆฌ์— ๊ด€ํ•œ ๊ฒƒ์„ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด ์šฐ๋ฆฌ์˜ ๋ธŒ๋ผ์šฐ์ €์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ์‹œ์Šคํ…œ ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด์˜ ๋งค๊ฐœ์ฒด

โ†” ๋กœ์ปฌ์— ์ €์žฅ, ํƒˆ์ทจ์™€ ๋ณ€์กฐ๊ฐ€ ๊ฐ€๋Šฅ, ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ข…๋ฃŒํ•ด๋„ ํŒŒ์ผ๋กœ ๋‚จ์•„์žˆ์Œ, ์ƒ๋Œ€์ ์œผ๋กœ ๋น 

Untitled

Untitled

์„ธ์…˜

ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„ ์—ฐ๊ฒฐ์ด ํ™œ์„ฑํ™”๋œ ์ƒํƒœ๋กœ,

์ฟ ํ‚ค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋กœ๊ทธ์ธ๊ณผ ๊ฐ™์€ ์‚ฌ์šฉ์ž ์ธ์ฆ์„ ํ• ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ

โ†” ๋กœ์ปฌ๊ณผ ์„œ๋ฒ„์— ์ €์žฅ, ๋ธŒ๋ผ์šฐ์ €ย ์ข…๋ฃŒ์‹œย ์„ธ์…˜์„ ์‚ญ์ œ, ์ƒ๋Œ€์ ์œผ๋กœ ์•ˆ์ „

Untitled

Untitled

JWT

์ฟ ํ‚ค&์„ธ์…˜ ๋ฐฉ์‹๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์„œ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ™์ด ๋ณด๋‚ด์ค˜์•ผํ•จ ์„œ๋ฒ„์—๊ฒŒ ๋ณด์—ฌ์ค˜์•ผ ํ•˜๋Š” ์‹ ๋ถ„์ฆ ๊ฐ™์€ ๊ฑฐ

Untitled

Untitled

๐Ÿ“Œ ์šฉ์–ด ์ •๋ฆฌ

  • ์ฟ ํ‚คย = ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ ๊ฐ„ ๋งค๊ฐœ์ฒด
  • ์„ธ์…˜ย = ์ฟ ํ‚ค๋ž‘ ๋น„์Šท. ๋Œ€์‹  ์„œ๋ฒ„ ์ชฝ์— ์ •๋ณด๋ฅผ ์ €์žฅ
  • ํ† ํฐย = ์„œ๋ฒ„์—๊ฒŒ ๋ณด์—ฌ์ค˜์•ผํ•˜๋ฉฐ, ์„œ๋ฒ„๊ฐ€ ๊ธฐ์–ตํ•˜๋Š” ์ด์ƒํ•˜๊ณ  ๋ฌด์ง€ ๊ธด string ~ like ์‹ ๋ถ„์ฆ
  • JWTย = ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ํ† ํฐ. DB ์—†์ด ๊ฒ€์ฆ ๊ฐ€๋Šฅ

์ฟ ํ‚ค ์ƒ์„ฑ

Cookie cookie = new Cookie("id", "asdf"); // ์ฟ ํ‚ค ์ƒ์„ฑ
cookie.setMaxAge(60*60*24); // ์œ ํšจ๊ธฐ๊ฐ„ ์„ค์ •(์ดˆ)
response.addCookie(cookie); // ์‘๋‹ต์— ์ฟ ํ‚ค ์ถ”๊ฐ€

์‘๋‹ต ํ—ค๋”

์‘๋‹ต ํ—ค๋”

์ฟ ํ‚ค์˜ ์‚ญ์ œ ๋ณ€๊ฒฝ

Cookie cookie = new Cookie("id", ""); 
cookie.setMaxAge(0); // ์œ ํšจ๊ธฐ๊ฐ„ 0์œผ๋กœ
response.addCookie(cookie); // ์‘๋‹ต์— ์ฟ ํ‚ค ์ถ”๊ฐ€
Cookie cookie = new Cookie("id", ""); 
cookie.setValud(URLEncoder.encode("๋‚จ๊ถ์„ฑ));
cookie.setDomain("www.fastcampus.co.kr");
cookie.setPath("/ch2");
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie); // ์‘๋‹ต์— ์ฟ ํ‚ค ์ถ”๊ฐ€

์ฟ ํ‚ค ์ฝ์–ด์˜ค๊ธฐ

Cookie[] cookies = request.getCookies();
for (Cookie cookie: cookies) {
	String name = cookie.getname();
	String value = cookie.getValue();
	System.out.printf("[Cookie] name = %s, value=%s\n", name, value);
}

์ฟ ํ‚ค ์‹ค์Šต

๋กœ๊ทธ์ธ ์ •๋ณด ์ €์žฅํ•˜๊ธฐ (์•„์ด๋””๊ธฐ์–ต)

package com.fastcampus.ch2;

import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/login")
public class LoginController {
	@GetMapping("/login")
	public String loginForm() {
		return "loginForm";
	}
	@PostMapping("/login")
	public String login(String id, String pwd, boolean rememberId, HttpServletResponse response) throws Exception {
		System.out.println("id="+id);
		System.out.println("pwd="+pwd);
		System.out.println("rememberId="+rememberId);
		// 1. id์™€ pwd์„ ํ™•์ธ
		if (!loginCheck(id, pwd)) {
			// 2-1. ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด loginForm์œผ๋กœ ์ด๋™
			String msg = URLEncoder.encode("id ๋˜๋Š” pwd๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.", "utf-8");
			return "redirect:/login/login?msg=" + msg;		
		}
		// 2-2. id์™€ pwd ์ผ์น˜ํ•˜๋ฉด
		if (rememberId) {
			//			1. ์ฟ ํ‚ค ์ƒ์„ฑ
			Cookie cookie = new Cookie("id", id);
			//		2. ์‘๋‹ต์— ์ €์žฅ
			response.addCookie(cookie);
		} else {
			Cookie cookie = new Cookie("id", id);
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
		
		
		//		3. ํ™ˆ์œผ๋กœ ์ด๋™
		return "redirect:/";
	}
	private boolean loginCheck(String id, String pwd) {
		// TODO Auto-generated method stub
		return "asdf".equals(id) && "1234".equals(pwd);
	}
}
<input type="text" name="id" value = ${cookie.id.value}" placeholder="์ด๋ฉ”์ผ ์ž…๋ ฅ" autofocus>
<input type="password" name="pwd" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ">
<button>๋กœ๊ทธ์ธ</button>
<div>
    <label><input type="checkbox" name="rememberId" ${empty cookie.id.value ? "": "checked"}> ์•„์ด๋”” ๊ธฐ์–ต</label> |
    <a href="">๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐพ๊ธฐ</a> |
    <a href="">ํšŒ์›๊ฐ€์ž…</a>
</div>

์•„์ด๋””๊ธฐ์–ต ์ฒดํฌ X - ์ฟ ํ‚ค ์‚ญ์ œ

์•„์ด๋””๊ธฐ์–ต ์ฒดํฌ X - ์ฟ ํ‚ค ์‚ญ์ œ

์•„์ด๋”” ๊ธฐ์–ต ์ฒดํฌ โ†’ ์ฟ ํ‚ค ์ƒ์„ฑ (id = asdf)

์•„์ด๋”” ๊ธฐ์–ต ์ฒดํฌ โ†’ ์ฟ ํ‚ค ์ƒ์„ฑ (id = asdf)



1jeongg
Written by 1jeongg Follow

I'm studying Android development by Kotlin and Spring by Java