WIL_10์ฃผ์ฐจ

์ง€์ธยท2023๋…„ 7์›” 23์ผ
0

WIL

๋ชฉ๋ก ๋ณด๊ธฐ
10/11
post-thumbnail
post-custom-banner

๐Ÿฐ ์ต๋ช… ์ปค๋ฎค๋‹ˆํ‹ฐ ํŒ€ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ–ˆ๋‹ค. ๋กœ๊ทธ์ธ, ๋กœ๊ทธ์•„์›ƒ ๊ตฌํ˜„์„ ๋งก์•˜๋‹ค.
๋กœ๊ทธ์•„์›ƒ ๊ตฌํ˜„ํ•  ๋•Œ ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๊ต‰์žฅํžˆ ๋งŽ์•˜๋‹ค. ๊ทธ ์ค‘ ๋Œ€๋ถ€๋ถ„์ด ๋ ˆ๋””์Šค๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„์„ ํ–ˆ๋Š”๋ฐ ์ „ ํŒ€์›๋ถ„์ด ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ๋งŒ์„ ์จ์„œ ๊ตฌํ˜„์— ์„ฑ๊ณตํ•˜์…”์„œ ์ฐธ๊ณ ํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์•˜๋‹ค.

๋กœ๊ทธ์•„์›ƒ ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ

๋ฐฑ ๋ถ€๋ถ„์—์„œ๋Š” ์ž˜ ๊ตฌํ˜„์ด ๋์—ˆ๋Š”๋ฐ ํ”„๋ก ํŠธ๋ž‘ ์—ฐ๊ฒฐํ•˜๋Š”๊ฒŒ ์–ด๋ ค์› ๋‹ค. ๋‚˜ ํ˜ผ์žํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋„ ์•„๋‹ ๋ฟ๋”๋Ÿฌ ์‹œ๊ฐ„์ด ๋ถ€์กฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ๋ฐฐ์šด ๋‚ด์šฉ์œผ๋กœ ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ–ˆ๋‹ค. ๋•Œ๋ฌธ์— ๋‚˜๋Š” ๋งŽ์€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์ง€ ๋ชปํ•ด๋‹ค.

๊ทธ๋ž˜๋„ ์—†๋Š” ์‹œ๊ฐ„ ์ชผ๊ฐœ๊ฐ€๋ฉด์„œ console๊ณผ log๋ฅผ ํ•˜๋‚˜ ํ•˜๋‚˜ ์ฐ์–ด๊ฐ€๋ฉฐ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ ์ด์œ ๋ฅผ ์•Œ์•„๋ƒˆ๋‹ค.

  • AJAX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ—ค๋”์— JWT๋ฅผ ๋‹ด์•„ ์„œ๋ฒ„์— ๋กœ๊ทธ์•„์›ƒ ์š”์ฒญ์„ ๋ณด๋ƒˆ๋‹ค. (getJwtFromCookie๋Š” ์ฟ ํ‚ค์—์„œ JWT๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.)
    function logout() {
        const jwtToken = getJwtFromCookie();
        console.log(jwtToken)

        // AJAX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„์— ๋กœ๊ทธ์•„์›ƒ ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
        $.ajax({
            type: "POST",
            url: "/api/logout",
            headers: {
                'Authorization': jwtToken,
            },
            success: function (response, textStatus) {
                alert('๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
                console.log(textStatus)
                window.location.href = "/";
                deleteJwtCookie();
            },
            error: function (jqXHR, textStatus, errorThrown) {
                // ๋กœ๊ทธ์•„์›ƒ ์‹คํŒจ ์‹œ ์ฒ˜๋ฆฌ
                alert("๋กœ๊ทธ์•„์›ƒ ์‹คํŒจ");
            }
        });
        // window.location.href = "/";
        // deleteJwtCookie();
    }
  • console์— ์ฐํžˆ๋Š” jwtToken์€ Bearer%20eyJhbG.. (์ƒ๋žต) ์ด์˜€๋‹ค.

  • ์„œ๋ฒ„๋กœ ์ž˜ ๋„˜์–ด์˜ค๋Š” ๊ฒƒ๋„ ํ™•์ธํ–ˆ๋‹ค.

  • ํ•˜์ง€๋งŒ controller ์—์„œ "Bearer " ๊ณต๋ฐฑ ์ดํ›„์˜ ํ† ํฐ๊ฐ’(ey...)๋งŒ log์— ๋‚˜ํƒ€๋‚˜์•ผ ํ•˜๋Š”๋ฐ ์‹ค์ œ ์ฐํžŒ ํ† ํฐ์€ 20eyJhbGciOiJI... (์ƒ๋žต) ๊ณต๋ฐฑ์„ ํ‘œ์‹œํ•˜๋Š” ์ˆซ์ž๋„ ํ‘œ์‹œ๊ฐ€ ๋˜์–ด ์žˆ์—ˆ๋‹ค.

  • ๋•Œ๋ฌธ์— ํ† ํฐ ๊ฒ€์ฆ์—์„œ ์žกํ˜€ null ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด ํ† ํฐ์ด ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ์— ์ €์žฅ์ด ๋˜์ง€ ์•Š์•˜๋˜ ๊ฑฐ์˜€๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ substring(7)์„ 9๋กœ ๋ฐ”๊ฟ”์คฌ๋”๋‹ˆ ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ์— ์ž˜ ์ €์žฅ์ด ๋˜๋Š”๊ฑธ ํ™•์ธํ–ˆ๋‹ค.

    public String resolveToken(HttpServletRequest request) {
        String bearerToken = request.getHeader(AUTHORIZATION_HEADER);
        log.info("bearerToken : " + bearerToken);

        if (StringUtils.hasText(bearerToken)&& bearerToken.startsWith("Bearer")) {
            return bearerToken.substring(7);
        }
        return null;
    }

[์ตœ์ข…] ๋ฐฑ์˜คํ”ผ์Šค S.A

๐Ÿฐ ๋น„๋ก ํ”„๋กœ์ ํŠธ์—๋Š” ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•œ ๋กœ๊ทธ์•„์›ƒ์ด ๋“ค์–ด๊ฐ€์ง€ ๋ชปํ–ˆ์ง€๋งŒ ์ด์Šˆ์˜ ์›์ธ์„ ์•Œ์•„๋ƒˆ๊ณ  ์ž˜ ๋กœ๊ทธ์•„์›ƒ์ด ๋˜๋Š” ๊ฒƒ๋„ ํ™•์ธ์ด ๋˜์–ด์„œ ๊ธฐ๋ถ„ ์ข‹๊ฒŒ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งˆ๋ฌด๋ฆฌ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

profile
์—ด์ฉก
post-custom-banner

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

comment-user-thumbnail
2023๋…„ 7์›” 23์ผ

์ข‹์€ ์ •๋ณด ์–ป์–ด๊ฐ‘๋‹ˆ๋‹ค, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