-
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 5 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,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; | ||
} | ||
} |
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); | ||
} | ||
|
||
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,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> { | ||
|
||
} |
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