-
Notifications
You must be signed in to change notification settings - Fork 0
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
액세스 토큰 만료시 Refresh 하는 기능 구현 #43
Merged
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6cd3748
chore(config): enable OpenFeign
jacobhboy 65ae96f
chore(config): OpenFeign 설정 변경
jacobhboy 00eb08a
chore(build): jwt 의존성 추가
jacobhboy 6018394
chore(yml): jwt 설정 추가
jacobhboy 660068e
feat(auth): 구글 auth feign 추가
jacobhboy 5aa78ae
feat(auth): 구글 auth로 로그인 구현
jacobhboy dfbdb8f
feat(auth): 토큰 발급
jacobhboy 20105d5
feat(user): 유저 추가 혹은 업데이트 구현
jacobhboy 7887bd0
feat(auth): jwt credential 추가
jacobhboy 121913e
feat(user): email 추가
jacobhboy a9fc11d
feat(auth): exception들 추가
jacobhboy 0b4c6e1
feat(user): Role 제거
jacobhboy 1dcb6b4
feat(auth): dto 추가
jacobhboy d880e66
chore(merge): 머지 하였음
jacobhboy aefb008
feat(user): Role 추가
jacobhboy e768859
feat(profile): getCurrentUser 추가
jacobhboy d2b50d2
feat(comment): getCurrentUser 추가
jacobhboy 57c1208
feat(like): getCurrentUser 추가
jacobhboy fb3b48c
feat(auth): 현재 사용자를 저장하는 authRepository 구현
jacobhboy 0b423f5
feat(auth): 인터셉터 구현
jacobhboy 7270f7c
feat(user): 아이디로 유저 조회하는 기능 추가
jacobhboy a854d44
feat(auth): extract가 token을 받도록 변경
jacobhboy d972cd5
feat(auth): access token을 refresh하는 기능 구현
jacobhboy f9dead8
fix(auth): merge conflict 해결
jacobhboy 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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sickgyun/server/auth/annotation/AdminOnly.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,12 @@ | ||
package com.sickgyun.server.auth.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@LoginRequired | ||
public @interface AdminOnly { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sickgyun/server/auth/annotation/LoginRequired.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,11 @@ | ||
package com.sickgyun.server.auth.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface LoginRequired { | ||
} |
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,7 @@ | ||
package com.sickgyun.server.auth.domain; | ||
|
||
public record Token( | ||
String accessToken, | ||
String refreshToken | ||
) { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sickgyun/server/auth/exception/NotBssmEmailException.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,11 @@ | ||
package com.sickgyun.server.auth.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.sickgyun.server.common.exception.SickgyunException; | ||
|
||
public class NotBssmEmailException extends SickgyunException { | ||
public NotBssmEmailException(String email) { | ||
super(HttpStatus.BAD_REQUEST, email + "은 Bssm 이메일이 아닙니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sickgyun/server/auth/exception/TokenExpiredException.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,11 @@ | ||
package com.sickgyun.server.auth.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.sickgyun.server.common.exception.SickgyunException; | ||
|
||
public class TokenExpiredException extends SickgyunException { | ||
public TokenExpiredException() { | ||
super(HttpStatus.FORBIDDEN, "토큰이 만료되었습니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sickgyun/server/auth/exception/TokenInvalidException.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,11 @@ | ||
package com.sickgyun.server.auth.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.sickgyun.server.common.exception.SickgyunException; | ||
|
||
public class TokenInvalidException extends SickgyunException { | ||
public TokenInvalidException() { | ||
super(HttpStatus.UNAUTHORIZED, "토큰이 유효하지 않습니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sickgyun/server/auth/exception/TokenMissingException.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,11 @@ | ||
package com.sickgyun.server.auth.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.sickgyun.server.common.exception.SickgyunException; | ||
|
||
public class TokenMissingException extends SickgyunException { | ||
public TokenMissingException() { | ||
super(HttpStatus.UNAUTHORIZED, "토큰이 없습니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sickgyun/server/auth/exception/UserIsNotAdminException.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,11 @@ | ||
package com.sickgyun.server.auth.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.sickgyun.server.common.exception.SickgyunException; | ||
|
||
public class UserIsNotAdminException extends SickgyunException { | ||
public UserIsNotAdminException() { | ||
super(HttpStatus.UNAUTHORIZED, "사용자가 어드민이 아닙니다."); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sickgyun/server/auth/exception/UserNotLoginException.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,11 @@ | ||
package com.sickgyun.server.auth.exception; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
import com.sickgyun.server.common.exception.SickgyunException; | ||
|
||
public class UserNotLoginException extends SickgyunException { | ||
public UserNotLoginException() { | ||
super(FORBIDDEN, "유저가 로그인하지 않았습니다."); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/sickgyun/server/auth/infra/feign/GoogleOauthFeign.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,13 @@ | ||
package com.sickgyun.server.auth.infra.feign; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import com.sickgyun.server.auth.infra.feign.dto.GoogleUserInfoResponse; | ||
|
||
@FeignClient(name = "GoogleOauthFeign", url = "https://www.googleapis.com/oauth2/v1/userinfo") | ||
public interface GoogleOauthFeign { | ||
@GetMapping | ||
GoogleUserInfoResponse getInfo(@RequestParam(name = "access_token") String accessToken); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sickgyun/server/auth/infra/feign/dto/GoogleUserInfoResponse.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,12 @@ | ||
package com.sickgyun.server.auth.infra.feign.dto; | ||
|
||
import com.sickgyun.server.user.domain.User; | ||
|
||
public record GoogleUserInfoResponse( | ||
String email, | ||
String name | ||
) { | ||
public User toUser() { | ||
return new User(name, email); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/sickgyun/server/auth/interceptor/AuthInterceptor.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,56 @@ | ||
package com.sickgyun.server.auth.interceptor; | ||
|
||
import static org.springframework.http.HttpHeaders.*; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
import com.sickgyun.server.auth.annotation.AdminOnly; | ||
import com.sickgyun.server.auth.annotation.LoginRequired; | ||
import com.sickgyun.server.auth.exception.UserIsNotAdminException; | ||
import com.sickgyun.server.auth.repository.AuthRepository; | ||
import com.sickgyun.server.auth.util.BearerTokenExtractor; | ||
import com.sickgyun.server.auth.util.JwtParser; | ||
import com.sickgyun.server.user.domain.User; | ||
import com.sickgyun.server.user.domain.repository.UserRepository; | ||
import com.sickgyun.server.user.domain.role.Role; | ||
import com.sickgyun.server.user.exception.UserNotFoundException; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class AuthInterceptor implements HandlerInterceptor { | ||
private final JwtParser jwtParser; | ||
private final AuthRepository authRepository; | ||
private final UserRepository userRepository; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
if (handler instanceof HandlerMethod hm) { | ||
if (hm.hasMethodAnnotation(LoginRequired.class)) { | ||
String jwt = BearerTokenExtractor.extract(request.getHeader(AUTHORIZATION)); | ||
Long userId = jwtParser.getIdFromJwt(jwt); | ||
|
||
User user = userRepository.findById(userId) | ||
.orElseThrow(() -> new UserNotFoundException(userId)); | ||
|
||
authRepository.updateCurrentUser(user); | ||
} | ||
if (hm.hasMethodAnnotation(AdminOnly.class)) { | ||
User currentUser = authRepository.getCurrentUser(); | ||
shouldUserAdmin(currentUser); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private static void shouldUserAdmin(User currentUser) { | ||
if (currentUser.getRole() != Role.ADMIN) { | ||
throw new UserIsNotAdminException(); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/sickgyun/server/auth/presentation/AuthController.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,34 @@ | ||
package com.sickgyun.server.auth.presentation; | ||
|
||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.sickgyun.server.auth.presentation.dto.AccessTokenRequest; | ||
import com.sickgyun.server.auth.presentation.dto.RefreshTokenRequest; | ||
import com.sickgyun.server.auth.presentation.dto.TokenResponse; | ||
import com.sickgyun.server.auth.service.CommandAuthService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/auth") | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
private final CommandAuthService authService; | ||
|
||
@PostMapping("/login") | ||
public TokenResponse login(@RequestBody AccessTokenRequest accessToken) { | ||
return TokenResponse.from( | ||
authService.login(accessToken.accessToken()) | ||
); | ||
} | ||
|
||
@PostMapping("/refresh") | ||
public TokenResponse refreshAccessToken(@RequestBody RefreshTokenRequest refreshToken) { | ||
return TokenResponse.from( | ||
authService.refresh(refreshToken.refreshToken()) | ||
); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/sickgyun/server/auth/presentation/dto/AccessTokenRequest.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,6 @@ | ||
package com.sickgyun.server.auth.presentation.dto; | ||
|
||
public record AccessTokenRequest( | ||
String accessToken | ||
) { | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/sickgyun/server/auth/presentation/dto/RefreshTokenRequest.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,6 @@ | ||
package com.sickgyun.server.auth.presentation.dto; | ||
|
||
public record RefreshTokenRequest( | ||
String refreshToken | ||
) { | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/sickgyun/server/auth/presentation/dto/TokenResponse.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,15 @@ | ||
package com.sickgyun.server.auth.presentation.dto; | ||
|
||
import com.sickgyun.server.auth.domain.Token; | ||
|
||
public record TokenResponse( | ||
String accessToken, | ||
String refreshToken | ||
) { | ||
public static TokenResponse from(Token token) { | ||
return new TokenResponse( | ||
token.accessToken(), | ||
token.refreshToken() | ||
); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/sickgyun/server/auth/repository/AuthRepository.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,22 @@ | ||
package com.sickgyun.server.auth.repository; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import com.sickgyun.server.auth.exception.UserNotLoginException; | ||
import com.sickgyun.server.user.domain.User; | ||
|
||
@Repository | ||
public class AuthRepository { | ||
private User currentUser; | ||
|
||
public User getCurrentUser() { | ||
if (currentUser == null) { | ||
throw new UserNotLoginException(); | ||
} | ||
return currentUser; | ||
} | ||
|
||
public void updateCurrentUser(User currentUser) { | ||
this.currentUser = currentUser; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/sickgyun/server/auth/service/CommandAuthService.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,54 @@ | ||
package com.sickgyun.server.auth.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.sickgyun.server.auth.domain.Token; | ||
import com.sickgyun.server.auth.service.implementation.AuthReader; | ||
import com.sickgyun.server.auth.service.implementation.AuthValidator; | ||
import com.sickgyun.server.auth.service.implementation.TokenProvider; | ||
import com.sickgyun.server.auth.util.BearerTokenExtractor; | ||
import com.sickgyun.server.user.domain.User; | ||
import com.sickgyun.server.user.service.implementation.UserCreator; | ||
import com.sickgyun.server.user.service.implementation.UserReader; | ||
import com.sickgyun.server.user.service.implementation.UserUpdater; | ||
import com.sickgyun.server.user.service.implementation.UserValidator; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class CommandAuthService { | ||
private final TokenProvider tokenProvider; | ||
private final AuthReader authReader; | ||
private final AuthValidator authValidator; | ||
private final UserValidator userValidator; | ||
private final UserUpdater userUpdater; | ||
private final UserCreator userCreator; | ||
private final UserReader userReader; | ||
|
||
public Token login(String accessToken) { | ||
User user = authReader.getGoogleUser(accessToken); | ||
authValidator.shouldBeBssmEmail(user); | ||
|
||
User updatedUser; | ||
if (userValidator.checkUserExist(user)) { | ||
updatedUser = userUpdater.updateUser(user); | ||
} else { | ||
updatedUser = userCreator.create(user); | ||
} | ||
|
||
return tokenProvider.createNewTokens(updatedUser); | ||
} | ||
|
||
public Token refresh(String bearer) { | ||
String refreshToken = BearerTokenExtractor.extract(bearer); | ||
authValidator.shouldRefreshTokenValid(refreshToken); | ||
|
||
Long userId = authReader.getIdFromJwt(refreshToken); | ||
String accessToken = tokenProvider.createAccessToken(userReader.readUser(userId)); | ||
|
||
return new Token(accessToken, refreshToken); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/sickgyun/server/auth/service/implementation/AuthReader.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,24 @@ | ||
package com.sickgyun.server.auth.service.implementation; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.sickgyun.server.auth.infra.feign.GoogleOauthFeign; | ||
import com.sickgyun.server.auth.util.JwtParser; | ||
import com.sickgyun.server.user.domain.User; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthReader { | ||
private final GoogleOauthFeign googleFeign; | ||
private final JwtParser jwtParser; | ||
|
||
public User getGoogleUser(String accessToken) { | ||
return googleFeign.getInfo(accessToken).toUser(); | ||
} | ||
|
||
public Long getIdFromJwt(String token) { | ||
return jwtParser.getIdFromJwt(token); | ||
} | ||
} |
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.
깔쌈하네요