-
Notifications
You must be signed in to change notification settings - Fork 2
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
[Feat] #44 TokenRefresh하기~ #48
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4cfea05
[feat] tokenRefresh를 적용하였습니다.
suhyeon7497 43b16db
Merge branch 'weekly/6' of https://github.com/kakao-tech-campus-2nd-s…
suhyeon7497 81410ce
[feat] cors setting
suhyeon7497 0e89f7b
[chore] cookie key 이름 변경
suhyeon7497 9ac0c40
[refactor] 수정 사항 반영
suhyeon7497 3710c6a
[refactor] ErrorCode는 getter를 사용하지 않는다.
suhyeon7497 d7e309a
[feat] UserErrorCode
suhyeon7497 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 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 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
28 changes: 28 additions & 0 deletions
28
src/main/java/team7/inplace/global/exception/code/UserErroCode.java
This file contains 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,28 @@ | ||
package team7.inplace.global.exception.code; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@AllArgsConstructor | ||
public enum UserErroCode implements ErrorCode { | ||
NOT_FOUND(HttpStatus.NOT_FOUND, "U001", "User is not found"); | ||
|
||
private final HttpStatus status; | ||
private final String code; | ||
private final String message; | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return status; | ||
} | ||
|
||
@Override | ||
public String code() { | ||
return code; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return message; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/team7/inplace/security/config/CorsConfig.java
This file contains 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,17 @@ | ||
package team7.inplace.security.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class CorsConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOriginPatterns("*") | ||
.allowedMethods("GET", "POST", "PUT", "DELETE") | ||
.allowCredentials(true); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/team7/inplace/security/config/RedisConfig.java
This file contains 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,26 @@ | ||
package team7.inplace.security.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Bean | ||
LettuceConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379)); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate() { | ||
RedisTemplate<?, ?> template = new RedisTemplate<>(); | ||
template.setConnectionFactory(redisConnectionFactory()); | ||
template.setKeySerializer(new StringRedisSerializer()); | ||
template.setValueSerializer(new StringRedisSerializer()); | ||
return template; | ||
} | ||
} |
This file contains 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 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 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 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 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,14 @@ | ||
package team7.inplace.security.util; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
|
||
public class CookieUtil { | ||
|
||
public Cookie createCookie(String key, String value) { | ||
Cookie cookie = new Cookie(key, value); | ||
cookie.setMaxAge(60 * 60); | ||
cookie.setPath("/"); | ||
cookie.setHttpOnly(true); | ||
return cookie; | ||
} | ||
} |
This file contains 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
38 changes: 38 additions & 0 deletions
38
src/main/java/team7/inplace/token/application/RefreshTokenFacade.java
This file contains 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,38 @@ | ||
package team7.inplace.token.application; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import team7.inplace.global.exception.InplaceException; | ||
import team7.inplace.global.exception.code.AuthorizationErrorCode; | ||
import team7.inplace.security.util.JwtUtil; | ||
import team7.inplace.token.application.dto.TokenCommand; | ||
import team7.inplace.token.application.dto.TokenCommand.ReIssued; | ||
import team7.inplace.user.application.UserService; | ||
import team7.inplace.user.application.dto.UserCommand; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class RefreshTokenFacade { | ||
|
||
private final JwtUtil jwtUtil; | ||
private final RefreshTokenService refreshTokenService; | ||
private final UserService userService; | ||
|
||
@Transactional | ||
public ReIssued getReIssuedRefreshTokenCookie(String username, String refreshToken) | ||
throws InplaceException { | ||
if (refreshTokenService.isInvalidRefreshToken(refreshToken)) { | ||
throw InplaceException.of(AuthorizationErrorCode.INVALID_TOKEN); | ||
} | ||
|
||
UserCommand.Info userInfo = userService.getUserByUsername(username); | ||
String reIssuedRefreshToken = jwtUtil | ||
.createRefreshToken(userInfo.username(), userInfo.id(), userInfo.role().getRoles()); | ||
String reIssuedAccessToken = jwtUtil | ||
.createAccessToken(userInfo.username(), userInfo.id(), userInfo.role().getRoles()); | ||
refreshTokenService.saveRefreshToken(username, reIssuedRefreshToken); | ||
|
||
return TokenCommand.ReIssued.of(reIssuedRefreshToken, reIssuedAccessToken); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/team7/inplace/token/application/RefreshTokenService.java
This file contains 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,30 @@ | ||
package team7.inplace.token.application; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import team7.inplace.global.exception.InplaceException; | ||
import team7.inplace.global.exception.code.AuthorizationErrorCode; | ||
import team7.inplace.security.util.JwtUtil; | ||
import team7.inplace.token.domain.RefreshToken; | ||
import team7.inplace.token.persistence.RefreshTokenRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RefreshTokenService { | ||
|
||
private final RefreshTokenRepository refreshTokenRepository; | ||
private final JwtUtil jwtUtil; | ||
|
||
public boolean isInvalidRefreshToken(String refreshToken) throws InplaceException { | ||
String username = jwtUtil.getUsername(refreshToken); | ||
return !refreshTokenRepository.findById(username) | ||
.orElseThrow(() -> InplaceException.of(AuthorizationErrorCode.INVALID_TOKEN)) | ||
.getRefreshToken().equals(refreshToken); | ||
} | ||
|
||
public void saveRefreshToken(String username, String token) { | ||
RefreshToken refreshToken = new RefreshToken(username, token); | ||
refreshTokenRepository.save(refreshToken); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/team7/inplace/token/application/dto/TokenCommand.java
This file contains 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,14 @@ | ||
package team7.inplace.token.application.dto; | ||
|
||
public class TokenCommand { | ||
|
||
public record ReIssued( | ||
String accessToken, | ||
String refreshToken | ||
) { | ||
|
||
public static ReIssued of(String reIssuedRefreshToken, String reIssuedAccessToken) { | ||
return new ReIssued(reIssuedRefreshToken, reIssuedAccessToken); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/team7/inplace/token/domain/RefreshToken.java
This file contains 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,19 @@ | ||
package team7.inplace.token.domain; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분 필요한가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 필요합니다...... |
||
@RedisHash(value = "refreshToken", timeToLive = 60 * 60 * 1000L) | ||
public class RefreshToken { | ||
|
||
@Id | ||
private String username; | ||
private String refreshToken; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/team7/inplace/token/persistence/RefreshTokenRepository.java
This file contains 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,10 @@ | ||
package team7.inplace.token.persistence; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
import team7.inplace.token.domain.RefreshToken; | ||
|
||
@Repository | ||
public interface RefreshTokenRepository extends CrudRepository<RefreshToken, String> { | ||
|
||
} |
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.
LGTM