Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
요구사항
프로젝트 마일스톤
주요 변경 사항
프로젝트 버전이 변경되었습니다.
v2.1-M10세부 사항
2.1:api-doc버전을 따릅니다.M10: 미션 10을 의미합니다.프론트엔드가 변경되었습니다.
v2.1.21. JWT 컴포넌트 구현
JWT 의존성을 추가하세요.
implementation 'com.nimbusds:nimbus-jose-jwt:10.3'토큰을 발급, 갱신, 유효성 검사를 담당하는 컴포넌트(
JwtTokenProvider)를 구현하세요.[s0yti3992-image.png](https://bakey-api.codeit.kr/api/files/resource?root=static&seqId=14437&version=1&directory=/s0yti3992-image.png&name=s0yti3992-image.png)
2. 리팩토링 - 로그인
미션 9와 마찬가지로 Spring Security의 formLogin + 미션 9의 인증 흐름은 그대로 유지하면서 필요한 부분만 대체합니다.
세션 생성 정책을
STATELESS로 변경하고,sessionConcurrency설정을 삭제하세요.AuthenticationSuccessHandler컴포넌트를 대체하세요.기존 구현체는
LoginSuccessHandler입니다.JwtLoginSuccessHandler를 정의하고 대체하세요.인증 성공 시
JwtProvider를 활용해 토큰을 발급하세요.REFRESH_TOKEN)에 저장하세요.200 JwtDto로 응답합니다.[7s8mi349r-image.png](https://bakey-api.codeit.kr/api/files/resource?root=static&seqId=14438&version=1&directory=/7s8mi349r-image.png&name=7s8mi349r-image.png)
설정에 추가하세요.
3. JWT 인증 필터 구현
엑세스 토큰을 통해 인증하는 필터(
JwtAuthenticationFilter)를 구현하세요.요청 당 한번만 실행되도록
OncePerRequestFilter를 상속하세요.요청 헤더(
Authorization)에 Bearer 토큰이 포함된 경우에만 인증을 시도하세요.JwtProvider를 통해 엑세스 토큰의 유효성을 검사하세요.유효한 토큰인 경우
UsernamePasswordAuthenticationToken객체를 활용해 인증 완료 처리하세요.4. 리프레시 토큰을 활용한 엑세스 토큰 재발급
POST /api/auth/refreshHeader Cookie: REFRESH_TOKEN=…200 JwtDto401 ErrorResponsepermitAll설정에 포함하세요.GET /auth/me)2.0.x과 마찬가지로2.1.x에서는 사용자 정보와 엑세스 토큰 정보를 브라우저의 메모리에서 관리합니다.5. 리팩토링 - 로그아웃
쿠키에 저장된 리프레시 토큰을 삭제하는
LogoutHandler를 구현하세요.구현한 핸들러를 추가하세요.
6. 심화) 리팩토링 - 토큰 상태 관리
토큰 기반 인증 방식은 세션 기반 인증 방식과 달리 무상태(stateless)이기 때문에 사용자의 로그인 상태를 제어하기 어렵습니다.
따라서
SessionRegistry를 통해 세션의 상태를 관리했던 것처럼, JWT의 상태를 관리할 수 있는 컴포넌트를 추가해야합니다.토큰의 상태를 관리하는
JwtRegistry를 구현하세요.[9da9kvl8y-image.png](https://bakey-api.codeit.kr/api/files/resource?root=static&seqId=14442&version=1&directory=/9da9kvl8y-image.png&name=9da9kvl8y-image.png)
JwtRegistryregisterJwtInformationJwtInformation을 등록합니다.1)를 제어합니다.invalidateJwtInformationByUserId: UserId로 해당 유저의 모든JwtInformation정보를 삭제합니다.hasActiveJwtInformationBy*:JwtInformation이 Registry에 존재하는지 확인합니다.ByUserId: 사용자의 로그인 상태를 판단할 때 활용합니다.ByAccessToken: 필터에서 유효한 토큰인지 확인할 때 활용합니다.ByRefreshToken: 토큰 재발급 시 유효한 토큰인지 확인할 때 활용합니다.rotateJwtInformation: 토큰 재발급 시 토큰 로테이션을 수행합니다.clearExpiredJwtInformation: 만료된JwtInformation을 삭제합니다.InMemoryJwtRegistry메모리에
JwtInformation을 저장하는JwtRegistry구현체입니다.동시성 처리를 위해 다음과 같이 구성하세요. 동시성에 대해서는 다음 미션에서 학습합니다.
JwtAuthenticationFilter에서JwtRegistry를 활용해 토큰의 상태를 검사하는 로직을 추가하세요.JwtRegistry를 활용해 동시 로그인 제한 기능을 리팩토링하세요.JwtRegistry를 활용해 권한이 변경된 사용자가 로그인 상태라면 강제로 로그아웃되도록 하세요.JwtRegistry를 활용해 사용자의 로그인 여부를 판단하도록 리팩토링하세요.JwtLogoutHandler에서JwtRegistry를 활용해 로그아웃 시 토큰을 무효화하세요.Authentication정보가 없을 수 있습니다.주기적으로 만료된 토큰 정보를 레지스트리에서 삭제하세요.
@EnableScheduling를 추가하세요.@Scheduled를 활용해서 5분마다 만료된 토큰을 삭제하세요.멘토에게