개요

이번엔 스트리밍 대해서도 공부할겸 비디오 스트리밍 서버를 만들어볼겁니다. 아직 만들고있는 중이라 차근차근 하나씩 배우고 바꿔가며 만들어보겠습니다. 이번 글에서는 JavaScript를 이용해 비디오 플레이어를 만들어 보겠습니다. 아 그전에 이번 프로젝트에 대해서 설명하겠습니다.

 

 

구조

최종적으로 이런 구조로 만들어보고싶은데 가능할지는.. 잘 모르겠습니다. 하나하나 해보면서 최대한 해봐야죠.

 

이렇게 구성한 이유는 몇가지가 있습니다.

 

  1. 사용자가 동영상을 업로드하면 동영상을 변환하는 과정은 비동기로 처리하도록 했습니다.
    • 720p 10분짜리의 영상을 변환하는데 약 3-4분 정도 걸렸고, 서버에 부담을 많이 되어 비동기로 작업을 진행했습니다.
    • 비동기로 작업했을 때 3개 이상의 작업을 동시에 처리하면 서버부하가 걸리는 것을 확인했고, Thread를 제한했습니다.
    • 단일 서버에서 고작 2개의 동영상만 동시에 업로드 된다는 것은 말이 안되기에 변환 작업을 담당하는 서버를 두어 동시에 여러 데이터를 처리하도록 구조를 변경했습니다.
    • 유저는 업로드 '요청'을 하면 DB에서 동영상이 업로드 되었는지 여부를 지속적으로 확인하여 업로드가 완료되었는지 실패되었는지 확인할 수 있습니다.
  2. Storage는 아직 공부를 하지 못한 부분이지만 우선 위의 구현이 끝나면 RAID(?) 뭐 그걸로 해서 실제로 구축해보려고합니다.

 

적응형 스트리밍

이번 프로젝트에서는 비디오를 실행할 적응형 스트리밍에 대해서 알고 넘어가야합니다. 적응형 스트리밍은 인터넷 환경에서 끊김 없는 비디오 스트리밍을 제공하기 위한 기술입니다. 단순히 비디오를 HTTP로 다운로드를 해서 재생하는 것과는 달리 네트워크 상태에 따라 동적으로 비디오 품질을 조절할 수 있습니다.

 

 

무엇이 다른건가?

기존의 방식은 하나의 동영상 데이터가 선택되어 다운로드를 하며 플레이 하는 방식입니다.

만약 유저의 네트워크 상태가 좋지 않다면 동영상을 제대로 시청할 수 없게 될 수 있습니다. 또한 동영상의 길이가 30분이라고 했을때 유저가 1분만에 페이지를 나가거나 동영상을 넘겨 20분부터 시청한다면 시청하지 않은 비디오의 데이터가 낭비되는 문제가 생길 수 있겠죠. 

이런 문제를 해결하기 위해 적응형 스트리밍 기술이 만들어졌습니다.

 

 

 

특징

 

컨텐츠 보호

적응형 스트리밍 기술은 동영상을 인코딩 하여 사용되기 때문에 원본 동영상을 사용하지 않습니다.

 

동영상 분할

동영상을 작은 조각으로 나누어서 사용됩니다. 이를 세그먼트(조각) 분할이라고 합니다.

  • 하나의 비디오 파일을 여러 개의 작은 조각(세그먼트)으로 나눔
  • 보통 2-10초 길이의 세그먼트 사용
  • 각 세그먼트는 독립적으로 재생 가능

품질 인코딩

  • 동일한 영상을 여러 가지 품질(비트레이트)로 인코딩
  • 예: 1080p, 720p, 480p, 360p 등
  • 각 품질별로 세그먼트 생성

매니페스트 파일

  • 모든 품질의 세그먼트 정보를 담은 인덱스 파일
  • 재생 가능한 품질 목록, 세그먼트 URL, 재생 시간 등 포함
  • 프로토콜별로 다른 형식 사용 (HLS: .m3u8, DASH: .mpd)

동적 품질 전환

  • 클라이언트가 네트워크 상태 모니터링
  • 대역폭에 따라 적절한 품질의 세그먼트 요청
  • 재생 중에도 끊김 없이 품질 전환 가능

 

HLS와 DASH

현재 널리 사용되는 적응형 스트리밍 프로토콜입니다.

  1. HLS (HTTP Live Streaming)
    • Apple이 개발한 프로토콜
    • iOS 기기와의 호환성이 뛰어남
    • 가장 널리 사용되는 스트리밍 프로토콜
    • .m3u8 재생목록 파일과 .ts 세그먼트 파일 사용
  2. DASH (Dynamic Adaptive Streaming over HTTP)
    • MPEG에서 표준화한 프로토콜
    • Netflix, YouTube 등에서 사용
    • .mpd 매니페스트 파일과 .mp4 세그먼트 사용

 

구현하기

이제 비디오 플레이어를 만들어봅시다.

 

Hls.js

저는 HLS를 사용할겁니다. 웹에서 HLS 를 다루기 위해서는 Hls.js 를 사용해야 합니다.

 

GitHub - video-dev/hls.js: HLS.js is a JavaScript library that plays HLS in browsers with support for MSE.

HLS.js is a JavaScript library that plays HLS in browsers with support for MSE. - video-dev/hls.js

github.com

<script src="https://cdn.jsdelivr.net/npm/hls.js@1"></script>

이렇게 넣고 시작합시다.

 

 

기본적인 사용방법은 다음과 같습니다.

if (!Hls.isSupported()) return; // Hls 지원 여부
const hls = new Hls({
    debug: true,  // 디버깅 여부
    playlistLoadPolicy: {
        default: {
            // 첫 번째 바이트를 받을 때까지 기다리는 최대 시간 (10초)
            maxTimeToFirstByteMs: 10000,

            // 전체 로딩이 완료될 때까지 기다리는 최대 시간 (10초)
            maxLoadTimeMs: 10000,

            // 타임아웃 발생 시 재시도 설정
            timeoutRetry: {
                maxNumRetry: 2,      // 최대 2번 재시도
                retryDelayMs: 0,     // 재시도 사이의 대기 시간 없음
                maxRetryDelayMs: 0   // 최대 대기 시간 제한 없음
            },

            // 에러 발생 시 재시도 설정
            errorRetry: {
                maxNumRetry: 3,      // 최대 3번 재시도
                retryDelayMs: 1000,  // 재시도 전 1초 대기
                maxRetryDelayMs: 8000 // 재시도 간 최대 대기 시간 8초
            }
        }
    }
});

hls.loadSource(document.querySelector('#videoPlayer'));
hls.attachMedia('/video/asdf.m3u8');

 

 

먼저 Hls 가 지원되는지 확인하고 Hls 설정해줍니다. 설정할게 없다면 new Hls() 이렇게만 해도 됩니다.

loadSource에 <video></video> 요소를 넣어주고 attachMedia에 플레이리스트 경로를 넣어주면 됩니다. 

 

 

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Video Player</title>
    <link rel="stylesheet" href="/css/video.css">
    <script src="https://cdn.jsdelivr.net/npm/hls.js@1"></script>
    <script type="module" src="/js/video.js"></script>

</head>
<body>
<div class="video-container">
    <div class="video-wrapper">
        <video id="videoPlayer" crossorigin="anonymous" aria-id=""></video>
        <div class="thumbnail-box">
            <img class="thumbnail-image" src="" alt="">
            <button type="button" id="playButton">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512">
                    <path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80L0 432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"></path>
                </svg>
            </button>
        </div>
        <div class="panel disabled">
            <div class="play-pause-event">
                <svg class="animate-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80L0 432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg>
            </div>
            <div class="loading-spinner disabled">
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
                    <path d="M304 48a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zm0 416a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zM48 304a48 48 0 1 0 0-96 48 48 0 1 0 0 96zm464-48a48 48 0 1 0 -96 0 48 48 0 1 0 96 0zM142.9 437A48 48 0 1 0 75 369.1 48 48 0 1 0 142.9 437zm0-294.2A48 48 0 1 0 75 75a48 48 0 1 0 67.9 67.9zM369.1 437A48 48 0 1 0 437 369.1 48 48 0 1 0 369.1 437z"/>
                </svg>
            </div>
            <div class="controller">
                <div class="timeline">
                    <div class="buffer-progress"></div>
                    <div class="progress"></div>
                </div>
                <div class="control-box">
                    <div class="left-control-box">
                        <button class="play-pause" aria-keyshortcuts="space">
                            <svg class="animate-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80L0 432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg>
                        </button>
                        <div class="time-display">0:00 / 0:00</div>
                        <div class="volume-container">
                            <button class="volume-btn">
                                <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polygon points="11 5 6 9 2 9 2 15 6 15 11 19 11 5"></polygon><line x1="23" y1="9" x2="17" y2="15"></line><line x1="17" y1="9" x2="23" y2="15"></line></svg>
                            </button>
                            <div class="volume-slider">
                                <div class="volume-progress"></div>
                            </div>
                        </div>
                    </div>
                    <div class="right-control-box">
                        <div class="quality-container">
                            <button class="quality-btn"></button>
                            <div class="quality-options disabled"></div>
                        </div>
                        <button class="fullscreen">
                            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M136 32c13.3 0 24 10.7 24 24s-10.7 24-24 24L48 80l0 88c0 13.3-10.7 24-24 24s-24-10.7-24-24L0 56C0 42.7 10.7 32 24 32l112 0zM0 344c0-13.3 10.7-24 24-24s24 10.7 24 24l0 88 88 0c13.3 0 24 10.7 24 24s-10.7 24-24 24L24 480c-13.3 0-24-10.7-24-24L0 344zM424 32c13.3 0 24 10.7 24 24l0 112c0 13.3-10.7 24-24 24s-24-10.7-24-24l0-88-88 0c-13.3 0-24-10.7-24-24s10.7-24 24-24l112 0zM400 344c0-13.3 10.7-24 24-24s24 10.7 24 24l0 112c0 13.3-10.7 24-24 24l-112 0c-13.3 0-24-10.7-24-24s10.7-24 24-24l88 0 0-88z"/></svg>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>

 

비디오 플레이어의 디자인은 유튜브 플레이어를 참고해서 만들었습니다. <video></video> 를 덮는 panel을 두고 동영상을 조작할 수 있도록 만들었습니다.

 

 

CSS

.video-container * {
    box-sizing: border-box;
}

.video-container button {
    background: none;
    border: none;
    cursor: pointer;
    color: white;

    width: 30px;
    height: 30px;
    padding: 5px;
}
.video-container .disabled {
    display: none;
}
.video-container button > svg {
    pointer-events: none;
    fill: white;
}
.video-container svg {
    width: 20px;;
    height: 20px;
}

.video-container {
    max-width: 800px;
    margin: 20px auto;
    overflow: hidden;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.video-wrapper {
    position: relative;
    width: 100%;
}

#videoPlayer {
    width: 100%;
    display: block;
}

.thumbnail-box {
    position: absolute;
    top: 0; bottom: 0;
    left: 0; right: 0;
}
.thumbnail-box img {
    width: 100%;
    height: 100%;
}
#playButton {
    position: absolute;
    transform: translate(-50%, -50%);
    top: 50%; left: 50%;
    width: 60px; height: 60px;
    border-radius: 100%;
    background-color: #ec2020;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

#playButton:hover {
    background-color: #d81c1c;
}

.panel {
    position: absolute;
    width: 100%;
    height: 100%;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(to top,
        rgba(0, 0, 0, 0.7) 0%,
        rgba(0, 0, 0, 0.2) 40%,
        rgba(0, 0, 0, 0) 100%
    );
    opacity: 0;
    transition: opacity 0.3s;
    align-content: end;
}

.panel:hover, .panel.active {
    opacity: 1;
}

.controller {
    padding: 20px 10px 5px 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.timeline {
    position: relative;
    height: 5px;
    background: rgba(255, 255, 255, 0.3);
    cursor: pointer;
    border-radius: 3px;
    overflow: hidden;
    transition: height 0.15s ease-in-out;
}
.timeline:hover {
    height: 8px;
}

/* 로드된 세그먼트를 표시할 요소 추가 */
.buffer-progress {
    position: absolute;
    height: 100%;
    background: rgba(255, 255, 255, 0.4);
    border-radius: 3px;
    width: 0;
}

.progress {
    position: absolute;
    height: 100%;
    background: #ff0000;
    border-radius: 3px;
    width: 0;
    z-index: 1;  /* 버퍼 프로그레스 위에 표시 */
}



.control-box{
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.left-control-box, .right-control-box {
    display: flex;
    align-items: center;
    gap: 10px;
}

.volume-container {
    display: flex;
    align-items: center;
}
.volume-container:hover .volume-slider {
    width: 60px;
}

.volume-slider {
    transition: width 0.2s ease-in-out;
    width: 0px;
    height: 5px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 3px;
    cursor: pointer;
}

.volume-progress {
    height: 100%;
    background: #fff;
    border-radius: 3px;
    width: 100%;
}

.time-display {
    color: white;
    font-size: 14px;
    text-align: center;
}

.quality-container {
    position: relative;
}

.quality-btn {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    padding: 5px 10px;
    font-size: 14px;
    width: initial !important;
}

.quality-options {
    display: block;
    position: absolute;
    bottom: 100%;
    right: 0;
    background: rgba(0, 0, 0, 0.9);
    border-radius: 4px;
    padding: 5px 0;
    margin-bottom: 5px;
    z-index: 2;
}

.quality-options button {
    display: block;
    width: 100%;
    padding: 5px 20px;
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    text-align: left;
    white-space: nowrap;
}

.quality-options button:hover {
    background: rgba(255, 255, 255, 0.1);
}

.animate-icon {
    animation: iconFade 0.3s ease-out;
}

@keyframes iconFade {
    from {
        transform: scale(0.9);
        opacity: 0.5;
    }
    to {
        transform: scale(1);
        opacity: 1;
    }
}

.play-pause-event {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    opacity: 0;
    pointer-events: none;
    background: rgba(0, 0, 0, 0.4);
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 100%;
}
.play-pause-event svg {
    fill: white;
}
.play-pause-event.active {
    animation: scaleAndFade 0.7s ease-out forwards;
}

@keyframes scaleAndFade {
    0% {
        transform: translate(-50%, -50%) scale(1);
        opacity: 1;
    }

    100% {
        transform: translate(-50%, -50%) scale(1.7);
        opacity: 0;
    }

}

.loading-spinner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.loading-spinner svg {
    width: 30px;
    height: 30px;
    fill: white;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

 

 

JavaScript

const KEYS = {
    NEXT : 'ArrowRight',
    PREV : 'ArrowLeft',
    PLAY_PAUSE : 'Space',
    FULL_SCREEN : 'KeyF'
}
const DISABLED = 'disabled';

class Video {

    constructor(videoElement, playlistUrl, panelElement) {
        if (!this.#isSupported()) return;
        this.video = videoElement;
        this.playlistUrl = playlistUrl;
        this.loadingSpinner = document.querySelector('.loading-spinner');

        this.#setHls();
        this.controller = new Controller(this.hls, this.video, this.loadingSpinner, panelElement);
        this.#addLoadingEvent();
    }

    setPlayButton(thumbnailBoxElement, playButtonElement) {
        this.thumbnail = thumbnailBoxElement;
        this.playButton = playButtonElement;
    }

    #addLoadingEvent() {
        // 로딩 시작
        this.video.addEventListener('waiting', () => {
            this.loadingSpinner.classList.remove(DISABLED);
            this.controller.panel.classList.add('active');
        });

        // 로딩 완료
        this.video.addEventListener('canplay', () => {
            this.loadingSpinner.classList.add(DISABLED);
            this.controller.panel.classList.remove('active');
        });

    }
    #isSupported() {
        return Hls.isSupported();
    }
    #setHls() {
        this.hls = new Hls();
    }

    #initPlayButtonEvent() {
        this.thumbnail.remove();
        this.controller.panel.classList.remove(DISABLED);
        this.play();
    }

    initialEventListener() {
        this.controller.initialDependenciesEventListener();
    }

    async autoPlay() {
        this.hls.loadSource(this.playlistUrl);
        this.hls.attachMedia(this.video);

        if (await this.play()) {
            this.thumbnail.remove();
            this.controller.panel.classList.remove(DISABLED);
        } else {
            this.playButton.addEventListener('click', () => {
                this.#initPlayButtonEvent();
            }, {once : true})
        }
    }

    async play() {
        return await this.controller.playPause.play();
    }

    pause() {
        this.controller.playPause.pause();
    }

}

class Controller {

    constructor(hls, video, loadingSpinner, panelElement) {
        this.hls = hls;
        this.video = video;
        this.loadingSpinner = loadingSpinner;
        this.panel = panelElement;
    }

    setQuality(qualityBtnElement, qualityOptionsBox) {
        this.quality = new Quality(qualityBtnElement, qualityOptionsBox);

        // 화질 최초 선택
        this.hls.on(Hls.Events.MANIFEST_PARSED, (event, data) => {
            console.log('Available qualities:', data.levels);

            // 사용 가능한 화질 옵션 생성
            this.quality.createQualityOptions(data.levels);
            this.quality.addEventChangeQuality(this.hls, this.playPause, this.loadingSpinner);

            // 현재 화질 레벨 설정
            let currentLevel = this.hls.currentLevel;
            this.quality.updateQualityButton(currentLevel, data.levels);

            // 자동 화질 선택 모드 설정
            this.hls.currentLevel = -1;
        });

        // 화질 변경 이벤트 처리
        this.hls.on(Hls.Events.LEVEL_SWITCHED, (event, data) => {
            this.quality.updateQualityButton(data.level, data.levels);
        });

    }
    setVolume(volumeBtn, volumeSlider, volumeProgress) {
        this.volume = new Volume(this.video, volumeBtn, volumeSlider, volumeProgress);
    }
    setPlayPauseBtn(playPauseBtnElement, playPauseEventElement) {
        this.playPause = new PlayPause(this.video, this.panel, playPauseBtnElement, playPauseEventElement);
    }
    setFullScreen(fullscreenBtnElement) {
        this.fullScreen = new FullScreen(this.video, fullscreenBtnElement);
    }
    setTimeline(timelineElement, progressElement, timeDisplay) {
        this.timeline = new Timeline(this.video, timelineElement, progressElement, timeDisplay);
    }

