[Spring] Cookie ํ™œ์šฉ | @CookieValue

Jeiniยท2023๋…„ 5์›” 21์ผ
0

๐Ÿƒย  Spring

๋ชฉ๋ก ๋ณด๊ธฐ
8/33
post-thumbnail

๐Ÿ’ก @CookieValue


โœ”๏ธ ์ฟ ํ‚ค๋ฅผ ์ „๋‹ฌ ๋ฐ›๊ฒŒ ํ•ด ์ฃผ๋Š” ์• ๋„ˆํ…Œ์ด์…˜
โœ”๏ธ @CookieValue("id") String cookieID

@PostMapping("/login")
	public String login(@CookieValue("id") String cookieID, ..) {
        ...

โœ๏ธ session Id ๊ฐ€์ ธ์˜ค๊ธฐ

@PostMapping("/login")
	public String login(@CookieValue("JSESSIONID") String sessionID, ...) {

โœ๏ธ Cookie ์‹ค์Šต



๐Ÿช JSP์—์„œ ๋ฐฐ์› ๋˜ Cookie๋ฅผ Spring์—์„œ๋„ ํ™œ์šฉํ•ด ๋ณด์ž.

โœ”๏ธ ์•„์ด๋”” ๊ธฐ์–ต์„ ๋ˆ„๋ฅด๋ฉด ์ฟ ํ‚ค ์ €์žฅ
โœ”๏ธ ์•„์ด๋”” ๊ธฐ์–ต์„ ๋ˆ„๋ฅด์ง€ ์•Š์œผ๋ฉด ์ฟ ํ‚ค ์‚ญ์ œ

โœ๏ธ LoginController.java

package kr.ac.jipark09;

import java.io.UnsupportedEncodingException;
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 {
		// 1. id์™€ pwd๋ฅผ ํ™•์ธ
		if(!loginCheck(id, pwd)) {
			// 2-1. ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด, index์œผ๋กœ ์ด๋™
			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) {
		return "asdf".equals(id) && "1234".equals(pwd);
	}
}
  • rememberId๋Š” checkbox์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ›์€ ๊ฒƒ
    <checkbox> ๊ฐ€ ์ฒดํฌ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ
    : String์œผ๋กœ ๋ฐ›์„ ๊ฒฝ์šฐ โžก๏ธ on (๊ธฐ๋ณธ๊ฐ’. value๋ฅผ ์จ์„œ ๋ฐ”๊ฟ”์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.)
    : boolean์œผ๋กœ ๋ฐ›์„ ๊ฒฝ์šฐ โžก๏ธ true

โœ๏ธ loginForm.jsp

<form action="<c:url value='/login/login'/>" method="post" onsubmit="return formCheck(this);">
        <h3 id="title">Login</h3>
        <div id="msg">
	    <c:if test="${not empty param.msg}">
		<i class="fa fa-exclamation-circle"> ${URLDecoder.decode(param.msg)}</i>            
	    </c:if>        
	</div>
        <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>
        <script>
            function formCheck(frm) {
                 let msg ='';
     
                 if(frm.id.value.length == 0) {
                     setMessage('id๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.', frm.id);
                     return false;
                 }
     
                 if(frm.pwd.value.length == 0) {
                     setMessage('password๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.', frm.pwd);
                     return false;
                 }

                 return true;
            }
     
            function setMessage(msg, element){
                 document.getElementById("msg").innerHTML = ` ${'${msg}'}`;
     
                 if(element) {
                     element.select();
                 }
            }
     </script>
 </form>
  • ์•„์ด๋”” ๊ธฐ์–ต ๋ˆ„๋ฅด๊ณ  ๋‹ค์‹œ ๋กœ๊ทธ์ธ ์ฐฝ์— ๊ฐ€๋ณด๋ฉด ์•„์ด๋””๊ฐ€ ๋ฐ”๋กœ ๋„ฃ์–ด์ง„ ๊ฒƒ์„ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Reference
: https://fastcampus.co.kr/dev_academy_nks

profile
Fill in my own colorful colors๐ŸŽจ

0๊ฐœ์˜ ๋Œ“๊ธ€