-
Notifications
You must be signed in to change notification settings - Fork 5
[FEATURE] 인증 인가 전반적인 구현을 완료합니다 #68
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
28 commits
Select commit
Hold shift + click to select a range
3d44031
refactor : Move file's directory
isyoudwn 522328f
refactor : Refactor JwtConfig
isyoudwn 110f022
refactor : Rename JwtResponse
isyoudwn 2a5a279
refactor : Refactor JwtProvider
isyoudwn 62b57de
refactor : Move JwtConfig's directory path
isyoudwn c902cd7
feat : Add refresh token model
isyoudwn fbee8cd
feat : Add refresh token repsitory
isyoudwn ea7968e
feat : Add Role enum
isyoudwn a810d0a
refactor : Delete userRole class
isyoudwn dc488dd
refactor : Add Accessor domain
isyoudwn d90a93f
refactor : Add Auth annotation
isyoudwn a9ee79f
refactor : Add AuthException class
isyoudwn b92dda3
refactor : Add TokenExtractor
isyoudwn cc4468e
refactor : Refactor TokenExtractor
isyoudwn dcc0fe0
feat : Add RefreshTokenExtractor
isyoudwn 5ae66f2
feat : Add AuthenticationResolver
isyoudwn 1197c69
feat : Add find user by email method
isyoudwn 09874d9
feat : Add get user detail method
isyoudwn 72eaf1f
feat : Add login method
isyoudwn 2558555
feat : Add login controller
isyoudwn f13cc9e
refactor : Refactor auth domain
isyoudwn b6ee0b3
refactor : Refactor auth service layer
isyoudwn 9d1bfaa
refactor : Refactor refresh token from cookie
isyoudwn 6bb43a3
feat : Add reissueResponseTokens api
isyoudwn 3eafa46
feat : Add Permission check AOP
isyoudwn 7fdb53b
refactor : Delete test directory
isyoudwn ed6649b
Merge branch 'develop' into isyoudwn/auth
polyglot-k 70d44f3
fix(user) : 오타 수정
polyglot-k 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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/tasksprints/auction/api/auth/AuthController.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,44 @@ | ||
| package com.tasksprints.auction.api.auth; | ||
|
|
||
| import static org.springframework.http.HttpHeaders.SET_COOKIE; | ||
|
|
||
| import com.tasksprints.auction.common.constant.ApiResponseMessages; | ||
| import com.tasksprints.auction.common.response.ApiResult; | ||
| import com.tasksprints.auction.domain.auth.dto.request.LoginRequest; | ||
| import com.tasksprints.auction.domain.auth.dto.response.AccessToken; | ||
| import com.tasksprints.auction.domain.auth.dto.response.ResponseTokens; | ||
| import com.tasksprints.auction.domain.auth.service.AuthService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.CookieValue; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
|
|
||
| @Controller | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/api/v1/auth") | ||
| public class AuthController { | ||
| private final AuthService authService; | ||
|
|
||
| @PostMapping("/login") | ||
| public ResponseEntity<ApiResult<AccessToken>> login(@RequestBody LoginRequest.Login login) { | ||
| Long userId = authService.validateLogin(login.email(), login.password()); | ||
| ResponseTokens responseTokens = authService.issueResponseTokens(userId); | ||
|
|
||
| return ResponseEntity.ok() | ||
| .header(SET_COOKIE, responseTokens.refreshToken().toString()) | ||
| .body(ApiResult.success(ApiResponseMessages.LOGIN_SUCCESS, responseTokens.accessToken())); | ||
| } | ||
|
|
||
| @GetMapping("/reissue") | ||
| public ResponseEntity<ApiResult<AccessToken>> reissueTokens(@CookieValue("refresh-token") String refreshToken) { | ||
| ResponseTokens responseTokens = authService.reissueResponseTokens(refreshToken); | ||
|
|
||
| return ResponseEntity.ok() | ||
| .header(SET_COOKIE, responseTokens.refreshToken().toString()) | ||
| .body(ApiResult.success(ApiResponseMessages.LOGIN_SUCCESS, responseTokens.accessToken())); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/tasksprints/auction/common/config/JwtConfig.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,24 @@ | ||
| package com.tasksprints.auction.common.config; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public class JwtConfig { | ||
|
|
||
| @Value("${jwt.expire-ms}") | ||
| private final Long accessExpireMs; | ||
|
|
||
| @Value("${jwt.expire-ms}") | ||
| private final Long refreshExpireMs; | ||
|
|
||
| @Value("${jwt.issuer}") | ||
| private final String issuer; | ||
|
|
||
| @Value("${jwt.secret}") | ||
| private final String secretKey; | ||
| } | ||
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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/tasksprints/auction/common/jwt/Auth.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,11 @@ | ||
| package com.tasksprints.auction.common.jwt; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target(ElementType.PARAMETER) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface Auth { | ||
| } |
27 changes: 0 additions & 27 deletions
27
src/main/java/com/tasksprints/auction/common/jwt/JwtProperties.java
This file was deleted.
Oops, something went wrong.
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/tasksprints/auction/common/jwt/UserCheck.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,25 @@ | ||
| package com.tasksprints.auction.common.jwt; | ||
|
|
||
| import com.tasksprints.auction.domain.auth.exception.AuthException; | ||
| import com.tasksprints.auction.domain.auth.model.Accessor; | ||
| import org.aspectj.lang.JoinPoint; | ||
| import org.aspectj.lang.annotation.Aspect; | ||
| import org.aspectj.lang.annotation.Before; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Aspect | ||
| @Component | ||
| public class UserCheck { | ||
| @Before("@annotation(UserOnly)") | ||
| public void userCheck(JoinPoint joinPoint) { | ||
| Object[] args = joinPoint.getArgs(); | ||
|
|
||
| for (Object arg : args) { | ||
| if (arg instanceof Accessor accessor) { | ||
| if (!accessor.isUser()) { | ||
| throw new AuthException("Invalid Access"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/tasksprints/auction/common/jwt/UserOnly.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,11 @@ | ||
| package com.tasksprints.auction.common.jwt; | ||
|
|
||
| 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) | ||
| public @interface UserOnly { | ||
| } |
19 changes: 0 additions & 19 deletions
19
src/main/java/com/tasksprints/auction/common/jwt/dto/response/JwtResponse.java
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
src/main/java/com/tasksprints/auction/common/resolver/AuthenticationResolver.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,54 @@ | ||
| package com.tasksprints.auction.common.resolver; | ||
|
|
||
| import com.tasksprints.auction.common.jwt.Auth; | ||
| import com.tasksprints.auction.common.jwt.JwtProvider; | ||
| import com.tasksprints.auction.domain.auth.TokenExtractor; | ||
| import com.tasksprints.auction.domain.auth.exception.RefreshTokenException; | ||
| import com.tasksprints.auction.domain.auth.model.Accessor; | ||
| import com.tasksprints.auction.domain.auth.service.RefreshTokenCookieManager; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.core.MethodParameter; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.bind.support.WebDataBinderFactory; | ||
| import org.springframework.web.context.request.NativeWebRequest; | ||
| import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
| import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class AuthenticationResolver implements HandlerMethodArgumentResolver { | ||
| private final JwtProvider jwtProvider; | ||
| private final RefreshTokenCookieManager refreshTokenCookieManager; | ||
| private final TokenExtractor accessTokenExtractor; | ||
|
|
||
| @Override | ||
| public boolean supportsParameter(MethodParameter parameter) { | ||
| return parameter | ||
| .hasParameterAnnotation(Auth.class); | ||
| } | ||
|
|
||
| @Override | ||
| public Object resolveArgument(MethodParameter parameter, | ||
| ModelAndViewContainer mavContainer, | ||
| NativeWebRequest webRequest, | ||
| WebDataBinderFactory binderFactory) { | ||
| HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); | ||
|
|
||
| if (request == null) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| try { | ||
| String refreshToken = refreshTokenCookieManager.extractRefreshToken(request); | ||
| String accessToken = accessTokenExtractor.extractToken(request); | ||
|
|
||
| jwtProvider.validateToken(accessToken); | ||
| jwtProvider.validateToken(refreshToken); | ||
|
|
||
| Long userId = Long.valueOf(jwtProvider.getSubject(refreshToken)); | ||
| return Accessor.user(userId); | ||
| } catch (RefreshTokenException e) { | ||
| return Accessor.guest(); | ||
| } | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/tasksprints/auction/domain/auth/AccessTokenExtractor.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,23 @@ | ||
| package com.tasksprints.auction.domain.auth; | ||
|
|
||
| import static com.tasksprints.auction.common.constant.ApiResponseMessages.ACCESS_TOKEN_NOT_FOUND; | ||
|
|
||
| import com.tasksprints.auction.domain.auth.exception.AccessTokenException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @Qualifier("accessTokenExtractor") | ||
| public class AccessTokenExtractor implements TokenExtractor { | ||
| private static final String TYPE = "Bearer "; | ||
| private static final String HEADER = "Authorization"; | ||
|
|
||
| public String extractToken(HttpServletRequest request) { | ||
| String header = request.getHeader(HEADER); | ||
| if(header != null && header.startsWith(TYPE)) { | ||
| return header.substring(TYPE.length()); | ||
| } | ||
| throw new AccessTokenException(ACCESS_TOKEN_NOT_FOUND); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.