-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: 스프링 시큐리티 코드 리팩터링 #154
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
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
210ae52
refactor: 토큰 기능 제공 클래스 이름 변경
nayonsoso ee49cfe
refactor: 토큰 접두사 추가 함수 이름 변경, static import 적용
nayonsoso 7227179
feat: subject 추출 함수 추가
nayonsoso e8632fc
test: TokenProvider 테스트 작성
nayonsoso feb828f
refactor: 예외 응답 함수 추출
nayonsoso b795eb8
feat: 로그아웃 체크 필터 생성
nayonsoso 54141e1
test: 로그아웃 체크 필터 테스트 작성
nayonsoso e89dc76
refactor: 중복 코드 함수로 추출
nayonsoso ff902d2
refactor: JWT 인증 필터 수정
nayonsoso d0e0929
refactor: 사용하지 않는 코드,클래스 제거
nayonsoso 53299c9
test: JWT 인증 필터 테스트 작성
nayonsoso c33466b
refactor: 스프링 시큐리티 설정 클래스 수정
nayonsoso aaca84f
refactor: 중복 선언된 cors 설정 제거
nayonsoso 1d37ff4
refactor: cors 관련 설정 ConfigurationProperties로 변경
nayonsoso b607988
refactor: TokenType 패키지 이동
nayonsoso 995ab18
refactor: TokenProvider, TokenValidator 패키지 이동
nayonsoso 505b746
refactor: JwtProperties 분리
nayonsoso a4b0db4
refactor: JwtUtils 분리
nayonsoso 2019370
refactor: ConfigurationPropertiesScan 적용
nayonsoso d9c965b
refactor: 인스턴스화 방지
nayonsoso 605f24a
test: 테스트 메서드 이름에 컨벤션 적용
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
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
51 changes: 51 additions & 0 deletions
51
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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.example.solidconnection.auth.service; | ||
|
|
||
| import com.example.solidconnection.auth.domain.TokenType; | ||
| import com.example.solidconnection.config.security.JwtProperties; | ||
| 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.util.JwtUtils.parseSubject; | ||
| import static com.example.solidconnection.util.JwtUtils.parseSubjectOrElseThrow; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class TokenProvider { | ||
|
|
||
| private final RedisTemplate<String, String> redisTemplate; | ||
| private final JwtProperties jwtProperties; | ||
|
|
||
| public String generateToken(String email, TokenType tokenType) { | ||
| Claims claims = Jwts.claims().setSubject(email); | ||
| 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(); | ||
| } | ||
|
|
||
| public String saveToken(String token, TokenType tokenType) { | ||
| String subject = parseSubjectOrElseThrow(token, jwtProperties.secret()); | ||
| redisTemplate.opsForValue().set( | ||
| tokenType.addPrefixToSubject(subject), | ||
| token, | ||
| tokenType.getExpireTime(), | ||
| TimeUnit.MILLISECONDS | ||
| ); | ||
| return token; | ||
| } | ||
|
|
||
| public String getEmail(String token) { | ||
| return parseSubject(token, jwtProperties.secret()); | ||
| } | ||
| } | ||
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
17 changes: 0 additions & 17 deletions
17
src/main/java/com/example/solidconnection/config/cors/CorsPropertiesConfig.java
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
src/main/java/com/example/solidconnection/config/cors/WebConfig.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/solidconnection/config/security/CorsProperties.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,9 @@ | ||
| package com.example.solidconnection.config.security; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @ConfigurationProperties(prefix = "cors") | ||
| public record CorsProperties(List<String> allowedOrigins) { | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TokenProvider는 Auth의 책임이니 적절하게 이동한 거 같습니다! 다만 앞으로 애플 등의 인증 방식이 추가될 수 있으므로, 확장성을 위해 인터페이스 도입을 고려하면 좋을 것 같습니다. 현재는 구조를 유지하면서, 새로운 인증 방식 추가 시점에 인터페이스화를 논의해도 좋을 거 같습니다!