    initialDependenciesEventListener() {
        this.timeline.setDraggingEvent(this.playPause);
    }

    setKeyShortCuts() {
        document.addEventListener('keydown', (e) => {
            // 입력 필드에 포커스가 있을 때는 단축키 비활성화
            if (document.activeElement.tagName === 'INPUT' ||
                document.activeElement.tagName === 'TEXTAREA') {
                return;
            }
            this.playPause.keyShortcuts(e,10, 10);
            this.fullScreen.keyShortcuts(e);
        })

    }
}
class PlayPause {

    constructor(video, panel, playPauseBtnElement, playPauseEventElement) {
        this.video = video;
        this.panel = panel;
        this.playPauseBtn = playPauseBtnElement;
        this.playPauseEvent = playPauseEventElement;

        this.#addEvent();
    }

    #addEvent() {
        this.playPauseBtn.addEventListener('click', () => {
            this.#playPauseToggle();
        });

        this.panel.addEventListener('click', (e) => {
            if (e.target !== this.panel) return;
            this.#playPauseToggle();
            this.#showEventAnimation();
        })

        // 비디오 종료 시 reload 버튼 변경
        this.video.addEventListener('ended', () => {
            this.playPauseBtn.innerHTML = this.getReloadSvg();
        });
    }

    /**
     * @param e
     * @param increaseAmount : number n초 후로 비디오 타임라인 이동
     * @param decreaseAmount : number n초 전으로 비디오 타임라인 이동
     */
    keyShortcuts(e, increaseAmount, decreaseAmount) {
        if (e.code === KEYS.NEXT) {
            // 오른쪽 화살표 (n초 앞으로)
            this.video.currentTime = Math.min(this.video.duration, this.video.currentTime + increaseAmount);
        } else if (e.code === KEYS.PREV) {
            // 왼쪽 화살표 (n초 뒤로)
            this.video.currentTime = Math.max(0, this.video.currentTime - decreaseAmount);
        } else if (e.code === KEYS.PLAY_PAUSE) {
            // 시작, 일시정지
            this.#playPauseToggle();
        }
    }

    #playPauseToggle() {
        this.isPaused() ? this.play() : this.pause();
    }

    #showEventAnimation() {
        // 이전 타이머가 있다면 정리
        if (this.activeTimer) {
            clearTimeout(this.activeTimer);
            this.playPauseEvent.classList.remove('active');
            // 강제 리플로우 발생
            void this.playPauseEvent.offsetWidth;
        }

        this.playPauseEvent.classList.add('active');

        // 애니메이션이 700ms 이기 때문에 700으로 설정
        this.activeTimer = setTimeout(() => {
            this.playPauseEvent.classList.remove('active');
            this.activeTimer = null;
        }, 700);
    }

    async play() {
        try {
            let isPlay = await this.video.play();
            this.playPauseBtn.innerHTML = this.getPauseSvg();
            this.playPauseEvent.innerHTML = this.getPauseSvg();
            return isPlay;
        } catch (e) {
            return false;
        }
    }

    pause() {
        this.video.pause();
        this.playPauseBtn.innerHTML = this.getPlaySvg();
        this.playPauseEvent.innerHTML = this.getPlaySvg();
    }

    isPaused() {
        return this.video.paused;
    }

    getPauseSvg() {
        return '<svg class="animate-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path d="M48 64C21.5 64 0 85.5 0 112L0 400c0 26.5 21.5 48 48 48l32 0c26.5 0 48-21.5 48-48l0-288c0-26.5-21.5-48-48-48L48 64zm192 0c-26.5 0-48 21.5-48 48l0 288c0 26.5 21.5 48 48 48l32 0c26.5 0 48-21.5 48-48l0-288c0-26.5-21.5-48-48-48l-32 0z"/></svg>';
    }
    getPlaySvg() {
        return '<svg class="animate-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80L0 432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg>';
    }
    getReloadSvg() {
        return '<svg class="animate-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M463.5 224H472c13.3 0 24-10.7 24-24V72c0-9.7-5.8-18.5-14.8-22.2s-19.3-1.7-26.2 5.2L413.4 96.6c-87.6-86.5-228.7-86.2-315.8 1c-87.5 87.5-87.5 229.3 0 316.8s229.3 87.5 316.8 0c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0c-62.5 62.5-163.8 62.5-226.3 0s-62.5-163.8 0-226.3c62.2-62.2 162.7-62.5 225.3-1L327 183c-6.9 6.9-8.9 17.2-5.2 26.2s12.5 14.8 22.2 14.8H463.5z"/></svg>';
    }

}
class Quality {

    constructor(qualityBtnElement, qualityOptionsBox) {
        this.qualityBtn = qualityBtnElement;
        this.qualityOptionsBox = qualityOptionsBox;

        this.#addEvent();
    }

    createQualityOptions(levels) {
        this.qualityOptionsBox.innerHTML = '';

        // AUTO 옵션 추가
        const autoButton = document.createElement('button');
        autoButton.setAttribute('data-quality', '-1');
        autoButton.textContent = 'AUTO';
        this.qualityOptionsBox.appendChild(autoButton);

        // 높은 화질부터 추가
        for (let i = levels.length - 1; i >= 0; i--) {
            const level = levels[i];
            const resolution = level.height + 'p';
            const button = document.createElement('button');
            button.setAttribute('data-quality', i.toString());
            button.textContent = resolution;
            this.qualityOptionsBox.appendChild(button);
        }
    }

    // 화질 버튼 텍스트 업데이트

    updateQualityButton(level, levels) {
        if (level === -1) {
            this.qualityBtn.textContent = 'AUTO';
        } else if (levels && level < levels.length) {
            this.qualityBtn.textContent = levels[level].height + 'p';
        }
        this.qualityOptionsBox.classList.add(DISABLED);
    }

    #addEvent() {
        // 선택 후 박스 감춤 이벤트
        this.qualityBtn.addEventListener('click', () => {
            this.qualityOptionsBox.classList.toggle(DISABLED);
        })
        document.addEventListener('click', (e) => {
            if (e.target !== this.qualityOptionsBox && e.target !== this.qualityBtn) {
                this.qualityOptionsBox.classList.add(DISABLED);
            }
        })
    }

    addEventChangeQuality(hls, playPause, loadingSpinner) {
        const qualityOptions = this.qualityOptionsBox.querySelectorAll('button');
        qualityOptions.forEach(button => {
            button.addEventListener('click', () => {
                const quality = parseInt(button.getAttribute('data-quality'));

                if (quality === hls.currentLevel) {
                    this.qualityOptionsBox.classList.add(DISABLED);
                    return;
                }

                loadingSpinner.classList.remove(DISABLED);

                if (quality === -1) {
                    // AUTO 모드 활성화
                    hls.currentLevel = -1;
                    hls.loadLevel = -1;
                    hls.config.startLevel = -1;  // 자동 품질 선택
                } else {
                    // 수동으로 특정 품질 선택
                    hls.currentLevel = quality;
                    hls.loadLevel = quality;
                }

                this.updateQualityButton(quality, hls.levels);

                const onFragLoaded = (event, data) => {
                    loadingSpinner.classList.add(DISABLED);
                    hls.off(Hls.Events.FRAG_LOADED, onFragLoaded);
                };

                hls.on(Hls.Events.FRAG_LOADED, onFragLoaded);
            });
        });
    }



}
class Volume {

    constructor(video, volumeBtn, volumeSlider, volumeProgress) {
        this.video = video;
        this.volumeBtn = volumeBtn;
        this.volumeSlider = volumeSlider;
        this.volumeProgress = volumeProgress;
        // 드래그를 통한 볼륨 조절 기능
        this.isDraggingVolume = false;

        this.addEvent();

    }

    #loadLocalVolumeData() {
        const savedVolume = localStorage.getItem('videoVolume');
        if (savedVolume !== null) {
            this.video.volume = parseFloat(savedVolume);
            this.#drawVolumeProgress(this.video.volume);
        }
        this.updateVolumeIcon(this.video.volume);
    }

    addEvent() {
        // Initialize volume display
        this.video.addEventListener('loadedmetadata', () => {
            this.#loadLocalVolumeData();
        });

        // 볼륨 음소거 토글
        this.volumeBtn.addEventListener('click', () => {
            this.video.muted = !this.video.muted;
            this.updateVolumeIcon(this.video.muted ? 0 : this.video.volume);
            this.#drawVolumeProgress(this.video.muted ? 0 : this.video.volume);
        });

        // 볼륨 조절
        this.volumeSlider.addEventListener('click', (e) => {
            const pos = this.#getPos(e);
            this.video.volume = pos;
            this.#drawVolumeProgress(pos);
            this.updateVolumeIcon(pos);
        });

        // 볼륨 슬라이더 드래그 이벤트
        this.volumeSlider.addEventListener('mousedown', (e) => {
            this.isDraggingVolume = true;
            this.handleVolumeChange(e);
        });

        document.addEventListener('mousemove', (e) => {
            if (this.isDraggingVolume) this.handleVolumeChange(e);
        });

        document.addEventListener('mouseup', () => {
            this.isDraggingVolume = false;
        });
    }

    // 볼륨 아이콘 업데이트
    updateVolumeIcon(volume) {
        const volumeLevel = Math.floor(volume * 100);
        if (volumeLevel === 0) {
            this.volumeBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M301.1 34.8C312.6 40 320 51.4 320 64l0 384c0 12.6-7.4 24-18.9 29.2s-25 3.1-34.4-5.3L131.8 352 64 352c-35.3 0-64-28.7-64-64l0-64c0-35.3 28.7-64 64-64l67.8 0L266.7 40.1c9.4-8.4 22.9-10.4 34.4-5.3zM425 167l55 55 55-55c9.4-9.4 24.6-9.4 33.9 0s9.4 24.6 0 33.9l-55 55 55 55c9.4 9.4 9.4 24.6 0 33.9s-24.6 9.4-33.9 0l-55-55-55 55c-9.4 9.4-24.6 9.4-33.9 0s-9.4-24.6 0-33.9l55-55-55-55c-9.4-9.4-9.4-24.6 0-33.9s24.6-9.4 33.9 0z"/></svg>';
        } else if (volumeLevel <= 50) {
            this.volumeBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M301.1 34.8C312.6 40 320 51.4 320 64l0 384c0 12.6-7.4 24-18.9 29.2s-25 3.1-34.4-5.3L131.8 352 64 352c-35.3 0-64-28.7-64-64l0-64c0-35.3 28.7-64 64-64l67.8 0L266.7 40.1c9.4-8.4 22.9-10.4 34.4-5.3zM412.6 181.5C434.1 199.1 448 225.9 448 256s-13.9 56.9-35.4 74.5c-10.3 8.4-25.4 6.8-33.8-3.5s-6.8-25.4 3.5-33.8C393.1 284.4 400 271 400 256s-6.9-28.4-17.7-37.3c-10.3-8.4-11.8-23.5-3.5-33.8s23.5-11.8 33.8-3.5z"/></svg>';
        } else {
            this.volumeBtn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M333.1 34.8C312.6 40 320 51.4 320 64l0 384c0 12.6-7.4 24-18.9 29.2s-25 3.1-34.4-5.3L131.8 352 64 352c-35.3 0-64-28.7-64-64l0-64c0-35.3 28.7-64 64-64l67.8 0L266.7 40.1c9.4-8.4 22.9-10.4 34.4-5.3zm172 72.2c43.2 35.2 70.9 88.9 70.9 149s-27.7 113.8-70.9 149c-10.3 8.4-25.4 6.8-33.8-3.5s-6.8-25.4 3.5-33.8C507.3 341.3 528 301.1 528 256s-20.7-85.3-53.2-111.8c-10.3-8.4-11.8-23.5-3.5-33.8s23.5-11.8 33.8-3.5zm-60.5 74.5C466.1 199.1 480 225.9 480 256s-13.9 56.9-35.4 74.5c-10.3 8.4-25.4 6.8-33.8-3.5s-6.8-25.4 3.5-33.8C425.1 284.4 432 271 432 256s-6.9-28.4-17.7-37.3c-10.3-8.4-11.8-23.5-3.5-33.8s23.5-11.8 33.8-3.5z"/></svg>';
        }
    }

    handleVolumeChange(e) {
        let pos = this.#getPos(e);
        this.video.volume = pos;
        this.#drawVolumeProgress(pos);
        this.updateVolumeIcon(pos);

        // 볼륨 설정 저장
        localStorage.setItem('videoVolume', this.video.volume);
    }

    #drawVolumeProgress(volume) {
        this.volumeProgress.style.width = (volume * 100) + '%';
    }

    #getPos(e) {
        const rect = this.volumeSlider.getBoundingClientRect();
        let pos = (e.clientX - rect.left) / rect.width;
        return Math.max(0, Math.min(1, pos)); // Clamp between 0 and 1
    }


}
class Timeline {

    constructor(video, timelineElement, progressElement, timeDisplay) {
        this.video = video;
        this.timeline = timelineElement;
        this.progress = progressElement;
        this.bufferProgress = this.timeline.querySelector('.buffer-progress');
        this.timeDisplay = timeDisplay;
        this.isDraggingProgress = false;
        this.currentTimelineLocation = 0;
        this.#addEvent();
    }

    setDraggingEvent(playPause) {

        // 버퍼링 상태 업데이트
        this.video.addEventListener('progress', () => {
            this.#updateBufferProgress();
        });

        // 타임라인 드래그 이벤트
        this.timeline.addEventListener('mousedown', (e) => {
            this.isDraggingProgress = true;
            playPause.pause();
            this.currentTimelineLocation = this.#handleProgressChange(e);
        });

        document.addEventListener('mousemove', (e) => {
            if (this.isDraggingProgress) {
                this.currentTimelineLocation = this.#handleProgressChange(e);
                this.percentage(this.currentTimelineLocation, this.video.duration);
            }
        });

        document.addEventListener('mouseup', () => {
            if (this.isDraggingProgress) {
                this.isDraggingProgress = false;
                this.video.currentTime = this.currentTimelineLocation;
                playPause.play();
            }
        });
    }

    #addEvent() {
        // 타임라인 클릭
        this.timeline.addEventListener('click', (e) => {
            this.video.currentTime = this.#handleProgressChange(e);
        });

        // 타임라인 업데이트
        this.video.addEventListener('timeupdate', () => {
            this.percentage(this.video.currentTime,this.video.duration);
        });


    }

    #handleProgressChange(e) {
        const rect = this.timeline.getBoundingClientRect();
        const pos = (e.clientX - rect.left) / rect.width;
        return pos * this.video.duration;
    }

    #updateBufferProgress() {
        if (this.video.buffered.length > 0) {
            const bufferedEnd = this.video.buffered.end(this.video.buffered.length - 1);
            const duration = this.video.duration;
            this.bufferProgress.style.width = ((bufferedEnd / duration) * 100) + '%';
        }
    }

    percentage(current, total) {
        this.timeDisplay.textContent = `${this.#formatTime(current)} / ${this.#formatTime(total)}`;
        this.progress.style.width = (current / total) * 100 + '%';
    }

    // 시간 포맷팅
    #formatTime(seconds) {
        if (isNaN(seconds)) return '--:--';
        const minutes = Math.floor(seconds / 60);
        seconds = Math.floor(seconds % 60);
        return `${minutes}:${seconds.toString().padStart(2, '0')}`;
    }
}
class FullScreen {

    constructor(video, fullscreenBtn) {
        this.video = video;
        this.fullScreen = fullscreenBtn;
        this.#addEvent();
    }

    #addEvent() {
        this.fullScreen.addEventListener('click', () => this.#toggleFullScreen());
    }

    keyShortcuts(e) {
        if (e.code === KEYS.FULL_SCREEN) {
            this.#toggleFullScreen();
        }
    }

    #toggleFullScreen() {
        document.fullscreenElement ? this.#fullScreenExit() : this.#fullScreen();
    }
    #fullScreen() {
        this.video.parentElement.requestFullscreen();
    }
    #fullScreenExit() {
        document.exitFullscreen();
    }

}

window.addEventListener('load', () => {
    const videoElement = document.querySelector('#videoPlayer');
    const videoId = videoElement.getAttribute('aria-id');
    const playlistUrl = `/videos/${videoId}/master.m3u8`;

    const video = new Video(
        videoElement,
        playlistUrl,
        document.querySelector('.panel')
    );

    video.setPlayButton(
        document.querySelector('.thumbnail-box'),
        document.querySelector('#playButton')
    );
    video.controller.setQuality(
        document.querySelector('.quality-btn'),
        document.querySelector('.quality-options')
    )
    video.controller.setPlayPauseBtn(
        document.querySelector('.play-pause'),
        document.querySelector('.play-pause-event')
    );
    video.controller.setVolume(
        document.querySelector('.volume-btn'),
        document.querySelector('.volume-slider'),
        document.querySelector('.volume-progress')
    )
    video.controller.setFullScreen(
        document.querySelector('.fullscreen')
    );
    video.controller.setTimeline(
        document.querySelector('.timeline'),
        document.querySelector('.progress'),
        document.querySelector('.time-display')
    )
    video.controller.setKeyShortCuts();
    video.initialEventListener();
    video.autoPlay();
});

 

JS만 거의 600줄 정도 됩니다. 최대한 깔끔하게 만들려고 Class를 만들어 관리하도록 해보았습니다. HTML, CSS는 쉬웠는데 JS가 쉽지않더라구요. Class를 만들기 전에는 800줄 정도였습니다 ㄷㄷ 더 복잡했었기도하고..

 

 

Chrome 자동재생 정책

비디오 플레이어를 만들면서 하나 알아낸 사실은 크롬에는 비디오 자동재생 정책이란게 존재한다는 것을 알았습니다. 원하지 않는 스팸성 광고를 차단하기 위한 정책인데요. 동영상이 음소거상태이거나, 이전에 유저와 상호작용을 했다거나(재생버튼 클릭으로 동영상 재생), 이전에 미디어를 소비한 것을 바탕으로 점수를 합산해 높은 점수를 받은(신뢰할 수 있는) 사이트에서는 자동재생을 허용하도록 한다고합니다.

 

about://media-engagement 여기에 들어가보면 그 스코어를 확인할 수 있고, 유튜브 같은 경우 신뢰할 수 있는 사이트로 분류되어 음소거 되지않은 영상을 자동재생하는 것이 가능합니다.

 

 

 

