-
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
Changes from 4 commits
4cfea05
43b16db
81410ce
0e89f7b
9ac0c40
3710c6a
d7e309a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package team7.inplace.global.exception.code; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
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 null; | ||
} | ||
|
||
@Override | ||
public String code() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return ""; | ||
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. @Getter 로 사용하지 말고 인터페이스에 있는 값 사용 부탁드립니다! 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. 넵! |
||
} | ||
} |
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); | ||
} | ||
} |
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; | ||
} | ||
} |
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); | ||
} | ||
} |
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); | ||
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. 살짝 읽기 불편한데 orElseThrow 앞에서 줄 바꾸면 안될까요? 아니면 람다식 부분에서 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. 인정합니다. |
||
} | ||
|
||
public void saveRefreshToken(String username, String token) { | ||
RefreshToken refreshToken = new RefreshToken(username, token); | ||
refreshTokenRepository.save(refreshToken); | ||
} | ||
|
||
} |
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); | ||
} | ||
} | ||
} |
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package team7.inplace.token.persistence; | ||
|
||
import java.util.Optional; | ||
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> { | ||
|
||
Optional<RefreshToken> findById(String id); | ||
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. 인정합니다! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package team7.inplace.token.presentation; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.CookieValue; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import team7.inplace.global.exception.InplaceException; | ||
import team7.inplace.global.exception.code.AuthorizationErrorCode; | ||
import team7.inplace.security.util.AuthorizationUtil; | ||
import team7.inplace.security.util.JwtUtil; | ||
import team7.inplace.token.application.RefreshTokenFacade; | ||
import team7.inplace.token.application.dto.TokenCommand.ReIssued; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class RefreshTokenController { | ||
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. 했습니다! |
||
|
||
private final JwtUtil jwtUtil; | ||
private final RefreshTokenFacade refreshTokenFacade; | ||
|
||
@GetMapping("/refresh-token") | ||
public ResponseEntity<Void> refreshToken(@CookieValue(value = "refresh_token") Cookie cookie, | ||
HttpServletResponse response) { | ||
if (cannotRefreshToken(cookie)) { | ||
throw InplaceException.of(AuthorizationErrorCode.INVALID_TOKEN); | ||
} | ||
|
||
String refreshToken = cookie.getValue(); | ||
ReIssued reIssuedToken = refreshTokenFacade.getReIssuedRefreshTokenCookie( | ||
jwtUtil.getUsername(refreshToken), refreshToken); | ||
addTokenToCookie(response, reIssuedToken); | ||
|
||
return ResponseEntity.ok().build(); | ||
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. 저희 new ResponseEntity() 형식으로 하기로 한 거 아니었나요? 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. 오케이입니다. |
||
} | ||
|
||
private void addTokenToCookie(HttpServletResponse response, ReIssued reIssuedToken) { | ||
Cookie accessTokenCookie = createCookie("access_token", reIssuedToken.accessToken()); | ||
Cookie refreshTokenCookie = createCookie("refresh_token", reIssuedToken.refreshToken()); | ||
response.addCookie(accessTokenCookie); | ||
response.addCookie(refreshTokenCookie); | ||
} | ||
|
||
private boolean cannotRefreshToken(Cookie cookie) { | ||
return AuthorizationUtil.getUserId() == null || !jwtUtil.isRefreshToken(cookie.getValue()); | ||
} | ||
|
||
private Cookie createCookie(String key, String value) { | ||
Cookie cookie = new Cookie(key, value); | ||
cookie.setMaxAge(60 * 60); | ||
cookie.setPath("/"); | ||
cookie.setHttpOnly(true); | ||
return cookie; | ||
} | ||
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. Util 클래스를 만들어야 할까요... Controller에서 처리할 일은 아닌가 싶기도 하구요 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. util만들었습니다! |
||
} |
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