-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: Jwt 토큰 관련 클래스를 상속이 아니라 조합하도록 #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nayonsoso
merged 17 commits into
solid-connection:develop
from
nayonsoso:refactor/296-use-composition-for-token
May 22, 2025
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c089899
refactor: 스프링 빈으로 활용하기 위해 private 생성자 제거
nayonsoso 89008fa
refactor: 헤더에서 토큰 추출하는 코드 클래스로 분리
nayonsoso 8526aab
refactor: 토큰의 블랙리스트를 관리하는 코드 분리
nayonsoso 4b912a6
refactor: 토큰 제공과 관련된 코드 TokenProvider로 이동
nayonsoso 0e44f1d
refactor: TokenProvider를 abstract 클래스가 아니라 컴포넌트로
nayonsoso 0df626f
refactor: TokenProvider를 상속하여 쓰던 곳에서 주입받아 쓰도록
nayonsoso 28deb3d
refactor: JwtUtils를 쓰던 곳에서 TokenProvider 쓰도록
nayonsoso ed4f4ce
refactor: TokenProvider의 함수 인자 수정
nayonsoso 9ceb6d2
test: TokenProvider 테스트 코드 작성
nayonsoso 578db3e
refactor: 더 작은 작업에서 예외를 발생시키도록
nayonsoso ac0d81e
chore: 개행 수정, 안쓰는 의존 제거
nayonsoso a10c991
refactor: 제대로 된 DIP를 위해 패키지 이동
nayonsoso 2eddf88
chore: todo 삭제
nayonsoso e0da40b
refactor: jwt 토큰 관련 코드 패키지 이동
nayonsoso 20a7364
refactor: TokenProvider 인터페이스 생성
nayonsoso 756bb08
refactor: TokenProvider 인터페이스에 의존하도록
nayonsoso d921d2d
style: 개행 삭제
nayonsoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 5 additions & 37 deletions
42
src/main/java/com/example/solidconnection/auth/service/TokenProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,15 @@ | ||
| package com.example.solidconnection.auth.service; | ||
|
|
||
| import com.example.solidconnection.auth.domain.TokenType; | ||
| import com.example.solidconnection.security.config.JwtProperties; | ||
| import io.jsonwebtoken.Claims; | ||
| import io.jsonwebtoken.Jwts; | ||
| import io.jsonwebtoken.SignatureAlgorithm; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
|
|
||
| import java.util.Date; | ||
| import java.util.concurrent.TimeUnit; | ||
| public interface TokenProvider { | ||
|
|
||
| import static com.example.solidconnection.util.JwtUtils.parseSubject; | ||
| String generateToken(String string, TokenType tokenType); | ||
|
|
||
| public abstract class TokenProvider { | ||
| String saveToken(String token, TokenType tokenType); | ||
|
|
||
| protected final JwtProperties jwtProperties; | ||
| protected final RedisTemplate<String, String> redisTemplate; | ||
| String parseSubject(String token); | ||
|
|
||
| public TokenProvider(JwtProperties jwtProperties, RedisTemplate<String, String> redisTemplate) { | ||
| this.jwtProperties = jwtProperties; | ||
| this.redisTemplate = redisTemplate; | ||
| } | ||
|
|
||
| protected final String generateToken(String string, TokenType tokenType) { | ||
| Claims claims = Jwts.claims().setSubject(string); | ||
| Date now = new Date(); | ||
| Date expiredDate = new Date(now.getTime() + tokenType.getExpireTime()); | ||
| return Jwts.builder() | ||
| .setClaims(claims) | ||
| .setIssuedAt(now) | ||
| .setExpiration(expiredDate) | ||
| .signWith(SignatureAlgorithm.HS512, jwtProperties.secret()) | ||
| .compact(); | ||
| } | ||
|
|
||
| protected final String saveToken(String token, TokenType tokenType) { | ||
| String subject = parseSubject(token, jwtProperties.secret()); | ||
| redisTemplate.opsForValue().set( | ||
| tokenType.addPrefix(subject), | ||
| token, | ||
| tokenType.getExpireTime(), | ||
| TimeUnit.MILLISECONDS | ||
| ); | ||
| return token; | ||
| } | ||
| Claims parseClaims(String token); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/example/solidconnection/auth/token/JwtTokenProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.example.solidconnection.auth.token; | ||
|
|
||
| import com.example.solidconnection.auth.domain.TokenType; | ||
| import com.example.solidconnection.auth.service.TokenProvider; | ||
| import com.example.solidconnection.auth.token.config.JwtProperties; | ||
| import com.example.solidconnection.common.exception.CustomException; | ||
| import io.jsonwebtoken.Claims; | ||
| import io.jsonwebtoken.Jwts; | ||
| import io.jsonwebtoken.SignatureAlgorithm; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.redis.core.RedisTemplate; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.Date; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.INVALID_TOKEN; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class JwtTokenProvider implements TokenProvider { | ||
|
|
||
| private final JwtProperties jwtProperties; | ||
| private final RedisTemplate<String, String> redisTemplate; | ||
|
|
||
| @Override | ||
| public final String generateToken(String string, TokenType tokenType) { | ||
| Claims claims = Jwts.claims().setSubject(string); | ||
| Date now = new Date(); | ||
| Date expiredDate = new Date(now.getTime() + tokenType.getExpireTime()); | ||
| return Jwts.builder() | ||
| .setClaims(claims) | ||
| .setIssuedAt(now) | ||
| .setExpiration(expiredDate) | ||
| .signWith(SignatureAlgorithm.HS512, jwtProperties.secret()) | ||
| .compact(); | ||
| } | ||
|
|
||
| @Override | ||
| public final String saveToken(String token, TokenType tokenType) { | ||
| String subject = parseSubject(token); | ||
| redisTemplate.opsForValue().set( | ||
| tokenType.addPrefix(subject), | ||
| token, | ||
| tokenType.getExpireTime(), | ||
| TimeUnit.MILLISECONDS | ||
| ); | ||
| return token; | ||
| } | ||
|
|
||
| @Override | ||
| public String parseSubject(String token) { | ||
| return parseClaims(token).getSubject(); | ||
| } | ||
|
|
||
| @Override | ||
| public Claims parseClaims(String token) { | ||
| try { | ||
| return Jwts.parser() | ||
| .setSigningKey(jwtProperties.secret()) | ||
| .parseClaimsJws(token) | ||
| .getBody(); | ||
| } catch (Exception e) { | ||
| throw new CustomException(INVALID_TOKEN); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.