Chrome의 자동재생 정책  |  Blog  |  Chrome for Developers

Chrome의 새로운 자동재생 정책을 통해 우수한 사용자 경험을 위한 권장사항을 알아보세요.

developer.chrome.com

자세한 내용은 여기에서 확인해보세요.

 

 

 

마치며

비디오 플레이어는 Hls만 연결해서 재생하는 것밖에 안하기 때문에 별로 중요하지 않아 자세한 설명은 하지 않고 마치겠습니다. 

 

 

 

 

[Spring Boot] 선착순 이벤트 쿠폰을 만들어보자 (1) - 동시성 이슈

들어가기 전에 우아한테크톡의 선착순 이벤트 서버 생존기를 보고 아이디어를 얻었고, 그 과정을 처음부터 부딪히면서 배우기 위해 바닥부터 만들어보면서 느낀점과 배운것들을 정리하기 위

tmd8633.tistory.com

 

[Spring Boot] 선착순 이벤트 쿠폰을 만들어보자 (2) - Redis

[Spring Boot] 선착순 이벤트 쿠폰을 만들어보자 (1) - 동시성 이슈들어가기 전에 우아한테크톡의 선착순 이벤트 서버 생존기를 보고 아이디어를 얻었고, 그 과정을 처음부터 부딪히면서 배우기 위

tmd8633.tistory.com

이전 글을 읽어보시기 바랍니다. 이전 글에서 이어집니다.

 

 

개요

이전 글에서 선착순 이벤트 쿠폰 발급에 대해서 공부했습니다. 이는 오늘 같이 공부해볼 내용을 하기 위해서 선행학습에 불과합니다. 파트별로 공부하기 위해서 구현해보았던 것이죠. 오늘은 이벤트 주문에 대해서 공부해볼겁니다.

 

큐넷

큐넷 시험신청이나 수강신청을 할 때 이런 대기열을 보셨을겁니다. 이걸 만들어볼겁니다. 이전 글에서 Redis를 활용하여 대기열을 만들었었는데 그걸 이용하면 금방 구현해볼 수 있을 것같습니다.

 

 

이전과 다른 점

이전 선착순 이벤트 쿠폰을 발행하는 로직은 줄을 세우고 순차적으로 원하는 양만큼만 발급해주면 끝났습니다. 그런데 이번에는 줄을 세우고 줄을 선 유저에게 자신이 몇번째인지 알려주고 이벤트 페이지에 접속하기전까지 갱신해주어야합니다. 이 점을 생각하면서 전체적인 구조를 그려보겠습니다.

 

 

구조

유저가 /event 에 접속 요청을 하면 Filter에서 인증된 토큰이 있는지 검사합니다. 없으면 Redis에서 토큰을 생성해 대기열에 넣고, /waiting 으로 redirect 해서 현재 대기열을 표시해줍니다.

/waiting 에서 /api/v1/polling 요청으로 현재 대기열이 몇명인지, 인증되었는지, 내 대기순번은 몇번인지를 1초마다 polling 해줍니다.

유저의 토큰이 인증되었으면 /event 로 보내줍니다.

 

이렇게 구성해보았습니다. 이제 구현해보겠습니다.

 

 

 

Page

@Controller
public class EventController {

    @GetMapping("/")
    public String mainPage() {
        return "main";
    }

    @GetMapping("/event")
    public String orderPage() {
        return "event";
    }

    @GetMapping("/waiting")
    public String waitingPage() {
        return "waiting";
    }

}

 

main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>메인페이지임</title>
</head>
<body>
    <h1>메인페이지임</h1>
    <a href="/event">이벤트 참여</a>
</body>
</html>

메인페이지에서 이벤트를 참여할 수 있게 만들었습니다.

event.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>이벤트 페이지</title>
</head>
<body>
    <h1>이벤트 페이지임</h1>
</body>
</html>

이벤트페이지입니다. 인증된 토큰이 있어야지 접속할 수 있습니다.

 

