-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] 로그인 ArgumentResolver 추가 (#97)
* refactor: 패키지 구조 변경 (#96) * refactor: 어드민 question API 클래스 분리 * feat: 로그인 ArgumentResolver 추가 * refactor: 어드민 question API 분리 원상태로 변경 * test: 워크플로우 실행 환경 확인을 위한 설정 추가 * chore: 테스트 컨테이너 reuse 옵션 추가 * chore: ci 테스트 h2 DB 설정
- Loading branch information
1 parent
7a88c25
commit d0b9e22
Showing
98 changed files
with
561 additions
and
385 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package donggi.dev.kkeuroolryo.auth; | ||
|
||
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 { | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/donggi/dev/kkeuroolryo/auth/LoginArgumentResolver.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,58 @@ | ||
package donggi.dev.kkeuroolryo.auth; | ||
|
||
import donggi.dev.kkeuroolryo.auth.application.AuthExtractor; | ||
import donggi.dev.kkeuroolryo.auth.application.dto.AuthPayload; | ||
import donggi.dev.kkeuroolryo.common.exception.BadRequestException; | ||
import donggi.dev.kkeuroolryo.common.exception.ErrorCodeAndMessage; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.HttpHeaders; | ||
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 | ||
public class LoginArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
private static final String BEARER_TYPE = "Bearer "; | ||
|
||
private final AuthExtractor authExtractor; | ||
|
||
public LoginArgumentResolver(final AuthExtractor authExtractor) { | ||
this.authExtractor = authExtractor; | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(final MethodParameter parameter) { | ||
return parameter.getParameterType().equals(Long.class) && | ||
parameter.hasParameterAnnotation(Auth.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument( | ||
final MethodParameter parameter, | ||
final ModelAndViewContainer mavContainer, | ||
final NativeWebRequest webRequest, | ||
final WebDataBinderFactory binderFactory | ||
) { | ||
final HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); | ||
|
||
if (request == null) { | ||
throw new BadRequestException(ErrorCodeAndMessage.INVALID_REQUEST); | ||
} | ||
|
||
final String accessToken = extractAccessToken(request.getHeader(HttpHeaders.AUTHORIZATION)); | ||
final AuthPayload authPayload = authExtractor.extract(accessToken); | ||
return authPayload.id(); | ||
} | ||
|
||
private String extractAccessToken(final String header) { | ||
if (header != null && header.startsWith(BEARER_TYPE)) { | ||
return header.substring(BEARER_TYPE.length()).trim(); | ||
} | ||
|
||
throw new BadRequestException(ErrorCodeAndMessage.INVALID_AUTH_TOKEN); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/donggi/dev/kkeuroolryo/auth/application/AuthExtractor.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,8 @@ | ||
package donggi.dev.kkeuroolryo.auth.application; | ||
|
||
import donggi.dev.kkeuroolryo.auth.application.dto.AuthPayload; | ||
|
||
public interface AuthExtractor { | ||
|
||
AuthPayload extract(String token); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/donggi/dev/kkeuroolryo/auth/application/dto/AuthPayload.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,8 @@ | ||
package donggi.dev.kkeuroolryo.auth.application.dto; | ||
|
||
public record AuthPayload( | ||
// String role, | ||
Long id | ||
) { | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/donggi/dev/kkeuroolryo/auth/config/AuthConfig.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 donggi.dev.kkeuroolryo.auth.config; | ||
|
||
import donggi.dev.kkeuroolryo.auth.application.AuthExtractor; | ||
import donggi.dev.kkeuroolryo.auth.infrastructure.JwtAuthExtractor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AuthConfig { | ||
|
||
private final String secretKey; | ||
|
||
public AuthConfig(@Value("${security.jwt.secret-key}") final String secretKey) { | ||
this.secretKey = secretKey; | ||
} | ||
|
||
@Bean | ||
public AuthExtractor authExtractor() { | ||
return new JwtAuthExtractor(secretKey); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/donggi/dev/kkeuroolryo/auth/infrastructure/JwtAuthExtractor.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,43 @@ | ||
package donggi.dev.kkeuroolryo.auth.infrastructure; | ||
|
||
import donggi.dev.kkeuroolryo.auth.application.AuthExtractor; | ||
import donggi.dev.kkeuroolryo.auth.application.dto.AuthPayload; | ||
import donggi.dev.kkeuroolryo.common.exception.ErrorCodeAndMessage; | ||
import donggi.dev.kkeuroolryo.common.exception.UnauthorizedException; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import io.jsonwebtoken.JwtException; | ||
import io.jsonwebtoken.JwtParser; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.security.Keys; | ||
import java.nio.charset.StandardCharsets; | ||
import javax.crypto.SecretKey; | ||
|
||
public class JwtAuthExtractor implements AuthExtractor { | ||
|
||
private final JwtParser parser; | ||
|
||
public JwtAuthExtractor(final String secretKey) { | ||
SecretKey key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8)); | ||
this.parser = Jwts.parserBuilder() | ||
.setSigningKey(key) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public AuthPayload extract(final String token) { | ||
final Claims claims = getClaims(token); | ||
return new AuthPayload(Long.parseLong(claims.getSubject())); | ||
} | ||
|
||
private Claims getClaims(final String token) { | ||
try { | ||
return parser.parseClaimsJws(token) | ||
.getBody(); | ||
} catch (ExpiredJwtException e) { | ||
throw new UnauthorizedException(ErrorCodeAndMessage.EXPIRED_TOKEN); | ||
} catch (JwtException e) { | ||
throw new UnauthorizedException(ErrorCodeAndMessage.INVALID_TOKEN); | ||
} | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...re/comment/application/CommentEditor.java → ...yo/comment/application/CommentEditor.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
6 changes: 3 additions & 3 deletions
6
...re/comment/application/CommentFinder.java → ...yo/comment/application/CommentFinder.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
24 changes: 12 additions & 12 deletions
24
...e/comment/application/CommentService.java → ...o/comment/application/CommentService.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
4 changes: 2 additions & 2 deletions
4
...e/comment/application/dto/CommentDto.java → ...o/comment/application/dto/CommentDto.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
6 changes: 3 additions & 3 deletions
6
...application/dto/CommentPaginationDto.java → ...application/dto/CommentPaginationDto.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
6 changes: 3 additions & 3 deletions
6
...uroolryo/core/comment/domain/Comment.java → ...v/kkeuroolryo/comment/domain/Comment.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
4 changes: 2 additions & 2 deletions
4
...o/core/comment/domain/CommentContent.java → ...oolryo/comment/domain/CommentContent.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
4 changes: 2 additions & 2 deletions
4
.../core/comment/domain/CommentPassword.java → ...olryo/comment/domain/CommentPassword.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
2 changes: 1 addition & 1 deletion
2
...ore/comment/domain/CommentRepository.java → ...ryo/comment/domain/CommentRepository.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
4 changes: 2 additions & 2 deletions
4
.../core/comment/domain/CommentUsername.java → ...olryo/comment/domain/CommentUsername.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
2 changes: 1 addition & 1 deletion
2
...ption/CommentInvalidContentException.java → ...ption/CommentInvalidContentException.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
2 changes: 1 addition & 1 deletion
2
...tion/CommentInvalidPasswordException.java → ...tion/CommentInvalidPasswordException.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
2 changes: 1 addition & 1 deletion
2
...tion/CommentInvalidUsernameException.java → ...tion/CommentInvalidUsernameException.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
2 changes: 1 addition & 1 deletion
2
...n/exception/CommentNotFoundException.java → ...n/exception/CommentNotFoundException.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
2 changes: 1 addition & 1 deletion
2
...ception/CommentUnauthorizedException.java → ...ception/CommentUnauthorizedException.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
2 changes: 1 addition & 1 deletion
2
...ception/NoOffsetPageInvalidException.java → ...ception/NoOffsetPageInvalidException.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
8 changes: 4 additions & 4 deletions
8
.../infrastructure/CommentJpaRepository.java → .../infrastructure/CommentJpaRepository.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
16 changes: 8 additions & 8 deletions
16
...yo/web/comment/CommentRestController.java → ...t/presentation/CommentRestController.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
2 changes: 1 addition & 1 deletion
2
...web/comment/dto/CommentDeleteCommand.java → ...resentation/dto/CommentDeleteCommand.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
4 changes: 2 additions & 2 deletions
4
...o/web/comment/dto/CommentRegisterDto.java → .../presentation/dto/CommentRegisterDto.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
4 changes: 2 additions & 2 deletions
4
.../web/comment/dto/NoOffsetPageCommand.java → ...presentation/dto/NoOffsetPageCommand.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
Oops, something went wrong.