waiting.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>대기중</title>
    <style>
        body {
            width: 100%;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .container {
            width: 300px;
            height: 400px;
            background-color: antiquewhite;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        h1,
        h3 {
            text-align: center;
        }
    </style>
    <script src="/js/waiting.js"></script>
</head>
<body>
<div class="container">
    <div id="card">
        <h3>대기번호</h3>
        <h1 id="rank">0</h1>
        <span>뒤에 <span id="waitingCount">0</span>명이 있습니다.</span>
    </div>
</div>
</body>
</html>

대기열페이지입니다. 내 대기번호와 그 뒤로 몇명의 대기자기 있는지 확인할 수 있습니다.

 

waiting.js

const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get('token');

if (token == null) {
    alert('토큰 없음');
    window.location.href = '/';
}

window.addEventListener('load', () => {

    const rank = document.querySelector('#rank');
    const remain = document.querySelector('#waitingCount');

    const poling = () => setInterval(() => fetchGet('/api/v1/poling', callback), 1000);
    poling();


    const callback = (json) => {
        let jRank = json.rank;
        let jTotal = json.total;
        let jAuthenticated = json.authenticated;

        if (jAuthenticated) {
            location.href = `/event?token=${token}`;
            return;
        }

        if (jRank == null) {
            alert('토큰 정보가 없습니다.');
            location.href = '/';
            return;
        }
        rank.innerHTML = jRank + '';
        remain.innerHTML = (Number(jTotal) - Number(jRank)) + '';
    }
});

function fetchGet(url, callback) {
    fetch(`${url}?token=${token}`)
        .then(response => response.json())
        .then(json => callback(json))
}

 

setInterval로 1000ms 마다 서버에서 대기번호, 전체대기열사이즈, 인증여부를 받아옵니다.

인증이 되었다면 인증된 토큰과 함께 /event 페이지로 이동합니다.

 

 

Dto

public record ResponseToken(
    boolean authenticated,	// 인증여부
    Long total, 		// 전체 대기열 수
    Long rank) {		// 대기번호

}

 

Redis

@Service
@RequiredArgsConstructor
public class TokenRedisService {

    private final TokenRedisRepository tokenRedisRepository;

    private static final String WAITING_KEY = "event:waiting";
    private static final String AUTHENTICATED_KEY = "event:authenticated";

    @PostConstruct
    void initDeleteAll() {
        tokenRedisRepository.delete(WAITING_KEY);
        tokenRedisRepository.delete(AUTHENTICATED_KEY);
    }

    public String register() {
        String token = UUID.randomUUID().toString();
        tokenRedisRepository.addIfAbsent(WAITING_KEY, token);
        return token;
    }

    public boolean isAuthenticated(String token) {
       return tokenRedisRepository.exists(AUTHENTICATED_KEY, token);
    }
    public boolean isWaiting(String token) {
        return tokenRedisRepository.exists(WAITING_KEY, token);
    }

    public ResponseToken getRank(String token) {
        Long total = tokenRedisRepository.getSize(WAITING_KEY);
        Long rank = tokenRedisRepository.getRank(WAITING_KEY, token);
        boolean authenticated = tokenRedisRepository.exists(AUTHENTICATED_KEY, token);
        return new ResponseToken(authenticated, total, rank);
    }

    public void authenticate(long poolSize) {
        for (long i = 0; i < poolSize; i++) {
            String token = tokenRedisRepository.popMinIfExists(WAITING_KEY);
            if (token == null) return;

            tokenRedisRepository.addIfAbsent(AUTHENTICATED_KEY, token);
        }
    }

}

 

@Repository
@RequiredArgsConstructor
public class TokenRedisRepository {

    private final StringRedisTemplate redisTemplate;

    public Long getSize(String key) {
        return redisTemplate.opsForZSet().size(key);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public String popMinIfExists(String key) {
        if (getSize(key) < 1) return null;
        var tuple = redisTemplate.opsForZSet().popMin(key);
        return (tuple == null) ? null : tuple.getValue();
    }

    public Boolean addIfAbsent(String key, String token) {
        return redisTemplate.opsForZSet().addIfAbsent(
            key,
            token,
            System.currentTimeMillis()
        );
    }

    public void removeToken(String key, String token) {
        redisTemplate.opsForZSet().remove(key, token);
    }

    public boolean exists(String key, String token) {
        return redisTemplate.opsForZSet().score(key, token) != null;
    }

    public Long getRank(String key, String token) {
        Long rank = redisTemplate.opsForZSet().rank(key, token);
        return rank != null ? rank + 1 : null;
    }

}

 

 

 

Filter

@Configuration
@RequiredArgsConstructor
public class FilterConfig implements WebMvcConfigurer {

    private final EventTokenFilter eventTokenFilter;

    @Bean
    FilterRegistrationBean<EventTokenFilter> filterRegistrationBean() {
        FilterRegistrationBean<EventTokenFilter> filter = new FilterRegistrationBean<>(eventTokenFilter);
        filter.setOrder(1);
        filter.addUrlPatterns("/event/*");
        return filter;
    }

}
@Component
@RequiredArgsConstructor
public class EventTokenFilter extends OncePerRequestFilter {

    private final TokenRedisService tokenRedisService;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        String token = request.getParameter("token");

        if (token == null) {
            token = tokenRedisService.register();
            response.sendRedirect("/waiting?token=" + token);
            return;
        }

        boolean isAuthenticated = tokenRedisService.isAuthenticated(token);

        if (!isAuthenticated) {
            response.sendRedirect("/waiting?token=" + token);
            return;
        }

        filterChain.doFilter(request, response);
    }

}

 

/event 요청의 Filter입니다.

  1. token이 없으면 토큰을 생성해서 대기열에 넣고 /waiting 으로 redirect 합니다.
  2. token이 있지만 인증된 토큰이 아니라면 /waiting 으로 redirect 합니다.

 

 

Polling

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
public class EventRestController {

    private final TokenRedisService tokenRedisService;

    @GetMapping("/poling")
    public ResponseEntity<ResponseToken> rank(String token) {
        ResponseToken rToken = tokenRedisService.getRank(token);
        return ResponseEntity.ok(rToken);
    }

}

 

 

 

Schedule

@Configuration
@RequiredArgsConstructor
public class EventScheduler {

    private final TokenRedisService tokenRedisService;
    
    private static final long POOL_SIZE = 100;

    @Scheduled(fixedDelay = 1000)
    public void eventScheduler() {
        tokenRedisService.authenticate(POOL_SIZE);
    }
}

 

1초마다 대기열에서 100명의 유저를 인증해줍니다.

 

 

 

결과

JMeter로 대기열에 사람을 채우면서 대기열에 진입해보겠습니다.

 

 

/waiting
/waiting
/event 페이지

 

정상 동작합니다. 영상으로 남기고싶었는데 귀찮아서 사진으로 그냥 찍었습니다. 궁금하면 만들어서 직접 해보세요

 

 

 

 

추가

여기까지만 하려는데 조금 아쉽습니다.

 

첫번째로 위의 코드는 고정적인 유저수를 1초간격으로 허용해주는데 내가 원하는 사이즈만큼만 허용시키고싶은 생각이 들더라구요 전체 허용시킬 size가 1000이고, 1초마다 100명의 유저를 인증해준다고했을때 전체 size 1000이 다 차면 더이상 허용시키지 않고 인증된 유저가 페이지를 벗어나면 그 수만큼 인증시키고싶었습니다.

 

두번째는 대기열에서 유저가 빠져나가면 대기열 토큰을 지워 불필요한 데이터가 담기지 않게 하고싶었습니다.

 

이 두 작업을 더해보겠습니다.

 

먼저 대기열에서 토큰을 지워주는 /api/v1/leave와 인증된토큰을 지워주는 /api/v1/exit를 만들어보겠습니다.

 

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1")
public class EventRestController {

    private final TokenRedisService tokenRedisService;

    @GetMapping("/poling")
    public ResponseEntity<ResponseToken> rank(String token) {
        ResponseToken rToken = tokenRedisService.getRank(token);
        return ResponseEntity.ok(rToken);
    }

    @PostMapping("/exit")
    public ResponseEntity<String> exit(String token) {
        tokenRedisService.exit(token);
        return ResponseEntity.ok("exit");
    }

    @PostMapping("/leave")
    public ResponseEntity<String> leave(String token) {
        tokenRedisService.leave(token);
        return ResponseEntity.ok("leave");
    }
}

 

TokenRedisService

    public void leave(String token) {
        tokenRedisRepository.removeToken(WAITING_KEY, token);
    }

    public void exit(String token) {
        tokenRedisRepository.removeToken(AUTHENTICATED_KEY, token);
    }

 

 

1. 대기열 토큰 제거

window.addEventListener('beforeunload', () => {
    navigator.sendBeacon(`/api/v1/leave?token=${token}`);
})

waiting.js 에서 unload 되기전에 /api/v1/leave 에 토큰을 보내 대기열에서 제거해주도록 했습니다.

 

 

 

2. 인증토큰 제거

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>이벤트 페이지</title>
</head>
<body>
    <h1>이벤트 페이지임</h1>
    <button>완료</button>

    <script>
        const button = document.querySelector('button');

        const param = new URLSearchParams(window.location.search);
        const token = param.get('token');

        button.addEventListener('click', () => {

            fetch(`/api/v1/exit?token=${token}`, {method : 'POST'})
                .then(res => {
                    window.location.href = '/';
                })
        })

    </script>
</body>
</html>

이벤트 페이지에서 모든 작업이 완료되었다면 /api/v1/exit 요청으로 인증된 토큰을 제거해줍니다.

 

 

TokenRedisService

    public void authenticateWithStaticPool(long poolSize) {
        for (long i = 0; i < poolSize; i++) {
            String token = tokenRedisRepository.popMinIfExists(WAITING_KEY);
            if (token == null) return;

            tokenRedisRepository.addIfAbsent(AUTHENTICATED_KEY, token);
        }
    }

    public void authenticateWithMaxPool(long maxPoolSize, long poolSize) {
        Long authenticatedSize = tokenRedisRepository.getSize(AUTHENTICATED_KEY);
        long remainAuthenticatedSize = Math.min(maxPoolSize - authenticatedSize, poolSize);
        System.out.println("현재 인증된 토큰 수 : " + authenticatedSize + ", 인증 가능한 수 : " + remainAuthenticatedSize);
        authenticateWithStaticPool(remainAuthenticatedSize);
    }

 

기존의 authenticate 를 위와 같이 변경해줍니다. authenticateWithStaticPool 은 기존의 authenticate이고,

authenticateWithMaxPool 은 최대 인증토큰 개수까지만 인증시키는 메소드입니다.

 

@Configuration
@RequiredArgsConstructor
public class EventScheduler {

    private final TokenRedisService tokenRedisService;

    private static final long POOL_SIZE = 100;
    private static final long MAX_POOL_SIZE = 1000;

    @Scheduled(fixedDelay = 1000)
    public void eventScheduler() {
        tokenRedisService.authenticateWithMaxPool(MAX_POOL_SIZE, POOL_SIZE);
    }
}

이렇게 하고 실행해봅시다.

 

 

결과

100 개씩 인증하다가 최대 사이즈가 다 차면 인증토큰을 더이상 발행하지 않습니다.

이때 1001번 대기자는 계속 polling 하면서 대기하다가 앞에서 인증토큰이 exit되면

1001번 대기자 이벤트페이지 접속

들어오는 걸 확인할 수 있습니다.

1001번째 대기자 polling되었을때의 Schedule

 

아직 해결못한 내용

이렇게 했을 때 아직 해결못한것이 있습니다. 인증된 토큰을 어떻게 지우냐 입니다. 위의 코드는 인증된 토큰을 지우려면 이벤트페이지에서 어떤 작업을 모두 완료했을때 뿐입니다.

만약 중간에 페이지를 닫거나 새로운 페이지로 이동한다면 어떻게 토큰을 지워야할까요?

JS에서 beforeunload 로 토큰을 지우도록 하면 다음 문제는 /event 페이지에서 /event/** 로 이동할때 발생합니다. 페이지가 변경되면 인증된 토큰이 사라지거든요.

이 문제에 대해서는 해결하면 글써보도록하겠습니다.

 

 

[Spring Boot] 선착순 이벤트 쿠폰을 만들어보자 (1) - 동시성 이슈

들어가기 전에 우아한테크톡의 선착순 이벤트 서버 생존기를 보고 아이디어를 얻었고, 그 과정을 처음부터 부딪히면서 배우기 위해 바닥부터 만들어보면서 느낀점과 배운것들을 정리하기 위

tmd8633.tistory.com

 

이전 글에서 이어집니다.

 

 

 

개요

저번 글에서 동시성이슈를 해결했었습니다. 이번에는 순간적인 대량의 트래픽을 감당하기 위해 TPS를 높혀보겠습니다.

우아한테크톡에서도 보았듯이 Redis를 통해 이 문제를 해결해 보도록 하겠습니다.

 

 

 

 

구조

 

  1. 유저가 이벤트 쿠폰 발행을 요청하면 Redis에서 SortedSet으로 정렬해서 대기열에 넣습니다.
  2. Schedule로 1초간격으로 한정된 유저만큼 쿠폰 발급을 요청합니다.
  3. /api/v3/status 로 발급이 완료되었는지 확인할 수 있습니다.

 

이런 순서로 동작하도록 설계했습니다. 여기서 중요한 것은 많은 트래픽을 감당하기 위해서 대기열에 진입하는 로직에서 무거운 작업을 빼서 최소화 하기로했습니다. 

 

 

CouponConfig

@Configuration
@RequiredArgsConstructor
public class CouponConfig {

    private final JpaCouponRepository jpaCouponRepository;
    public static final int COUPON_COUNT = 200;

    @PostConstruct
    public void init() {
        Coupon saveCoupon = Coupon.builder()
            .name("한정수량 쿠폰")
            .amount(COUPON_COUNT)
            .build();
        jpaCouponRepository.save(saveCoupon);
    }
}

쿠폰 발급갯수를 public static으로 만들어놓았습니다.

 

 

CouponController

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v3")
public class CouponControllerV3 {

    private final CouponRedisService couponRedisService;

    @PostMapping("/coupon")
    public ResponseEntity<ResponseCouponStatus> coupon() {
        long userId = Math.abs(new Random().nextLong() % 2000) + 1;
        boolean register = couponRedisService.register(userId);

        return ResponseEntity.ok(new ResponseCouponStatus(register ? "요청" : "마감", userId));
    }

    @GetMapping("/status")
    public ResponseEntity<Boolean> status(Long userId) {
        boolean isIssued = couponRedisService.isIssued(userId);
        return ResponseEntity.ok(isIssued);
    }

}

/api/v3/coupon 은 쿠폰 발급 요청 API

/api/v3/status 는 쿠폰 발급이 완료되었는지를 확인하는 API 입니다.

boolean register는 대기열에 정상적으로 등록되었으면 true, 등록이 되지않았으면(이미 마감된경우) false를 반환합니다.

userId 를 Response에 담아서 보내는 이유는 테스트를 위함입니다. 랜덤으로 userId를 만들었기때문에 랜덤으로 만들어진 userId로 /api/v3/status 로 발급여부를 확인하기 위해 다시 한번 요청을 해야하기때문입니다.

 

CouponRedisService

@Service
@RequiredArgsConstructor
public class CouponRedisService {

    private final CouponRedisRepository couponRedisRepository;

    private static final String WAITING_QUEUE_KEY = "coupon:waiting";     // 대기열
    private static final String ISSUED_SET_KEY = "coupon:issued";   // 처리된 유저 목록
    private boolean isFull = false;

    @PostConstruct
    void init() {
        couponRedisRepository.delete(WAITING_QUEUE_KEY);
        couponRedisRepository.delete(ISSUED_SET_KEY);
    }

    public boolean register(Long userId) {
        if (isFull) return false;
        return addIfAbsent(WAITING_QUEUE_KEY, String.valueOf(userId));
    }

    public List<String> getWaitingUsers() {
        if (isFull) return null;
        return addIssued();
    }

    private Long getSize(String key) {
        return couponRedisRepository.getSize(key);
    }

    private List<String> addIssued() {
        List<String> temp = new ArrayList<>(COUPON_COUNT);

        String userId = popMinIfExists(WAITING_QUEUE_KEY);

        while (userId != null) {
            if (getSize(ISSUED_SET_KEY) >= COUPON_COUNT) {
                isFull = true;
                couponRedisRepository.delete(WAITING_QUEUE_KEY);
                return temp;
            }
            Boolean isAdded = addIfAbsent(ISSUED_SET_KEY, userId);

            if (Boolean.TRUE.equals(isAdded)) {
                temp.add(userId);
            }

            userId = popMinIfExists(WAITING_QUEUE_KEY);
        }
        return temp;
    }

    private String popMinIfExists(String key) {
        return couponRedisRepository.popMinIfExists(key);
    }

    private Boolean addIfAbsent(String key, String userId) {
        return couponRedisRepository.addIfAbsent(key, userId);
    }

    public boolean isIssued(long userId) {
        return couponRedisRepository.existsByUserId(ISSUED_SET_KEY, userId);
    }
}

 

코드를 분리한게 많아서 하나하나 설명하겠습니다.

public 메소드는 register, getWaitingUsers, isIssued 뿐입니다.

register는 Redis로 대기열에 추가하는 메소드,

getWaitingUsers는 Schedule에서 대기열의 유저를 받아오는 메소드로 DB에 이벤트쿠폰을 넣어주는 역할을 합니다.

isIssued는 유저가 발급되었는지 확인하는 메소드로 /api/v3/status 요청에 사용됩니다.

 

COUPON_COUNT 는 import static 된 데이터입니다. CouponConfig에 COUPON_COUNT를 말합니다.

getWaitinUers는 ISSUED_SET_KEY size를 확인하고 최대 남은 발급수량만큼 대기열에서 유저를 pop 해줍니다.

 

 

CouponRedisRepository

@Repository
@RequiredArgsConstructor
public class CouponRedisRepository {

    private final StringRedisTemplate redisTemplate;

    public Long getSize(String key) {
        return redisTemplate.opsForZSet().size(key);
    }

    public void delete(String key) {
        redisTemplate.delete(key);
    }

    public String popMinIfExists(String key) {
        if (getSize(key) < 1) return null;
        var tuple = redisTemplate.opsForZSet().popMin(key);
        return (tuple == null) ? null : tuple.getValue();
    }

    public Boolean addIfAbsent(String key, String userId) {
        return redisTemplate.opsForZSet().addIfAbsent(
            key,
            userId,
            System.currentTimeMillis()
        );
    }

    public boolean existsByUserId(String key, long userId) {
        return redisTemplate.opsForZSet().score(key, String.valueOf(userId)) != null;
    }
}

RedisTemplate를 관리하는 Repository입니다.

 

 

Scheduler

@EnableScheduling
@Configuration
@RequiredArgsConstructor
public class CouponScheduler {

    private final CouponRedisService couponRedisService;
    private final CouponServiceV3 couponServiceV3;

    @Scheduled(fixedDelay = 1000)
    public void processCouponRequests() {

        var waitingUsers = couponRedisService.getWaitingUsers();

        if (waitingUsers == null || waitingUsers.isEmpty()) return;

        couponServiceV3.issueEventCoupons(waitingUsers);
    }
}

대기중인 유저를 가져와 saveAll 하는 메소드입니다. 작업이 1초 이상이 걸릴 수 있기 때문에 fixedDelay를 사용했습니다.

 

CouponServiceV3

@Service
@RequiredArgsConstructor
public class CouponServiceV3 {

    private final JpaCouponRepository jpaCouponRepository;
    private final CouponRepository couponRepository;


    @Transactional(isolation = Isolation.REPEATABLE_READ)
    public boolean issueEventCoupons(List<String> waitingUsers) {
        Coupon coupon = jpaCouponRepository.findByIdWithLock(1L)
            .orElseThrow(() -> new CouponException("쿠폰을 찾을 수 없습니다."));
        if (!coupon.isAvailable()) {
            return false;
        }

        coupon.decreaseAmount(waitingUsers.size());

        List<EventCoupon> saveEventCoupons = waitingUsers
            .stream()
            .map(x -> EventCoupon.builder()
                .userId(Long.valueOf(x))
                .createdAt(LocalDateTime.now())
                .coupon(coupon)
                .build())
            .toList();

        couponRepository.saveEventCoupons(saveEventCoupons);

        return coupon.isAvailable();
    }
}

대기중인 유저들에게 EventCoupon saveAll 하는 역할을 합니다.

 

 

결과

400 Thread, 10초간 요청하도록 설정

환경은 이전과 동일합니다.

 

 

Samples는 2.5배, 평균응답시간(ms) 2.8배, TPS 2.5배 향상이 있었습니다.

 

응답스펙이 Redis가 30 bytes가 높아서 그렇지 스펙을 똑같이 맞춘다면 어마어마한 속도차이가 있습니다.

Samples는 4.4배, 평균응답시간(ms) 4.8배, TPS 4.2배 빨랐습니다.

 

 

결론

우연히 우아한테크톡에서 봤던 영상덕분에 이벤트 쿠폰을 발행하는 과정에서 생길 수 있는 문제 (동시성, 트래픽처리)에 대해서 배울 수 있었던 것같습니다. Lock 에 대해서도 공부가 되었고, Redis가 아직 익숙하지 않지만 더 공부해서 잘 사용하게 될때쯤 글을 적어보려고합니다.  이게 글로는 엄청 짧아보이는데 Redis를 사용해 어떻게 해야 조금 더 빨라 질 수 있을까에 대해서 고민하면서 코드를 지우고 적고 하면서 배운걸 적다보니 결론만 내린 느낌같습니다. 그런데 지금 위에 코드를 보면 변경하고싶은 사항들이 많아보입니다. 대기열에서 발급받은유저목록으로 보내는 과정에서 조금더 최적화 할 수 있을 것같거든요. 하지만 더이상 손대지 않을겁니다. 이번 실기나 이벤트 쿠폰 발급은 메인이 아니거든요. 다음 글 부터는 대기열을 이용해 rank를 반환해주고 다음 과정을 넘어가기 전에 buffer를 만들어볼까합니다.

큐넷

이런 기능처럼요. 아직 해보지 않아서 며칠 걸릴 수 있습니다. 

들어가기 전에

 

우아한테크톡의 선착순 이벤트 서버 생존기를 보고 아이디어를 얻었고, 그 과정을 처음부터 부딪히면서 배우기 위해 바닥부터 만들어보면서 느낀점과 배운것들을 정리하기 위해 쓰는 글입니다.

 

 

개요

초보 개발자로써 높은 트래픽을 경험하는 것은 거의 불가능합니다. 그래서 그런지 저도 동시성 불감증이 있나 별로 크게 생각하지 않았었는데, 우아한테크톡에서 다룬 영상을 보고 직접 만들어보고 싶은 생각이 들었습니다. 개발 과정은 처음부터 완성된 결과물을 만들어내는게 아니고 바닥부터 단계별로 적용해보면서 조금씩 나아지는 결과물을 만들어 보는 방향으로 해보았습니다. 그게 더 공부가 될 것 같거든요.

 

 

Entity

@Entity
public class Coupon {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "COUPON_ID")
    private Long id;

    private String name;
    private int amount; // 최대 수량

}
@Entity
public class EventCoupon {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COUPON_ID")
    private Coupon coupon;

    @Column(unique = true, nullable = false)
    private Long userId;

    private LocalDateTime createdAt;

}

 

Entity는 테스트 환경에 맞게 간단하게 구성했습니다.

Coupon의 amount는 한정수량을 나타냅니다. 500 이면 500장만 발급할 수 있습니다.

EventCoupon의 Long userId 는 원래 User user 로 join 되어야 하지만 테스트 환경 상 수 많은 User 데이터를 넣는것이 번거롭기 때문에 User user의 id 값인 Long userId 로 넣었습니다. 저 자리에 실제로는 User user 가 들어가야 한다고 이해하면 됩니다.

이벤트 쿠폰은 중복수령할 수 없기 때문에 userId에 unique 속성을 주었습니다.

 

Config

@Configuration
@RequiredArgsConstructor
public class CouponConfig {

    private final JpaCouponRepository jpaCouponRepository;

    @PostConstruct
    public void init() {
        Coupon saveCoupon = Coupon.builder()
            .name("한정수량 쿠폰")
            .amount(200)
            .build();
        jpaCouponRepository.save(saveCoupon);
    }
}

쿠폰을 서버 띄울 때 하나 만들어줍니다. 수량은 200개로 고정하고 시작하겠습니다.

 

 

동시성 문제 해결하기

먼저 내가 작성한 로직이 정상적으로 동작해야 다음 파트로 넘어갈 수 있습니다. 제가 생각한 로직의 순서는 다음과 같습니다.

 

  1. Coupon에서 현재 amount를 확인 - amount가 0이면 예외
  2. EventCoupon에서 이전에 발급받은 이력 확인 - 발급이력이 존재하면 예외
  3. Coupon 의 amount 1 감소
  4. 이벤트 쿠폰 발급

간단하게 이렇게 구성했습니다.

 

synchronized

@Service
@RequiredArgsConstructor
public class CouponServiceV1 {

    private final JpaEventCouponRepository jpaEventCouponRepository;
    private final JpaCouponRepository jpaCouponRepository;
    private final CouponRepository couponRepository;

    @Transactional
    public synchronized void issueEventCoupon(long userId, LocalDateTime now) {

        Coupon coupon = jpaCouponRepository.findById(1L)
            .orElseThrow(() -> new CouponException("쿠폰을 찾을 수 없습니다."));
        if (!coupon.isAvailable()) {
            throw new CouponException("마감되었습니다.");
        }

        if (jpaEventCouponRepository.existsByUserId(userId)) {
            throw new DuplicateEventCouponException("중복해서 쿠폰을 받을 수 없습니다.");
        }

        coupon.decreaseAmount();
        couponRepository.issueEventCoupon(userId, now, coupon);
    }

}
@Repository
@RequiredArgsConstructor
public class CouponRepository {

    private final JpaEventCouponRepository jpaEventCouponRepository;

    public void issueEventCoupon(long userId, LocalDateTime now, Coupon coupon) {
        EventCoupon saveEventCoupon = EventCoupon.builder()
            .userId(userId)
            .coupon(coupon)
            .createdAt(now)
            .build();
        jpaEventCouponRepository.save(saveEventCoupon);
    }
    
}

그렇게 만들어진 CouponServiceV1과 CouponRepository입니다. Coupon Id는 1L로 그냥 고정하고 만들었습니다.

 

V1의 특징으로는 @Transactional 에 synchronized 키워드가 붙어있습니다.

'synchronized가 Thread-Safe 하니까 알아서 잘 해주겠지?' 하고 테스트코드를 작성해봅니다.

 

 

 

Test

@SpringBootTest
public class CouponServiceV1Test {

    @Autowired
    private CouponServiceV1 couponService;
    @Autowired
    private JpaCouponRepository jpaCouponRepository;
    @Autowired
    private JpaEventCouponRepository jpaEventCouponRepository;


    @Test
    @DisplayName("1000개의 동시 요청에도 200개의 이벤트쿠폰이 정상적으로 발급된다.")
    void issueEventCoupon() throws InterruptedException {
        // given
        final int threadPoolSize = 32;
        final int threadCount = 1000;

        final ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize);
        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        // when
        for (int i = 0; i < threadCount; i++) {
            executorService.submit(() -> {
                try {
                    long userId = new Random().nextLong(100000) + 1;
                    couponService.issueEventCoupon(userId, LocalDateTime.now());
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
        executorService.shutdown();

        // then
        Optional<Coupon> findCoupon = jpaCouponRepository.findById(1L);
        assertThat(findCoupon).isPresent();
        assertThat(findCoupon.get().getAmount()).isEqualTo(0);

        List<EventCoupon> findEventCoupon = jpaEventCouponRepository.findAll();
        assertThat(findEventCoupon).hasSize(200);

    }

}

 

ExecutorService로 여러 Thread로 해당 메소드를 실행하는 코드를 작성했습니다. 근데 조금 보기가 불편해서 ThreadExecutor 클래스를 새로 만들었습니다.

 

public class ThreadExecutor {

    private final ExecutorService executorService;
    private CountDownLatch countDownLatch;

    private ThreadExecutor(int nThreads) {
        this.executorService = Executors.newFixedThreadPool(nThreads);
    }

    public static ThreadExecutor newThreadPool(int threadPoolSize) {
        return new ThreadExecutor(threadPoolSize);
    }

    public ThreadExecutor threadCount(int threadCount) {
        this.countDownLatch = new CountDownLatch(threadCount);
        return this;
    }

    public void run(Runnable task) throws InterruptedException {
        assert (countDownLatch != null) : "Require countDownLatch before run()";
        for (int i = 0; i < countDownLatch.getCount(); i++) {
            executorService.submit(() -> {
                try {
                    task.run();
                } finally {
                    countDownLatch.countDown();
                }
            });
        }

        countDownLatch.await();
        executorService.shutdown();
    }

}
    @Test
    @DisplayName("1000개의 동시 요청에도 200개의 이벤트쿠폰이 정상적으로 발급된다.")
    void issueEventCoupon2() throws InterruptedException {
        // given
        final int threadPoolSize = 32;
        final int threadCount = 1000;

        // when
        ThreadExecutor.newThreadPool(threadPoolSize)
            .threadCount(threadCount)
            .run(() -> {
                long userId = new Random().nextLong(100000) + 1;
                couponService.issueEventCoupon(userId, LocalDateTime.now());
            });

        // then
        Optional<Coupon> findCoupon = jpaCouponRepository.findById(1L);
        assertThat(findCoupon).isPresent();
        assertThat(findCoupon.get().getAmount()).isEqualTo(0);

        List<EventCoupon> findEventCoupon = jpaEventCouponRepository.findAll();
        assertThat(findEventCoupon).hasSize(200);

    }

앞으로도 몇 번 더 써야하니까 미리 분리했고 조금더 깔끔해 보이네요 이제 진짜 돌려보겠습니다.

 

결과

[pool-3-thread-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Deadlock found when trying to get lock; try restarting transaction

 

Deadlock이 감지되었습니다.

Deadlock 감지 기능이 ON 되어있어서 이렇게 뜨는겁니다.
Deadlock 감지 기능 ON(default) -> 이전 요청을 롤백해서 선행요청에 Blocking이 해제되어 작업수행
Deadlock 감지 기능 OFF -> 양쪽에서 Timeout 되어 롤백됩니다.

MySQL에서 상태변경
SET GLOBAL innodb_deadlock_detect = OFF; -- Deadlock 감지 비활성화
SET GLOBAL innodb_deadlock_detect = ON; -- Deadlock 감지 활성화 (기본값)

쿠폰은 200개로 한정했지만 총 380개가 발행되었습니다. 대참사입니다. 처음에는 synchronized 키워드를 사용했는데 동시요청이 왜 발생한건지 이해가 되지 않았습니다.

 

원인

결론부터 말하자면 synchronized 는 정상적으로 동작했으나 트랜잭션의 프록시 객체에 의해서 실행되는 것이 문제였습니다.

public class CouponServiceV1Proxy {

    private CouponServiceV1 couponServiceV1;

    public void issueEventCoupon(long userId, LocalDateTime now) {
        // 트랜잭션 시작
        
        couponServiceV1.issueEventCoupon(userId, now);
        
        // 트랜잭션 종료
    }

}

대충 만들어보자면 이런 구조인거죠.

  1. 프록시객체에서 트랜잭션을 시작한다
  2. synchronized 메소드는 트랜잭션 내부에 있다.
  3. 트랜잭션이 종료되고 최종 커밋된다.
  4. synchronized 메소드내에서 변경이 일어나고 메소드가 종료되어도 아직 프록시 객체의 트랜잭션은 종료되지 않았다.
  5. 따라서 의도되지 않은 동시성 이슈가 발생했다.

 

프록시 객체가 만들어지는건 뻔히 알고있었는데도 synchronized 키워드에 대해서는 인지하지 못한 실수입니다.

그렇다면 트랜잭션 밖에 synchronized 를 붙히면 되지 않을까?

 

    private synchronized void issueEventCouponSynchronized(Long userId, LocalDateTime now) {
        couponService.issueEventCoupon(userId, now);
    }

대충 이렇게 만들어보았다.

성공

제대로 테스트가 성공했습니다. 그럼 이제 해피엔딩일까? 저도 처음에는 그렇게 생각했습니다. 잘 생각해보면 synchronized 는 하나의 서버에서만 Thread-Safe 하다는걸 느끼실겁니다. 서버가 2개 이상일때는 무방비해진다는거죠. 따라서 DB 자체에서 Lock을 걸어 데이터의 일관성을 유지하도록 해야합니다. 

 

서버의 확장이 전혀 없을 만한 환경에서는 사용해도 무방할것같습니다. 하지만 저는 synchronized 는 사용하지 않도록 하겠습니다.

 

 

 

Lock

 

[DB] Lock 이해하기

Lock 이란?하나의 자원에 여러 요청이 들어올 때 데이터의 일관성과 무결성을 보장하기 위해 하나의 커넥션에게만 변경할 수 있는 권한을 주는 기능으로 동시성을 제어하기 위한 목적으로 사용

tmd8633.tistory.com

MySQL에서 사용하는 비관적 락, 낙관적 락 과 같은 내용은 따로 글을 다루고 여기에 추가하겠습니다. 아직 글을 안썼습니다.

 

위의 글을 참고하시면 도움이 될겁니다.

 

 

@Service
@RequiredArgsConstructor
public class CouponServiceV2 {

    private final JpaEventCouponRepository jpaEventCouponRepository;
    private final JpaCouponRepository jpaCouponRepository;
    private final CouponRepository couponRepository;

    @Transactional(isolation = Isolation.REPEATABLE_READ) // MySQL 기본 격리 수준, Oracle 은 READ_COMMITTED
    public void issueEventCoupon(long userId, LocalDateTime now) {

        Coupon coupon = jpaCouponRepository.findByIdWithLock(1L)
            .orElseThrow(() -> new CouponException("쿠폰을 찾을 수 없습니다."));
        if (!coupon.isAvailable()) {
            throw new CouponException("수량 마감");
        }

        if (jpaEventCouponRepository.existsByUserId(userId)) {
            throw new DuplicateEventCouponException("중복해서 쿠폰을 받을 수 없습니다.");
        }

        coupon.decreaseAmount();
        couponRepository.issueEventCoupon(userId, now, coupon);
    }

}

그렇게 만들어진 CouponServiceV2 입니다.

트랜잭션이 시작될 때 읽은 데이터를 트랜잭션이 종료될 때까지 다른 트랜잭션이 변경할 수 없는 격리수준 (REPEATABLE_READ)을 사용했습니다. 저는 MySQL을 사용하고있고, 기본격리수준이 REPEATABLE_READ 라 명시하지 않아도 되지만 공부하기 위해서 작성해놓았습니다.

 

public interface JpaCouponRepository extends JpaRepository<Coupon, Long> {

    @Lock(LockModeType.PESSIMISTIC_WRITE) // 배타적 락 획득
    @Query("SELECT c FROM Coupon c WHERE c.id = :id")
    Optional<Coupon> findByIdWithLock(@Param("id") Long id);
}

@Lock이나 @Transactional isolation 에 대해서는 따로 글을 작성하겠습니다.

 

Coupon을 가져올 때, 배타적 락을 획득하도록 만들었습니다. UPDATE가 이루어지기 때문에 WRITE로 했습니다.

 

배타적 락을 획득함으로 다른 트랜잭션에서는 락을 반환하기전까지 Blocking 되도록 했습니다.

 

 

Test

@SpringBootTest
public class CouponServiceV2Test {

    @Autowired
    private CouponServiceV2 couponService;
    @Autowired
    private JpaCouponRepository jpaCouponRepository;
    @Autowired
    private JpaEventCouponRepository jpaEventCouponRepository;

    @Test
    @DisplayName("1000개의 동시 요청에도 200개의 이벤트쿠폰이 정상적으로 발급된다.")
    void issueEventCoupon2() throws InterruptedException {
        // given
        final int threadPoolSize = 32;
        final int threadCount = 1000;

        // when
        ThreadExecutor.newThreadPool(threadPoolSize)
            .threadCount(threadCount)
            .run(() -> {
                long userId = new Random().nextLong(2000) + 1;
                couponService.issueEventCoupon(userId, LocalDateTime.now());
            });

        // then
        Optional<Coupon> findCoupon = jpaCouponRepository.findById(1L);
        assertThat(findCoupon).isPresent();
        assertThat(findCoupon.get().getAmount()).isEqualTo(0);

        List<EventCoupon> findEventCoupon = jpaEventCouponRepository.findAll();
        assertThat(findEventCoupon).hasSize(200);

    }

}

달라진거 하나도 없습니다. CouponServiceV1 -> CouponServiceV2로만 변경되었습니다.

 

 

결과

성공

 

이번에는 JMeter로도 조금더 강하게 테스트 해보겠습니다.

 

400 Thread, 10초간 요청하도록 설정

 

10초간 21124번의 요청이 있었고, 에러 0% 입니다. 정상 작동합니다.

 

 

 

결론

위의 코드는 최대한 한 화면에 보이도록 작성한 코드입니다. 쓸만한 코드가 아닙니다. 그냥 내용만 봐주세요.

생각보다 오랫동안 고민하면서 작성했습니다. 특히 synchronized 부분에서 애먹었던것 같습니다. 'synchronized 는 thread-safe하다' 와 '트랜잭션은 프록시를 통해 실행된다.', '프록시객체를 사용하기 때문에 내무 메소드를 호출하면 안된다' 를 통합하는게 이상하게 너무 어려웠네요.. 그래서 Lock 파트는 내용 자체가 어렵지 않아서 금방 이해할 수 있었습니다. 이번 동시성 이슈를 다루면서 전에 생각하지 못했던 부분들 (락 획득 자원의 순서의 중요성, Deadlock 감지 기능 등)에 대해서 배울 수 있었던 시간이었습니다.

 

다음 글은 Redis를 활용해서 줄세우기를 만들어보도록 하겠습니다. 만들어보고 테스트해보고 글을 작성하는거라 좀 걸립니다.

Lock 이란?

하나의 자원에 여러 요청이 들어올 때 데이터의 일관성과 무결성을 보장하기 위해 하나의 커넥션에게만 변경할 수 있는 권한을 주는 기능으로 동시성을 제어하기 위한 목적으로 사용됩니다.  간단한 예시를 보면서 이해해 보도록 하겠습니다.

사용자에게 송금하는 메소드 transfer 가 있습니다. 순서는 다음과 같습니다. 잔고조회 -> 잔고 + amount

 

사용자A사용자B가 동시에 사용자C에게 송금한다고 했을 때, 사용자A, 사용자B는 1000 씩 송금을 했으니 사용자C의 잔고가 4000이 될 것으로 예상했으나 최종잔고는 3000이 되었습니다. 이는 동시성 문제로 데이터의 일관성과 무결성이 보장되지 않아서 생기는 문제입니다. 오늘은 이 문제를 해결하는 Lock 에 대해서 알아보도록 하겠습니다.

 

 

Lock의 종류

1. Shared Lock (공유 락)

  • 읽기 작업을 위한 Lock
  • 여러 트랜잭션이 동시에 데이터를 읽을 수 있음
  • 다른 Shared Lock과는 호환되지만 Exclusive Lock과는 호환되지 않음
    • 읽기 작업 중일 때, 새로운 읽기 작업이 들어온 경우 -> 가능
    • 읽기 작업 중일 때, 새로운 쓰기 작업이 들어온 경우 -> 불가능 (쓰기 작업으로 멱등성 보장x)

2. Exclusive Lock (배타적 락)

  • 쓰기 작업을 위한 Lock
  • 한 번에 하나의 트랜잭션만 데이터를 수정할 수 있음
  • 다른 어떤 Lock과도 호환되지 않음
    • 쓰기 작업 중일 때, 새로운 읽기 작업이 들어온 경우 -> 불가능
    • 쓰기 작업 중일 때, 새로운 쓰기 작업이 들어온 경우 -> 불가능

 

그렇다면 호환되지 않은 Lock의 요청이 들어올 때 불가능하다고 했는데 어떤 상황이 벌어질까?

 

 

Blocking

호환되지 않은 Lock 이 들어올 경우 블로킹(Blocking)이 발생합니다. 새로운 Lock의 요청이 들어올 때, 이미 Lock이 선행되고있으면 선행되고있는 Lock이 종료될떄까지 대기하고있다가 종료가 되면 새로운 Lock을 획득해 진행하게됩니다.

 

위의 예시에 Lock을 적용한다면 순서는 다음과 같습니다.

사용자A -> transfer(사용자A, 사용자C, 1000) -> Exclusive Lock 획득 -> 사용자B -> transfer(사용자B, 사용자C, 1000) -> Blocking (사용자A의 Lock이 해제될 때까지 대기) -> 송금 -> Blocking 해제, Exclusive Lock 획득 -> 송금 

 

위에서 Shared Lock은 여러 Shared Lock과 호환된다고했습니다. 만약 100개의 트랜잭션에서 Shared Lock을 획득하고 있을때 1개의 Exclusive Lock 요청이 들어온다면 100개의 Shared Lock이 모두 해제될때까지 대기하게 됩니다. 또는 끊기지 않고 Shared Lock이 들어온다면 Exclusive Lock은 계속 획득하지 못하게 됩니다. 따라서 Lock을 설정하는 범위를 알맞게 설정하는 것이 중요합니다.

 

 

그 다음으로 두개의 자원에 대한 Lock을 다룰때는 어떤 문제가 생길 수 있을까요?

 

 

Dead Lock

두 개 이상의 자원에서 서로 Blocking 되어 영구적으로 접근할 수 없는 상태(교착상태)입니다.

 

위의 transfer 메소드에서 사용자의 잔고를 줄이는 로직추가하여 예시로 들어보겠습니다. 로직은 다음과 같습니다. 그림그리는 것보다 코드가 더 읽기 쉽고 이해하기 편하니 코드로 작성하겠습니다.

    public void transfer(Long fromId, Long toId, BigDecimal amount) {
        // 잔액 확인 및 업데이트를 위해 Exclusive Lock
        User fromUser = userRepository.findByIdWithLock(fromId);

        // 수신자의 계좌에 Exclusive Lock
        User toUser = userRepository.findByIdWithLock(toId);

        // 송금 처리
        fromUser.withdraw(amount);
        toUser.deposit(amount);
    }

 

시간 사용자A
(사용자A -> 사용자B에게 100원 송금)
사용자B
(사용자B -> 사용자A에게 100원 송금)
1 사용자A 락 획득 사용자B 락 획득
2 사용자B 락 획득시도 (Blocking) 사용자A 락 획득시도 (Blocking)
3 대기 대기
4 Dead Lock

 

그러면 위의 코드에서 Dead Lock이 발생하지 않게 하기 위해서는 어떻게 해아할까요?

락 획득의 순서에서 일과성을 유지하는 것입니다.

    public void transfer(Long fromId, Long toId, BigDecimal amount) {
        User fromUser, toUser;
        if (fromId < toId) {
            fromUser = userRepository.findByIdWithLock(fromId);
            toUser = userRepository.findByIdWithLock(toId);
        } else {
            toUser = userRepository.findByIdWithLock(toId);
            fromUser = userRepository.findByIdWithLock(fromId);
        }
        fromUser.withdraw(amount);
        toUser.deposit(amount);
    }

 

사용자A ID = 1

사용자B ID = 2 라고 했을 때,

시간 사용자A
(사용자A -> 사용자B에게 100원 송금)
사용자B
(사용자B -> 사용자A에게 100원 송금)
1 사용자A 락 획득 사용자A 락 획득시도 Blocking
2 사용자B 락 획득 대기
3 송금 (락 반환) 사용자A 락 획득
4 - 사용자B 락 획득
5 - 송금 (락 반환)

 

 

결론

동시성 문제와 대용량 트래픽을 공부하기 위해 한정 수량 쿠폰과 대기열을 만들어보는 와중에 DeadLock와 데이터 무결성에 문제가 생겼고, 이를 해결하는 과정에서 Lock에 대해서 공부하게되었습니다. 다음 글은 JPA Lock 전략과 JAVA synchrozied, 그리고 JMeter 에 대해서 작성하려고합니다.

개요

이번에는 OAuth2 Resource Server 를 이용해 JWT를 손쉽게 적용해보도록 하겠습니다.

 

 

JWT란?

들어가기에 앞서 JWT에 대해서 간단히 설명하자면 JWT(JSON Web Token)는 자원을 사용하기 전에 사용해도 되는 유저인지 판단할 수 있게 해주는 티켓이라고 생각하면됩니다. 그러니까 놀이공원에 들어갈 수 있게 해주는 입장권인겁니다. 놀이기구를 타기위해서는 놀이공원에 돈을 내야합니다. 그걸 증명하는게 입장권이죠. 놀이기구를 타기 전에 입장권을 검사함으로써 놀이기구(자원)을 사용하도록 직원이 허가 해줍니다. 여기서 중요시 봐야할 점은 표를 검사하는 직원은 어떤 경로를 통해 입장권을 구매했는지, 어떻게 입장권을 구했는지 알지도 못할 뿐더러 궁금해 하지도 않습니다. 그저 입장권이 있는지, 없는지만 판단하게되죠. JWT는 그런 역할을 하는 겁니다.

Web 환경에서는 Session과 Cookie를 활용해 입장권을 저장해놓을 수 있지만 REST API 로 확장하게되면 Session 과 같은 저장공간을 사용하지 못합니다. 따라서 HTTP Request Header부분에 유저의 정보를 보관해서 요청을 보내서 권한을 사용할 수 있는지 없는지 판단하게됩니다. 

 

JWT에 대한 상세한 글은 따로 작성하도록 하겠습니다.

 

 

 

OAuth2 Resource Server

먼저 OAuth2 Client와 OAuth2 Resource Server 의 차이점을 짚고 넘어가겠습니다.

oauth2-client는 보호된 자원을 사용하려고 접근 요청을 하는 애플리케이션입니다.

oauth2-resource-server 는 보호된 자원을 호스팅하는 역할을 합니다.

 

대략적인 순서는 다음과 같습니다.

 

  • Client가 Resource Server에 접근하기 위해 AccessToken 요청
  • 인증 후 AccessToken 발급받음
  • Client는 이 AccessToken을 사용해 Resource Server의 API 호출
  • Resource Server는 토큰을 검증하고 요청된 리소스 제공

그러니까 oauth2-resource-server에서 토큰을 검증하는 부분을 이번에 구현하는 것입니다.

 

HttpSecurity oauth2ResourceServer 메소드를 살펴보면

 

메소드 설명
bearerTokenResolver Bearer 토큰을 추출하는 방법을 커스텀할 수 있는 기능
accessDeniedHandler 권한이 없을 때 예외 핸들링
authenticationEntryPoint 잘못된 토큰일 때 예외 핸들링
jwt jwt 관련 설정
opaqueToken opaqueToken 관련 설정

 

 

이렇게 되어있습니다. 우리는 jwt를 사용할 것이기 때문에 opaqueToken 설정을 제외하고 나머지를 구현해주면됩니다.

 

 

 

 

JWT 인증 흐름

공식문서 https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/jwt.html

 

  1. BearerTokenAuthenticationFilter 에서 추출된 토큰을 BearerTokenAuthenticationToken으로 만들어 AuthenticationManager로 전달합니다.
  2. AuthenticationProvider 의 구현체인 JwtAuthenticationProvider 에서 실제 인증절차를 거칩니다.
  3. 그 인증절차에는 비밀키를 이용해 JWT 토큰의 무결성(위변조 여부) 검사를 하는 JwtDecoder가 있고,
  4. JWT 토큰을 Authentication 객체로 변환해주는 JwtAuthenticationConverter가 있습니다.
  5. 인증이 성공하면 JwtAuthenticationToken을 반환하게되면서 인증과정이 종료됩니다.

이 과정에서 우리가 구현해야할 부분은

  • 비밀키와 함께 무결성검사를 하기위한 JwtDecoder
  • JWT 토큰의 payload(사용자정보)를 커스텀해 변경된 데이터를 저장하는 AuthenticationToken이 필요합니다. 따라서 JwtAuthenticationToken를 상속한 커스텀된 AuthenticationToken
  • 커스텀된 AuthenticationToken 을 convert 해줄 JwtAuthenticationConverter를 상속한 커스텀 converter

이렇게 3개가 필요합니다. 

 

 

구현하기 전에

@Component
@RequiredArgsConstructor
public class JwtTokenProvider {

    private final SecretKey secretKey;
    public final int JWT_ACCESS_TIME = 60 * 60;
    public final int JWT_REFRESH_TIME = 60 * 60 * 24;

    public String generateAccessToken(TokenId tokenId, long userId,
                                        Collection<? extends GrantedAuthority> authorities) {
        return createToken(tokenId, userId, authorities, JWT_ACCESS_TIME);
    }
    public String generateRefreshToken(TokenId tokenId, long userId,
                                      Collection<? extends GrantedAuthority> authorities) {
        return createToken(tokenId, userId, authorities, JWT_REFRESH_TIME);
    }

    // 토큰 생성
    private String createToken(TokenId tokenId,
                              long userId,
                              Collection<? extends GrantedAuthority> authorities,
                              int expirationPeriod) {
        Date now = new Date(System.currentTimeMillis());

        return Jwts.builder()
            .header()
            .keyId(UUID.randomUUID().toString())
            .add("typ", "JWT")
            .and()
            .issuer("TEST_JWT")
            .claim("iss", tokenId.getProvider())
            .claim("sub", tokenId.getProviderId())
            .claim("userId", userId)
            .claim("scope", authorities.stream()
                .map(GrantedAuthority::getAuthority)
                .toList())
            .issuedAt(now)
            .expiration(new Date(now.getTime() + expirationPeriod * 1000L))
            .signWith(secretKey)
            .compact();
    }

}

커스텀된 JWT 양식입니다. 참고로만 사용해주세요.

권한정보를 넣을것이기 때문에 scope에 넣어주었습니다.

 

구현

 

application.yaml

jwt:
  secret: "{secret-key}"

 

 

JwtDecoder

@Configuration
@RequiredArgsConstructor
public class JwtConfig {

    private final SecretKey secretKey;

    @Bean
    public JwtDecoder jwtDecoder() {
        return NimbusJwtDecoder.withSecretKey(secretKey)
            .macAlgorithm(MacAlgorithm.HS512)
            .build();
    }

}

JwtDecoder 에 비밀키와 알고리즘 HS512를 설정해주었습니다.

중요한 점은 SecretKey의 알고리즘과 JwtDecoder의 알고리즘이 서로 일치해야합니다.

 

HMAC-SHA 키를 생성할 때, 키의 길이에 따라 알고리즘이 자동 선택됩니다:

  • 256비트(32바이트) → HS256
  • 384비트(48바이트) → HS384
  • 512비트(64바이트) → HS512

 

 

CustomJwtToken

@Getter
public class CustomJwtToken extends JwtAuthenticationToken {

    private final Long userId;

    public CustomJwtToken(Jwt jwt, Collection<? extends GrantedAuthority> authorities) {
        super(jwt, authorities);
        this.userId = Long.valueOf(jwt.getClaimAsString("userId"));
    }

}

JwtAuthenticationToken을 상속에 만든 CustomJwtToken 입니다.

 

 

 

CustomJwtAuthentication

public class CustomJwtAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> {

    private final JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();

    @Override
    public final AbstractAuthenticationToken convert(Jwt jwt) { // 3
        AbstractAuthenticationToken token = jwtAuthenticationConverter.convert(jwt); // 4
        Collection<GrantedAuthority> authorities = token.getAuthorities();
        return new CustomJwtToken(jwt, authorities);
    }

}

 

JwtAuthenticationConverter의 convert 메소드는 final 이라 Override할 수 없어서 JwtAuthenticationConverter를 필드에서 사용하였습니다.

 

 

JwtAuthenticationConverter 를 조금더 커스텀 하고싶다면?

public class CustomJwtAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> {

    private final JwtAuthenticationConverter jwtAuthenticationConverter;

    public CustomJwtAuthenticationConverter() {
        this.jwtAuthenticationConverter = new JwtAuthenticationConverter();

        var grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
        // 권한정보가 들어가있는 claim key가 다른경우
        grantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
        // ROLE_ 접두사가 필요한 경우
        grantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");

        jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter);
    }
    @Override
    public final AbstractAuthenticationToken convert(Jwt jwt) {
        AbstractAuthenticationToken token = jwtAuthenticationConverter.convert(jwt);
        Collection<GrantedAuthority> authorities = token.getAuthorities();
        return new CustomJwtToken(jwt, authorities);
    }

}

JwtAuthenticationConverter는 Authorities "scope", 접두사 "SCOPE_" 이 기본값입니다. 이를 수정하고싶은경우

위와 같이 설정하면됩니다. 저 같은 경우 JWT Token을 생성할때 GrantedAutority에서 authority를 꺼냈기 때문에 접두사가 붙은 상태로 생성되었습니다. 따라서 

 

public class CustomJwtAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken> {

    private final JwtAuthenticationConverter jwtAuthenticationConverter;

    public CustomJwtAuthenticationConverter() {
        this.jwtAuthenticationConverter = new JwtAuthenticationConverter();

        var grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
        grantedAuthoritiesConverter.setAuthorityPrefix("");

        jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter);
    }
    @Override
    public final AbstractAuthenticationToken convert(Jwt jwt) {
        AbstractAuthenticationToken token = jwtAuthenticationConverter.convert(jwt);
        Collection<GrantedAuthority> authorities = token.getAuthorities();
        return new CustomJwtToken(jwt, authorities);
    }

}

접두사를 제거해서 사용하였습니다.

 

 

권한정보를 담지않는다면 위 과정은 생략해도 됩니다.

 

 

SecurityConfig

이전에 개발해놓았던 서버는 Web 기반이었습니다. 따라서 Security 설정을 API와 WEB을 구분해서 만들겠습니다.

 

@Bean
@Order(1)
SecurityFilterChain apiSecurityFilterChain(HttpSecurity http, ObjectMapper objectMapper, JwtTokenProvider jwtTokenProvider) throws Exception {

    http.securityMatcher("/api/**")

        .csrf(AbstractHttpConfigurer::disable)
        .cors(cors -> cors
            .configurationSource(apiConfigurationSource())
        )
        .authorizeHttpRequests(request -> request
            .requestMatchers("/api/user/**").hasAnyRole(Role.USER.name(), Role.ADMIN.name())
            .requestMatchers("/api/admin/**").hasRole(Role.ADMIN.name())
            .requestMatchers(HttpMethod.POST, "/api/login").anonymous()
            .anyRequest().permitAll()
        )
        .sessionManagement(session -> session
           .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        )
        // oauth2-resource-server 설정 부분
        .oauth2ResourceServer(server -> server
            .jwt(jwt -> jwt
                .jwtAuthenticationConverter(new CustomJwtAuthenticationConverter())
            )
        );
    return http.build();
}

@Bean
@Order(2)
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {

    http.securityMatcher("/**")

        .authorizeHttpRequests(request -> request
            .requestMatchers("/user/**").hasAnyRole(Role.USER.name(), Role.ADMIN.name())
            .requestMatchers("/admin/**").hasRole(Role.ADMIN.name())
            .requestMatchers("/login", "/signup").anonymous()
            .anyRequest().permitAll()
        )

        /// ... 이전 설정과 동일

    return http.build();
}

// SecretKey Bean 등록
@Bean
SecretKey secretKey(@Value("${jwt.secret}") String secretKey) {
    return Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8));
}

 

 

 

결과

@RestController
@RequiredArgsConstructor
public class RestMainController {


    @GetMapping("/api/user/profile")
    public ResponseEntity<Long> userProfile() {
        CustomJwtToken customJwtToken = (CustomJwtToken) SecurityContextHolder.getContext().getAuthentication();
        return ResponseEntity.ok(customJwtToken.getUserId());
    }

}

 

이렇게 테스트할 Controller를 만들고, 만들어진 JWT로 시도해봅시다.

 

정상적인 토큰인 경우

 

만료된 토큰인 경우

 

 

조금 더 간단하게 써보자

CustomJwtToken customJwtToken = (CustomJwtToken) SecurityContextHolder.getContext().getAuthentication();

일단 이렇게 쓰는게 너무 마음에 안듭니다.

 

public class JwtUserContextHolder {

    public static CustomJwtToken getJwtToken() {
        return (CustomJwtToken) SecurityContextHolder.getContext().getAuthentication();
    }
}
    @GetMapping("/user/profile")
    public ResponseEntity<Long> userProfile() {
        CustomJwtToken customJwtToken = JwtUserContextHolder.getJwtToken();
        return ResponseEntity.ok(customJwtToken.getUserId());
    }

 

이렇게 바꿨습니다. 이것도 조금 마음에 안듭니다. 어노테이션을 활용해서 변수로 바로 가져오게 하고싶습니다.

 

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface JwtToken {
}

@Component
public class JwtTokenArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(JwtToken.class)
            && parameter.getParameterType().equals(CustomJwtToken.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        return JwtUserContextHolder.getJwtToken();
    }
}
@GetMapping("/user/profile")
public ResponseEntity<Long> userProfile(@JwtToken CustomJwtToken customJwtToken) {
    return ResponseEntity.ok(customJwtToken.getUserId());
}

 

엄청 깔끔해 졌네요.

 

 

 

 

결론

사실 이번 글에서 JWT에 관한걸 모두 다루려고 했습니다. 그런데 JWT 발급부분 (oauth2-client)과 JWT 인증부분(oauth2-resource-server) 의 역할이 구분되어있는데 하나의 글로 작성하면 혼란이 생길것같아서 어쩔 수 없이 나눠서 글을 작성했습니다.

위의 코드들은 이미 한 번 모두 구현하고 나서 파트별로 지우고 추가하면서 약간의 컴파일 오류가 생길 수는 있습니다. 그러니 따라치지 마시고 흐름만 이해하고 넘어가주시기 바랍니다. 공부하려고 글을 작성하는 거지 저도 배우는 입장이라 틀릴 수 있다는 부분 알아주셨으면 좋겠습니다.

HttpSecurity

어질어질하다

 

Spring Security는 설정이 대부분일 정도로 Security 설정하는 부분이 정말 많습니다. 또, 6.x.x 버전까지 오면서 Deprecated된 메소드들도 정말 많은데요 중간점검할겸 메소드들을 좀 정리해봤습니다.

1. 보안 설정

1-1 .cors()

  • CORS 설정
  • 크로스 도메인 요청 처리
http.cors(cors -> cors
    .configurationSource(corsConfigurationSource));

 

 

[CS][Spring Security] CORS에 대해서 알아보자

1. CORS란 무엇인가?CORS(Cross-Origin Resource Sharing)는 웹 브라우저에서 외부 도메인 리소스를 안전하게 요청할 수 있도록 하는 표준 규약입니다. 프론트엔드와 백엔드가 분리하는데 있어 CORS에 대해서

tmd8633.tistory.com

 

 

1-2. csrf()

  • CSRF 보호 설정
  • CSRF 토큰 처리
http.csrf(csrf -> csrf
    .ignoringAntMatchers("/api/**")
    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()));

 

 

[CS][Spring Security] CSRF란?

CSRF란?CSRF Cross-Site Request Forgery의 약자로 인증된 사용자의 권한을 악용하여 해당 사용자가 의도하지 않은 요청을 웹사이트에 전송하는 공격 기법입니다. 공격자는 사용자가 이미 인증된 상태를

tmd8633.tistory.com

 

 

1-3. headers()

  • 보안 헤더 설정
  • XSS 방지, 클릭재킹 방지 등
http.headers(headers -> headers
    .defaultsDisabled()  // 기본 설정을 비활성화하고 커스텀 설정 시작
    .frameOptions(frame -> frame.deny())  // X-Frame-Options
    .xssProtection(xss -> xss.enable())   // X-XSS-Protection
    .contentTypeOptions(content -> content.disable())  // X-Content-Type-Options
    .cacheControl(cache -> cache.disable())  // Cache-Control
);

이 부분에 대해서는 아직 공부하지 못했습니다. 공부하고 정리해서 글 쓴다음에 이부분 업데이트 하겠습니다.

 

 

1-4. x509()

  • X.509 인증서 기반 인증
  • 클라이언트 인증서 인증
http.x509(x509 -> x509
    .subjectPrincipalRegex("CN=(.*?)(?:,|$)")     // CN(Common Name)을 추출하는 정규식
    .userDetailsService(userDetailsService))       // 인증서의 사용자 정보와 매핑

주로, SSL/TLS 클라이언트 인증에 사용되며 높은 수준의 보안이 필요한 B2B 환경이나 금융시스템에서 사용된다고 합니다.

 

서버간 통신(Server-to-Server)

  • 마이크로서비스 간 통신
  • API 게이트웨이 인증

IoT 디바이스 인증

  • 각 디바이스에 고유 인증서 발급
  • 디바이스 인증 관리

금융권 시스템

  • 공인인증서 기반 인증
  • 보안이 중요한 기업 시스템

 

이 부분도 자세히 몰라서 공부하고 업데이트 하겠습니다.

 

 

1-5. jee()

  • Java EE 보안 설정
  • 컨테이너 기반 인증
http.jee(jee -> jee
    .mappableRoles("USER", "ADMIN"));

 

Java EE (Enterprise Edition) 기반의 보안 설정을 구성하는 데 사용됩니다 하지만 현재는 잘 사용하지 않고 JWT, OAuth2 같은 인증 방식을 사용합니다. 그러니까 예전 어플리케이션에서 사용된다고만 알고있으면 됩니다.

 

 

1-6. requiresChannel()

  • 채널 보안 요구사항
http.requiresChannel(channel -> channel
        .requestMatchers("/secure/**").requiresSecure()     // HTTPS 필수
        .requestMatchers("/public/**").requiresInsecure()   // HTTP 허용
        .anyRequest().requiresSecure()                      // 나머지는 모두 HTTPS
    );

HTTP, HTTPS 허용에 대한 설정입니다. 사실 프로덕션 환경에서는 모든 요청에 HTTPS를 사용하는 것이 권장되기때문에 이런 설정을 보통 하지 않고 일괄적으로 HTTPS 설정을 하곤합니다.

 

 

http.requiresChannel(channel -> channel
        .anyRequest().requiresSecure() // 모든 요청에 HTTPS 필수
    );

 

 

2. 기본 인증 설정

2-1. httpBasic()

  • HTTP Basic 인증 활성화
  • 간단한 사용자명/비밀번호 기반으로 인증
http.httpBasic(basic -> basic
    .realmName("My App")
    .authenticationEntryPoint(customEntryPoint));

username:password 형식으로 Base64로 인코딩되기 때문에 보안에 취약할 수 있습니다.

http.formLogin() 과 같이 설정을 할 경우 formLogin 이 우선적용됩니다.

REST API에서 커스텀해서 사용할 수는 있으나 그냥 JWT나 쓰는게 더 안전합니다.

 

 

2-2. anonymous()

  • 익명 사용자 접근에 대한 설정
http.anonymous(anonymous -> anonymous
    .principal("guest")
    .authorities("ROLE_GUEST"));

아래 글에서 한번 다룬적이 있습니다.

 

 

[Spring Security] 스프링 시큐리티 Anonymous과 ExceptionHandling (4)

Anonymous 인증이란?Spring Security의 Anonymous 인증은 인증되지 않은 사용자(로그인하지 않은 사용자)를 처리하는 메커니즘입니다. 인증되지 않은 요청에 대해 AnonymousAuthenticationToken을 생성하여 보안

tmd8633.tistory.com

 

 

 

3. 폼 로그인

3-1. formLogin()

  • Form 기반 로그인 설정
  • 로그인 성공/실패 Handling
http.formLogin(form -> form
    .loginPage("/login")
    .defaultSuccessUrl("/home")
    .failureUrl("/login?error=true"));

 

 

3-2. logout()

  • 로그아웃 처리 설정
  • 로그아웃 후 Handling
http.logout(logout -> logout
    .logoutUrl("/logout")
    .logoutSuccessUrl("/login")
    .deleteCookies("JSESSIONID"));

 

formLogin, logout 에 대해서도 작성한 글이 있습니다.

 

[Spring Security] 스프링 시큐리티 시작하기 (로그인, 로그아웃과 권한) (2)

이전글 [Spring Security] 스프링 시큐리티 이해하기 (1)Spring SecuritySpring Security는 인증, 인가 및 일반적인 공격에 대한 보호를 제공하는 프레임워크 입니다. 서비스를 개발할때 필수적으로 구현해야

tmd8633.tistory.com

 

3-3. rememberMe()

  • 세션 만료 후에도 로그인 유지 기능
http.rememberMe(remember -> remember
    .rememberMeServices(rememberMeServices)
)
// 또는

http.rememberMe(remember -> remember
    .key("uniqueAndSecret")
    .tokenValiditySeconds(86400)
    .rememberMeParameter("remember-me")
    .rememberMeCookieName("remember-me")
);

 

 

[Spring Security] 스프링 시큐리티 RememberMe (3)

Remember-Me란?Remember-Me는 사용자가 브라우저를 닫았다가 다시 열어도 로그인 상태를 유지하는 기능입니다. 쿠키를 사용하여 구현되며, Spring Security는 두 가지 구현 방식을 제공합니다.  Simple Hash-B

tmd8633.tistory.com

로그인 폼에서 "로그인 정보 저장" 이런식으로 체크박스되어있는 그 기능에 대한 설명입니다. 아무래도 토큰 형식이다보니까 중요한 작업(결제, 정보수정 등) 에서는 재인증을 요구하는 등의 보안에 신경써야하는 기능입니다. 토큰은 DB에 저장하여 관리하는 것을 권장합니다.

 

 

3-4. passwordManagement()

 

  • 비밀번호 변경 페이지 설정
  • 비밀번호 정책 적용
http.passwordManagement(password -> password
    .changePasswordPage("/change-password")       // 비밀번호 변경 페이지
);

 

 

이 기능은 아무리 찾아봐도 잘 모르겠어서 아래 내용이 사실이 아닐 수 있습니다. 주의해서 읽어주세요

 

비밀번호 변경 페이지에 접속하게 되면 다음과 같은 헤더가 추가된다고합니다.

Change-Password: /change-password

별다른 기능은 없는것같습니다. 혹시라도 누가 이 글을 읽고 passwordManagement 에 대해서 아신다면 댓글로 알려주세요 저도 알고싶어요

 

 

4. 세션관리

4-1. sessionManagerment()

  • 세션 정책 설정
  • 동시 세션 제어 등
http.sessionManagement(session -> session
    .invalidSessionUrl("/login?invalid")
    .maximumSessions(1)
    .maxSessionsPreventsLogin(false)
    .expiredUrl("/login?expired")
);

 

 

[Spring Security] 스프링 시큐리티 SessionManagement (5)

1. Session Management란?Spring Security의 세션 관리는 사용자의 세션을 생성, 유지, 파괴하는 전반적인 프로세스를 관리합니다. 이는 보안과 사용자 경험 모두에 중요한 영향을 미칩니다. 또한 JWT이나 Se

tmd8633.tistory.com

 

 

4-2. securityContext()

  • 보안 컨텍스트 설정
  • SecurityContext 저장 방식 설정
http.securityContext(context -> context
    .requireExplicitSave(true)
    .securityContextRepository(securityContextRepository)  // 저장소 설정
);

 

SecurityContext의 관리 방식을 설정하는 메서드입니다.

 

 

Custom

@Component
public class CustomSecurityContextRepository implements SecurityContextRepository {
    
    @Override
    public SecurityContext loadContext(HttpRequestResponseHolder holder) {
        HttpServletRequest request = holder.getRequest();
        // 요청에서 SecurityContext 로드 로직
        return SecurityContextHolder.createEmptyContext();
    }
    
    @Override
    public void saveContext(SecurityContext context, 
                          HttpServletRequest request,
                          HttpServletResponse response) {
        // SecurityContext 저장 로직
    }
    
    @Override
    public boolean containsContext(HttpServletRequest request) {
        // SecurityContext 존재 여부 확인
        return false;
    }
}

 

 

세션 기반 저장소 (Default)

http.securityContext(context -> context
    .securityContextRepository(new HttpSessionSecurityContextRepository())
);

 

다중 연결 저장소

http.securityContext(context -> context
    .securityContextRepository(new DelegatingSecurityContextRepository(
        new RequestAttributeSecurityContextRepository(),
        new HttpSessionSecurityContextRepository()
    ))
);

 

 

Stateless

http.securityContext(context -> context
    .securityContextRepository(new NullSecurityContextRepository())
);

 

SecurityContext를 저장하지 않습니다. JWT 토큰 기반 인증에서 사용됩니다.

sessionManagement에서

.sessionCreationPolicy(SessionCreationPolicy.STATELESS)

설정을 하면 NullSecurityContextRepository 가 생성됩니다. 그러므로 저장소를 명시적으로 작성하지 않아도 된다는 얘기입니다.

 

// SessionManagementConfigurer.init(H http)
boolean stateless = this.isStateless();
if (securityContextRepository == null) {
    if (stateless) {
        this.sessionManagementSecurityContextRepository = new NullSecurityContextRepository();
    } else {
        // ... 생략
    }
} else {
    // ... 생략
}

// ... 생략

private boolean isStateless() {
    SessionCreationPolicy sessionPolicy = this.getSessionCreationPolicy();
    return SessionCreationPolicy.STATELESS == sessionPolicy;
}

 

요청 속성 저장소

http.securityContext(context -> context
    .securityContextRepository(new RequestAttributeSecurityContextRepository())
);

 

SecurityContext를 요청 속성에 저장하고 단일 요청범위 내에서만 유효합니다. 주로 비동기 요청 처리에서 사용한다고합니다.

사실 이 부분은 공부안해봐서 잘 모르겠습니다.

 

 

 

5. OAuth2

5-1. oauth2Login()

  • OAuth 2.0 로그인 설정
  • 소셜 로그인
http.oauth2Login(oauth2 -> oauth2
    .loginPage("/oauth2/login")
    .defaultSuccessUrl("/home")
    .userInfoEndpoint()
    .userService(customOAuth2UserService));
 

[Spring Security][OAuth2] OAuth2 Log In 소셜로그인 구현 (1)

OAuth2Spring Security의 OAuth 2.0 지원은 크게 두 가지 주요 기능으로 구성됩니다:OAuth2 Resource ServerOAuth2 ClientOAuth2 Log In추가로 OAuth2 Log In이라는 특별한 기능이 있는데, 이는 독립적으로 존재할 수 없으

tmd8633.tistory.com

 

 

5-2. oauth2Client()

  • OAuth 2.0 클라이언트 설정
  • 외부 OAuth 2.0 리소스 접근
http.oauth2Client(client -> client
    .clientRegistrationRepository(clientRegistrationRepository)
    .authorizedClientService(authorizedClientService));

 

 

 

5-3. oauth2ResourceServer()

  • OAuth 2.0 리소스 서버 설정
  • JWT 토큰 검증
http.oauth2ResourceServer(server -> server
    .accessDeniedHandler(new CustomAccessDeniedHandler())
    .authenticationEntryPoint(new CustomEntryPoint())
    .jwt(jwt -> jwt
        .jwtAuthenticationConverter(jwtAuthenticationConverter)
    )
);

 

JWT 관련해서 작성하고 있는 글이 있습니다. 업로드되는대로 업데이트 할겁니다.

 

 

6. 엔터프라이즈 보안

6-1. oidcLogout()

  • OpenID Connect 로그아웃 설정
http.oauth2Login(oauth2 -> oauth2
    .clientRegistrationRepository(clientRegistrationRepository));
    
http.oidcLogout(oidc -> oidc
    .clientRegistrationRepository(clientRegistrationRepository)
    .backChannel(logout -> logout
        .logoutUri("/api/logout")
        .logoutHandler((request, response, authentication) -> {

        })
    )

);

 

OIDC 설정은 아래 게시글에서 보시면 됩니다.

 

 

[Spring Security][OAuth2] OpenID Connect(OIDC) 구현 (2)

개요지난 글에서 OAuth2를 이용해서 로그인하는 방법을 알아보았습니다. 이번에는 OpenID Connect 를 이용해서 로그인을 구현해보도록 하겠습니다.  OIDC란?OpenID Connect(OIDC)는 OAuth 2.0 프로토콜 위에

tmd8633.tistory.com

 

 

6-2. saml2Login()

  • SAML 2.0 인증 설정
  • SSO(Single Sign On) 구현
http.saml2Login(saml2 -> saml2
    .relyingPartyRegistrationRepository(relyingPartyRegistrationRepository));

이 부분도 넘어가겠습니다 아직 여긴 공부 안했습니다.


 

7. 요청 처리

7-1. securityMatcher()

  • Security 기반 URL 접근 제어
http.securityMatcher("/**") // 해당 URL만 아래 Security 설정 적용
    .authorizeHttpRequests(request -> request
        .anyRequest().permitAll()
    )

 

REST API, WEB 환경에서 각각 Security 설정을 부여하고 싶은 경우

 

@Bean
    @Order(1)
    SecurityFilterChain apiSecurityFilterChain(HttpSecurity http) throws Exception {

        http.securityMatcher("/api/**")
            .authorizeHttpRequests(request -> request
                .requestMatchers("/api/user").hasAnyRole(Role.USER.name(), Role.ADMIN.name())
                .requestMatchers("/api/admin").hasRole(Role.ADMIN.name())
                .anyRequest().permitAll()
            );

        return http.build();
    }

    @Bean
    @Order(2)
    SecurityFilterChain webSecurityFilterChain(HttpSecurity http, RememberMeServices rememberMeServices) throws Exception {

        http.securityMatcher("/**")
            .authorizeHttpRequests(request -> request
                .requestMatchers("/user/**").hasAnyRole(Role.USER.name(), Role.ADMIN.name())
                .requestMatchers("/admin/**").hasRole(Role.ADMIN.name())
                .anyRequest().permitAll()
            );

        return http.build();
    }

securityMatcher 경로가 세부적일 수록 Order 우선순위가 높아야합니다.

 

// 이러면 절대안됨!!! 
http.securityMatcher("/api/**")
    .authorizeHttpRequests(request -> request
        .requestMatchers("/api/user").hasAnyRole(Role.USER.name(), Role.ADMIN.name())
        .requestMatchers("/api/admin").hasRole(Role.ADMIN.name())
        .anyRequest().permitAll()
    )
    .securityMatcher("/**")
    .authorizeHttpRequests(request -> request
        .requestMatchers("/user/**").hasAnyRole(Role.USER.name(), Role.ADMIN.name())
        .requestMatchers("/admin/**").hasRole(Role.ADMIN.name())
        .anyRequest().permitAll()
    );

이어 붙힐 수 있다고 이렇게 붙히면 안됩니다. 반드시 SecurityFilterChain으로 연결해서 두개의 Filter로 사용해야 됩니다.

 

 

7-2. authorizeHttpRequests()

  • URL 기반 접근 제어
  • 권한별 리소스 접근 설정
http.authorizeHttpRequests(auth -> auth
    .requestMatchers("/admin/**").hasRole("ADMIN")
    .requestMatchers("/user/**").hasRole("USER")
    .anyRequest().authenticated());

아래 게시물 권한설정 파트 보시면 됩니다.

 

 

[Spring Security] 스프링 시큐리티 시작하기 (로그인, 로그아웃과 권한) (2)

이전글 [Spring Security] 스프링 시큐리티 이해하기 (1)Spring SecuritySpring Security는 인증, 인가 및 일반적인 공격에 대한 보호를 제공하는 프레임워크 입니다. 서비스를 개발할때 필수적으로 구현해야

tmd8633.tistory.com

 

 

8. 예외처리

8-1. exceptionHandling()

  • 보안 예외 처리
  • 인증 / 인가 실패 처리
http.exceptionHandling(exception -> exception
    .authenticationEntryPoint(customAuthEntryPoint)
    .accessDeniedHandler(customAccessDeniedHandler));

 

아래 글에서 ExceptionHandling에 대해서 다뤘습니다.

 

 

[Spring Security] 스프링 시큐리티 Anonymous과 ExceptionHandling (4)

Anonymous 인증이란?Spring Security의 Anonymous 인증은 인증되지 않은 사용자(로그인하지 않은 사용자)를 처리하는 메커니즘입니다. 인증되지 않은 요청에 대해 AnonymousAuthenticationToken을 생성하여 보안

tmd8633.tistory.com

 

 

9. 기타 설정

9-1. requestCache()

  • 요청 캐시 설정
  • 인증 후 리다이렉트 요청 복원 기능
http.requestCache(cache -> cache
    .requestCache(new NullRequestCache()));

리다이렉트 요청에 대한 정보를 저장하는 곳을 설정하는 부분입니다. 설정은 securityContext() 부분과 비슷한 점이 많습니다.

 

// SessionManagementConfigurer.init(H http)

RequestCache requestCache = (RequestCache)http.getSharedObject(RequestCache.class);
if (requestCache == null && stateless) {
    http.setSharedObject(RequestCache.class, new NullRequestCache());
}

// .. 생략

SessionManagement STATELESS 설정을 하면 알아서 NullRequestCache를 넣어주는걸 볼 수 있습니다. Session 을 사용한다면 설정하지 않아도되고, API를 사용해서 Session Stateless 하려면 sessionManagement에서 STATELESS 설정만 해주면 자동으로 NullRequestCache가 설정되니까 별로 신경안써도 되는것 같습니다.

 

 

9-2. portMapper()

  • HTTP/HTTPS 포트 매핑
  • 보안 채널 전환
http.portMapper(ports -> ports
    .http(8080).mapsTo(8443)    // 8080 -> 8443
    .http(80).mapsTo(443)       // 80 -> 443
);



10. Filter

10-1. addFilter

http.addFilterBefore(new CustomFilter(), UsernamePasswordAuthenticationFilter.class)  // 특정 필터 이전에 추가
    .addFilterAfter(new CustomFilter(), UsernamePasswordAuthenticationFilter.class)   // 특정 필터 이후에 추가
    .addFilterAt(new CustomFilter(), UsernamePasswordAuthenticationFilter.class);     // 특정 필터 위치에 추가

 

자신이 만든 필터를 Security Filter에 추가 시킬 수 있습니다. 그냥 addFilter(new CustomFilter()) 이렇게도 사용할 수는 있지만 특정 필터를 기준으로 Filter를 추가하는게 명확한 필터 순서를 제어할 수 있기때문에 Before, After, At을 사용하는것을 권장합니다.

 

 

마무리

후 이 글 쓰는데 오래걸렸습니다. Spring Security 에 대해서 계속 공부하고는 있는데 공식문서만 들어가기만 하면 정신이 혼미해지네요. 내용도 많고 서로 얽혀있는게 많아서 이해하면서 읽기가 쉽지가 않았습니다. 그래서 중간점검이나 할겸 조금 정리해봤는데 틀린 부분도 있을 겁니다. 그러니 걸러가면서 읽어주시기 바랍니다.

개요

지난 글에서 OAuth2를 이용해서 로그인하는 방법을 알아보았습니다. 이번에는 OpenID Connect 를 이용해서 로그인을 구현해보도록 하겠습니다.

 

 

OIDC란?

OpenID Connect(OIDC)는 OAuth 2.0 프로토콜 위에 구축된 인증 레이어입니다. OAuth 2.0이 권한 부여(Authorization)에 중점을 둔다면, OIDC는 여기에 인증(Authentication) 기능을 추가하여 보다 완벽한 ID 관리 솔루션을 제공합니다.

 

 

OIDC의 흐름입니다. 여기서 주목해야할 점은 ID token입니다. OAuth2 방식과 다른점은 유저 정보를 포함하는지 여부라고 할 수 있습니다. 유저정보를 포함하는 것만으로 요청회수를 반으로 줄일 수 있습니다.

OAuth2 OIDC
Access Token 발급 요청 Access Token, ID Token 발급 요청
Access Token 발급 Access Token, ID Token 발급
유저 프로필 발급 요청  
유저 프로필 발급  

 

 

ID token 예시

{
  "iss": "https://auth.example.com",
  "sub": "user123",
  "aud": "client_id",
  "exp": 1516239022,
  "iat": 1516235422,
  "auth_time": 1516235422,
  "nonce": "n-0S6_WzA2Mj",
  "name": "John Doe",
  "email": "johndoe@example.com"
}

 

  • iss : 토큰 발급자
  • sub : 리소스 내 유저 식별자
  • aud : Client Id
  • exp : 토큰 만료시간
  • lat : 토큰 발급 시간

 

 

 

 

구현

먼저 카카오와 구글에서 OpenID Connect를 사용할 수 있도록 해줍시다.

 

 

카카오 로그인을 클릭하고 OpenID Connect 활성화 상태를 ON으로 바꿔줍니다.

 

 

 

구글 범위 설정에서 openid를 추가해주고

application.yaml scope에도 openid를 추가해줍시다.

 

 

OidcUserDetails

public class OidcUserDetails extends DefaultOidcUser {

    private final User user;

    public OidcUserDetails(User user, Collection<? extends GrantedAuthority> authorities, OidcIdToken idToken, OidcUserInfo userInfo, String nameAttributeKey) {
        super(authorities, idToken, userInfo, nameAttributeKey);
        this.user = user;
    }

}

 

 

@Service
@RequiredArgsConstructor
public class CustomOidcUserService extends OidcUserService {

    private final UserDB db;

    @Override
    public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
        OAuth2User oAuth2User = super.loadUser(userRequest);

        Provider provider = Provider.findByProvider(userRequest.getClientRegistration().getClientName());
        DefaultOAuth2User convert = provider.convert(oAuth2User);

        String username = provider + "_" + convert.getProviderId();

        OidcUser oidcUser = super.loadUser(userRequest);
        String userNameAttributeName = userRequest
            .getClientRegistration()
            .getProviderDetails()
            .getUserInfoEndpoint()
            .getUserNameAttributeName();

        User user = db.findByUsername(username)
            .orElseGet(() -> db.save(User.builder()
                .username(username)
                .role(Role.USER)
                .name(convert.getName())
                .provider(provider)
                .providerId(convert.getProviderId())
                .build())
            );
        return new OidcUserDetails(
            user,
            Set.of(new SimpleGrantedAuthority(Role.USER.getRoleName())),
            oidcUser.getIdToken(),
            oidcUser.getUserInfo(),
            userNameAttributeName);
    }
}

 

이전 OAuth2UserService와 매우 흡사합니다. 

 

 

SecurityConfig

        .oauth2Login(login -> login
            .defaultSuccessUrl("/user?social")
            .userInfoEndpoint(userInfo -> userInfo
                .oidcUserService(customOidcUserService)
            )
            .failureHandler((request, response, exception) -> {
                exception.printStackTrace();
                response.sendRedirect("/login?error");
            })
        )

.userService() 를 지우고 .oidcUserService에 OidcUserService를 넣어줍시다.

 

 

 

 

OAuth2AuthorizedClient

@Entity
@Table(name = "oauth2_authorized_client")
@IdClass(OAuth2AuthorizedClientId.class)
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class OAuth2AuthorizedClient {

    @Id
    private String clientRegistrationId;

    @Id
    private String principalName;

    private String accessTokenType;

    @Lob
    private byte[] accessTokenValue;

    private LocalDateTime accessTokenIssuedAt;

    private LocalDateTime accessTokenExpiresAt;

    private String accessTokenScopes;

    @Lob
    private byte[] refreshTokenValue;

    private LocalDateTime refreshTokenIssuedAt;

    @Column(columnDefinition = "timestamp default current_timestamp")
    private LocalDateTime createdAt;
    
}

 

@Data
@NoArgsConstructor
@AllArgsConstructor
public class OAuth2AuthorizedClientId implements Serializable {
    private String clientRegistrationId;
    private String principalName;
}

 

실제로 잘 작동하는지 확인해보도록하겠습니다. 위와 같이 테이블을 만들어줍니다. 이 스키마는 공식문서에서 제공하는 데이터를 기반으로 작성되었습니다.

 

Security Database Schema :: Spring Security

The standard JDBC implementation of the UserDetailsService (JdbcDaoImpl) requires tables to load the password, account status (enabled or disabled) and a list of authorities (roles) for the user. You can use these as a guideline for defining the schema for

docs.spring.io

 

@Configuration
public class OAuth2Config {

    @Bean
    OAuth2AuthorizedClientService authorizedClientService(ClientRegistrationRepository clientRegistrationRepository, JdbcTemplate jdbcTemplate) {
        return new JdbcOAuth2AuthorizedClientService(jdbcTemplate, clientRegistrationRepository);
    }
}

OAuth2AuthorizedCLientService를 JdbcOAuth2AuthorizedClientService로 넣어줍니다.

 

 

 

결과

 

이제 로그인을 해주면 잘 뜹니다! 만약 Google 로그인 시에 RefreshToken 값이 비어있다면

        provider:
          google:
            authorization-uri: https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent

access_type=offline, prompt=consent를 추가하면 refreshToken도 보내줄겁니다.

 

 

 

 

결론

  OAuth2 OIDC
중점 인가(Authorization) 인증(Authentication)
내용 AccessToken AccessToken + ID Token
용도 API 이용 권한 SSO(Single Sign On) 통합 로그인

 

OAuth2

Spring Security의 OAuth 2.0 지원은 크게 두 가지 주요 기능으로 구성됩니다:

  1. OAuth2 Resource Server
  2. OAuth2 Client
    • OAuth2 Log In

추가로 OAuth2 Log In이라는 특별한 기능이 있는데, 이는 독립적으로 존재할 수 없으며 OAuth2 Client 기능을 기반으로 동작합니다. 이러한 기능들은 OAuth 2.0 Authorization Framework에서 정의하는 리소스 서버와 클라이언트 역할을 담당하며, 인증 서버 역할은 Spring Authorization Server라는 별도 프로젝트를 통해 제공됩니다.

 

OAuth2 문서도 방대하기 때문에 하나씩 차근차근 알아가 보도록 하겠습니다.

 

 

 

시작하기 전에

지금까지 사용하던 InMemoryDB를 더는 사용하지 않고 MySQL을 사용합니다.

application.properties -> application.yaml 을 사용합니다.

 

@EqualsAndHashCode(of = "id")
@AllArgsConstructor @NoArgsConstructor
@Builder
@Entity
public class User {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String username;
    private String password;
    @Enumerated(EnumType.STRING)
    private Role role;
    private String name;

    @Enumerated(EnumType.STRING)
    private Provider provider;
    private String providerId;

}

변경된 User

Provider에 대해선 아래에 나옵니다.

 

 

// 신규
@AllArgsConstructor
@Getter
public enum Role {

    USER("ROLE_USER"),
    ADMIN("ROLE_ADMIN")
    ;
    private final String roleName;
}

// SecurityConfig

        http.authorizeHttpRequests(request -> request
            .requestMatchers("/user/**").hasAnyRole(Role.USER.name(), Role.ADMIN.name())
            .requestMatchers("/admin/**").hasRole(Role.ADMIN.name())
            .requestMatchers("/join", "/signup").anonymous()
            .anyRequest().permitAll()
        );
        
 // UserDetails
 
     @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of(new SimpleGrantedAuthority(user.getRole().getRoleName()));
    }

변경된 권한 인증방법

 

 

Google Cloud Platform

사용자 인증정보에서 승인된 리디렉션 URI를
http://localhost:8080/login/oauth2/code/google

로 설정해줍니다.

 

의존성 주입

implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'

OAuth2 Log In은 OAuth2 Client 기반으로 동작하는 기능입니다. 따라서 OAuth2 Client 를 받아줍니다.

 

 

 

Entity 설정

이번에 적용할건 Goole, Kakao 소셜로그인 입니다. 먼저 Provider를 정의해줍니다.

 

public enum Provider {

    GOOGLE,
    KAKAO;

}

 

그리고 User Entity에 넣어줍니다.

 

 

application.yaml

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: GOOGLE_CLIENT_ID
            client-secret: GOOGLE_SECRET
            scope:
              - email
              - profile

 

구글로그인 설정할때 받아둔 Client ID와 Secret Key를 넣어줍니다. scope는 

 

범위에서 설정한 범위를 말하는겁니다.

 

 

OAuth2UserService

UserDetailsService와 구현방법이 동일합니다.

@Service
@RequiredArgsConstructor
public class PrincipalOAuth2UserService extends DefaultOAuth2UserService {

    @Override
    public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
        return super.loadUser(userRequest);
    }
    
}

DefaultOAuth2UserService를 상속받아줍니다.

loadUser에서 소셜로그인을 시도한 유저의 데이터를 확인하고 로그인 처리를 해주면 됩니다.

 

@Service
@RequiredArgsConstructor
public class PrincipalOAuth2UserService extends DefaultOAuth2UserService {

    private final UserDB db;

    @Override
    public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
        OAuth2User oAuth2User = super.loadUser(userRequest);
        System.out.println(oAuth2User.getAttributes());

        Provider provider = Provider.GOOGLE;
        String id = oAuth2User.getName();

        String username = provider + "_" + id;

        User user = db.findByUsername(username)
            .orElseGet(() -> db.save(User.builder()
                .username(username)
                .role(Role.USER)
                .name(convert.name())
                .provider(provider)
                .providerId(id)
                .build())
            );
        return new PrincipalDetails(user);
    }
}

 

먼저 Google 로그인부터 구현해보겠습니다.

  1. super.loadUser 에서 소셜에서 인증된 사용자를 받아옵니다.
  2. Provier + "_" + id 를 username으로 사용합니다.
  3. DB에서 username으로 유저를 찾습니다. 없으면 db에 새로운 유저를 넣어줍니다.
  4. PrincipalDetails로 반환합니다.

여기에서 id는 소셜의 고유번호입니다. id가 1234134513 인 경우, GOOGLE_1234134513 이 username이 됩니다.

회원가입에 대한 추가 로직이 존재하면 코드를 변경하여 추가 정보를 받도록 변경하면됩니다.

 

 

 

SecurityConfig

            http.oauth2Login(login -> login
                .loginPage("/join")
                .defaultSuccessUrl("/user")
                .userInfoEndpoint(endPoint -> endPoint
                    .userService(principalOAuth2UserService)
                )
            )

userInfoEndPoint에서 userService에 위에서 만든 PrincipalOAuth2UserService를 넣어줍니다.

 

 

HTML

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form th:action="@{/join}" th:method="post">
        <input type="text" name="username">
        <input type="password" name="password">
        <label>
            <input type="checkbox" name="remember-me" /> Remember Me
        </label>
        <input type="submit" value="로그인">
    </form>
    <a href="/oauth2/authorization/google">구글로그인</a>
    <a href="/oauth2/authorization/kakao">카카오로그인</a>
</body>
</html>

 

/oauth2/authorization/google

/oauth2/authorization/kakao

를 추가해줍니다. 카카오는 이후에 개발하겠습니다.

 

 

결과

이제 서버를 키고 구글로그인을 눌러 로그인을 시도해봅시다.

잘 작동합니다.

 

뭔가 이상합니다. 저희는 기존코드에는 손도 안대고 security 에 oAuth2Login에 Service만 끼워넣었을뿐인데 기능이 동작합니다. 

그리고 '/oauth2/authorization/google'나 redirect URI인 '/login/oauth2/code/google' 부분은 Controller도 건드리지 않았는데 어떻게 동작한걸까요?

 

 

 

 

코드 파보기

 

CommonOAuth2Provider

지금 이 글을 읽고 있는 시점에 얼마나 많은 사람이 Google 소셜로그인을 개발했을까요? 수 백만명은 될겁니다. 그렇기에 Spring Security에서 가장 많이 사용하는 소셜은 미리 구현해놓은 겁니다.

 

public enum CommonOAuth2Provider {
    GOOGLE {
        public ClientRegistration.Builder getBuilder(String registrationId) {
            ClientRegistration.Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, "{baseUrl}/{action}/oauth2/code/{registrationId}");
            builder.scope(new String[]{"openid", "profile", "email"});
            builder.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth");
            builder.tokenUri("https://www.googleapis.com/oauth2/v4/token");
            builder.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs");
            builder.issuerUri("https://accounts.google.com");
            builder.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo");
            builder.userNameAttributeName("sub");
            builder.clientName("Google");
            return builder;
        }
    },
    GITHUB {
        public ClientRegistration.Builder getBuilder(String registrationId) {
            ClientRegistration.Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, "{baseUrl}/{action}/oauth2/code/{registrationId}");
            builder.scope(new String[]{"read:user"});
            builder.authorizationUri("https://github.com/login/oauth/authorize");
            builder.tokenUri("https://github.com/login/oauth/access_token");
            builder.userInfoUri("https://api.github.com/user");
            builder.userNameAttributeName("id");
            builder.clientName("GitHub");
            return builder;
        }
    },
    FACEBOOK {
        public ClientRegistration.Builder getBuilder(String registrationId) {
            ClientRegistration.Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_POST, "{baseUrl}/{action}/oauth2/code/{registrationId}");
            builder.scope(new String[]{"public_profile", "email"});
            builder.authorizationUri("https://www.facebook.com/v2.8/dialog/oauth");
            builder.tokenUri("https://graph.facebook.com/v2.8/oauth/access_token");
            builder.userInfoUri("https://graph.facebook.com/me?fields=id,name,email");
            builder.userNameAttributeName("id");
            builder.clientName("Facebook");
            return builder;
        }
    },
    OKTA {
        public ClientRegistration.Builder getBuilder(String registrationId) {
            ClientRegistration.Builder builder = this.getBuilder(registrationId, ClientAuthenticationMethod.CLIENT_SECRET_BASIC, "{baseUrl}/{action}/oauth2/code/{registrationId}");
            builder.scope(new String[]{"openid", "profile", "email"});
            builder.userNameAttributeName("sub");
            builder.clientName("Okta");
            return builder;
        }
    };

    private static final String DEFAULT_REDIRECT_URL = "{baseUrl}/{action}/oauth2/code/{registrationId}";

    private CommonOAuth2Provider() {
    }

    protected final ClientRegistration.Builder getBuilder(String registrationId, ClientAuthenticationMethod method, String redirectUri) {
        ClientRegistration.Builder builder = ClientRegistration.withRegistrationId(registrationId);
        builder.clientAuthenticationMethod(method);
        builder.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE);
        builder.redirectUri(redirectUri);
        return builder;
    }

    public abstract ClientRegistration.Builder getBuilder(String registrationId);
}

 

GOOGLE, GITHUB, FACEBOOK, OKTA 가 보입니다.

redirect url에 "{baseUrl}/{action}/oauth2/code/{registrationId}" 라고 적혀있네요. action에는 login이 들어가면 우리가 구글 클라우드 플랫폼에서 설정한 그 url과 일치합니다.

 

OAuth2LoginAuthenticationFIlter

public class OAuth2LoginAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    public static final String DEFAULT_FILTER_PROCESSES_URI = "/login/oauth2/code/*";
    private static final String AUTHORIZATION_REQUEST_NOT_FOUND_ERROR_CODE = "authorization_request_not_found";
    // ... 생략
}

 

 

OAuth2AuthorizationRequestRedirectFilter

public class OAuth2AuthorizationRequestRedirectFilter extends OncePerRequestFilter {
    public static final String DEFAULT_AUTHORIZATION_REQUEST_BASE_URI = "/oauth2/authorization";
    private final ThrowableAnalyzer throwableAnalyzer;
    // ... 생략
}

 

필터에서 해당 URI를 Default로 가지고있습니다. 그래서 우리가 구현하지 않고도 동작한거죠.

 

그럼 CommonOAuth2Provider에 없는 카카오나 네이버 같은 소셜은 어떻게 해야할까요? 처음부터 전부 다 구현해야할까요?

아닙니다. 쉽게 확장할 수 있습니다. 그건 아래에서 같이 따라해보면서 알아보도록 하겠습니다.

 

 

경로 설정

위의 Default 경로를 변경하고 싶다면 아래와 같이 baseUri를 설정하면 됩니다.

 

    http.oauth2Login(oauth2 -> oauth2
        .loginPage("/login/oauth2")
        ...
        .authorizationEndpoint(authorization -> authorization
            .baseUri("/login/oauth2/authorization")
            ...
        )
    );

접속 경로를 변경하고싶은 경우 authorizationEndPoint baseUri를 변경

 

    http.oauth2Login(oauth2 -> oauth2
        .redirectionEndpoint(redirection -> redirection
            .baseUri("/login/oauth2/callback/*")
            ...
        )
    );

Redirect Url을 변경하고싶은 경우 redirectionEndpoint baseUrl를 변경하고,

spring:
  security:
    oauth2:
      client:
        registration:
          kakao:
            client-name: 'kakao'
            redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'

redirect-uri를 변경하면 됩니다.

카카오 로그인

redirect URI가 위와 같이 잘 설정되어있는지 확인합니다.

 

application.yaml

spring:
  security:
    oauth2:
      client:
        registration:
          kakao:
            client-name: 'kakao'
            client-id: KAKAO_REST_API_KEY
            redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
            authorization-grant-type: authorization_code
            client-authentication-method: client_secret_post
            scope:
              - profile_nickname
              - profile_image
              - account_email

        provider:
          kakao:
            authorization-uri: 'https://kauth.kakao.com/oauth/authorize'
            token-uri: 'https://kauth.kakao.com/oauth/token'
            user-info-uri: 'https://kapi.kakao.com/v2/user/me'
            user-name-attribute: 'id'

 

카카오 REST API 키를 client-id에 넣어줍니다. scope는 카카오 로그인 동의항목에서 선택한 ID를 넣어주면됩니다.

저는 프로필, 이름, 이메일을 가져오도록 했습니다.

user-name-attribute는 OAuth2User 객체에 name에 들어가는 파라미터의 Key값입니다.

 

 

데이터 구조

먼저 PrincipalOAuth2UserService에 loadUser부터 수정해주어야 합니다. GOOGLE로 하드 코딩된걸 수정할겁니다.

근데 여기서 약간의 문제가 있습니다. Google에 ID 값은 'sub'이고 Kakao에 ID 값은 'id' 인 부분은 user-name-attribute로 통일했기때문에 getName()으로 providerId를 가져오는데 문제가 되지 않지만 이름, 사진, 이메일은 구글과 카카오의 데이터 구조가 완전히 다릅니다. 따라서 converter 해줄 객체가 필요합니다.

 

@Builder
public record DefaultOAuth2User(String providerId, 
                                String email, 
                                String name, 
                                String picture) {
}

매핑될 객체를 하나 만들어주고,

 

@Getter
@AllArgsConstructor
public enum Provider {

    GOOGLE(user -> DefaultOAuth2User.builder()
        .providerId(user.getName())
        .email(user.getAttribute("email"))
        .name(user.getAttribute("name"))
        .picture(user.getAttribute("picture"))
        .build()),
    
    KAKAO(user -> {
        Map<String, Object> kakaoAccount = user.getAttribute("kakao_account");
        Map<String, Object> attribute = (Map<String, Object>) kakaoAccount.get("profile");

        return DefaultOAuth2User.builder()
            .providerId(user.getName())
            .email((String) kakaoAccount.get("email"))
            .name((String) attribute.get("nickname"))
            .picture((String) attribute.get("profile_image_url"))
            .build();
    });

    private final Function<OAuth2User, DefaultOAuth2User> converter;

    public static Provider findByProvider(String clientName) {
        return Provider.valueOf(clientName.toUpperCase());
    }

    public DefaultOAuth2User convert(OAuth2User user) {
        return converter.apply(user);
    }

}

 

Provider enum객체를 활용해서 다음과 같이 매핑해주었습니다. convert로 객체를 변환해주는 역할을 하게 되었습니다.

 

사실 위 코드가 마음에 안들어서

public sealed interface OAuth2Converter permits
    OAuth2GoogleConverter,
    OAuth2KakaoConverter {

    DefaultOAuth2User convert(OAuth2User user);
}

public final class OAuth2GoogleConverter implements OAuth2Converter {

    @Override
    public DefaultOAuth2User convert(OAuth2User user) {
        return DefaultOAuth2User.builder()
            .providerId(user.getName())
            .email(user.getAttribute("email"))
            .name(user.getAttribute("name"))
            .picture(user.getAttribute("picture"))
            .build();
    }
}

public final class OAuth2KakaoConverter implements OAuth2Converter {

    @Override
    public DefaultOAuth2User convert(OAuth2User user) {
        Map<String, Object> kakaoAccount = user.getAttribute("kakao_account");
        Map<String, Object> attribute = (Map<String, Object>) kakaoAccount.get("profile");

        return DefaultOAuth2User.builder()
            .providerId(user.getName())
            .email((String) kakaoAccount.get("email"))
            .name((String) attribute.get("nickname"))
            .picture((String) attribute.get("profile_image_url"))
            .build();
    }
}
@Getter
@AllArgsConstructor
public enum Provider {

    GOOGLE(new OAuth2GoogleConverter()),
    KAKAO(new OAuth2KakaoConverter());

    private final OAuth2Converter converter;

    public static Provider findByProvider(String clientName) {
        return Provider.valueOf(clientName.toUpperCase());
    }

    public DefaultOAuth2User convert(OAuth2User user) {
        return converter.convert(user);
    }

}

 

이렇게 하니까 좀더 깔끔해졌습니다.

 

 

PrincipalOAuth2UserService

@Service
@RequiredArgsConstructor
public class PrincipalOAuth2UserService extends DefaultOAuth2UserService {

    private final UserDB db;

    @Override
    public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
        OAuth2User oAuth2User = super.loadUser(userRequest);

        Provider provider = Provider.findByProvider(userRequest.getClientRegistration().getClientName());
        DefaultOAuth2User convert = provider.convert(oAuth2User);

        String username = provider + "_" + convert.providerId();

        User user = db.findByUsername(username)
            .orElseGet(() -> db.save(User.builder()
                .username(username)
                .role(Role.USER)
                .name(convert.name())
                .provider(provider)
                .providerId(convert.providerId())
                .build())
            );
        return new PrincipalDetails(user);
    }
}

 

 

결과

이제 테스트해보면 카카오 로그인도 아주 간단하게 연동했습니다. 

 

application.yaml 을 사용하지 않고 추가하는 방법도 있습니다.

@Configuration
public class OAuth2LoginConfig {

	@Bean
	public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
		http
			.authorizeHttpRequests(authorize -> authorize
				.anyRequest().authenticated()
			)
			.oauth2Login(withDefaults());
		return http.build();
	}

	@Bean
	public ClientRegistrationRepository clientRegistrationRepository() {
		return new InMemoryClientRegistrationRepository(this.googleClientRegistration());
	}

	private ClientRegistration googleClientRegistration() {
		return ClientRegistration.withRegistrationId("google")
			.clientId("google-client-id")
			.clientSecret("google-client-secret")
			.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
			.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
			.redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
			.scope("openid", "profile", "email", "address", "phone")
			.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
			.tokenUri("https://www.googleapis.com/oauth2/v4/token")
			.userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
			.userNameAttributeName(IdTokenClaimNames.SUB)
			.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
			.clientName("Google")
			.build();
	}
}

 

그런데 key가 노출되기때문에 사용은 안할겁니다. 궁금하시다면 공식문서를 참고해주세요.

https://docs.spring.io/spring-security/reference/servlet/oauth2/login/core.html

 

 

 

결론

소셜로그인을 추가하는건 생각보다 간단합니다. 혼자 공부하면서 작성한거라 틀린부분이 있을 수 있습니다. 언제든지 지적해주세요.

 

 

 

카카오 로그인

두번째로 카카오 로그인도 준비하겠습니다.

 

 

 

1. 카카오 개발자 접속

 

Kakao Developers

카카오 API를 활용하여 다양한 어플리케이션을 개발해보세요. 카카오 로그인, 메시지 보내기, 친구 API, 인공지능 API 등을 제공합니다.

developers.kakao.com

 

내 애플리케이션 - 애플리케이션 추가하기 눌러서 애플리케이션을 만들어줍시다.

 

 

 

 

2. REST API 키 보관

애플리케이션에 들어와서 앱 키 메뉴에 들어가보면 REST API 키가 있습니다. 이 키를 보관해줍니다.

 

 

 

3. 플랫폼 설정

 

플랫폼 메뉴에 들어가면 Android, iOS, Web이 있습니다. 도메인을 설정해줍니다.

 

 

 

4. 카카오 로그인 설정

 

카카오 로그인을 들어와서 활성화 해줍니다.

밑에 Redirect URI가 있습니다. 

저는 http://localhost:8080/login/oauth2/code/kakao 이렇게 설정햇습니다.

 

5. 동의항목 설정

카카오 로그인 바로 밑에 동의항목 메뉴가 있습니다. 들어가줍니다.

 

 

서비스에 맞게 가져올 데이터를 선택해줍니다.

 

 

 

 

 

끝!

+ Recent posts