-
Notifications
You must be signed in to change notification settings - Fork 2
feat: 사용자 인증 구현 #149
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
feat: 사용자 인증 구현 #149
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b9c7a8f
feat: 사용자 인증 구현
Choi-JJunho 2c8f108
refactor: ArgumentResolver 이름 변경
Choi-JJunho f29b6d4
refactor: CustomException 적용
Choi-JJunho d183568
refactor: 예외상황 명시하도록 순서 변경
Choi-JJunho d37815b
refactor: 상수추출
Choi-JJunho c405f12
refactor: 미사용 생성자 제거
Choi-JJunho dc1d8d6
chore: 패키지 변경
Choi-JJunho b5bb2fb
refactor: 미사용 어노테이션 제거
Choi-JJunho 6fc7ced
Merge branch 'develop' of https://github.com/BCSDLab/KOIN_API_V2 into…
Choi-JJunho b3f501f
refactor: 오타수정
Choi-JJunho dae9bae
refactor: 오류메시지 구체화
Choi-JJunho be0b750
refactor: 미사용 메서드 제거
Choi-JJunho 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 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
14 changes: 14 additions & 0 deletions
14
src/main/java/in/koreatech/koin/domain/auth/StudentAuth.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,14 @@ | ||
| package in.koreatech.koin.domain.auth; | ||
|
|
||
| import static java.lang.annotation.ElementType.FIELD; | ||
| import static java.lang.annotation.ElementType.PARAMETER; | ||
| import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
|
||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target({PARAMETER, FIELD}) | ||
| @Retention(RUNTIME) | ||
| public @interface StudentAuth { | ||
|
|
||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/in/koreatech/koin/domain/auth/exception/AuthException.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,18 @@ | ||
| package in.koreatech.koin.domain.auth.exception; | ||
|
|
||
| public class AuthException extends RuntimeException { | ||
|
|
||
| private static final String DEFAULT_MESSAGE = "올바르지 않은 인증정보입니다."; | ||
|
|
||
| public AuthException() { | ||
| } | ||
|
|
||
| public AuthException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public static AuthException withDetail(String detail) { | ||
| String message = String.format("%s %s", DEFAULT_MESSAGE, detail); | ||
| return new AuthException(message); | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/main/java/in/koreatech/koin/domain/auth/resolver/StudentArgumentResolver.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,48 @@ | ||
| package in.koreatech.koin.domain.auth.resolver; | ||
|
|
||
| import in.koreatech.koin.domain.auth.JwtProvider; | ||
| import in.koreatech.koin.domain.auth.StudentAuth; | ||
| import in.koreatech.koin.domain.auth.exception.AuthException; | ||
| import in.koreatech.koin.domain.user.exception.UserNotFoundException; | ||
| import in.koreatech.koin.domain.user.repository.StudentRepository; | ||
| 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 StudentArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
|
||
| private static final String AUTHORIZATION = "Authorization"; | ||
|
|
||
| private final JwtProvider jwtProvider; | ||
| private final StudentRepository studentRepository; | ||
|
|
||
| @Override | ||
| public boolean supportsParameter(MethodParameter parameter) { | ||
| return parameter.hasParameterAnnotation(StudentAuth.class); | ||
| } | ||
|
|
||
| @Override | ||
| public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
| NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
|
|
||
| HttpServletRequest nativeRequest = webRequest.getNativeRequest(HttpServletRequest.class); | ||
| if (nativeRequest == null) { | ||
| throw new AuthException("요청 값이 비어있습니다."); | ||
| } | ||
|
|
||
| String authorizationHeader = nativeRequest.getHeader(AUTHORIZATION); | ||
| if (authorizationHeader == null) { | ||
| throw new AuthException("인증 헤더값이 비어있습니다. authorizationHeader: " + nativeRequest); | ||
| } | ||
| Long userId = jwtProvider.getUserId(authorizationHeader); | ||
| return studentRepository.findById(userId) | ||
| .orElseThrow(() -> UserNotFoundException.withDetail("authorizationHeader: " + authorizationHeader)); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/in/koreatech/koin/domain/user/controller/StudentController.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 in.koreatech.koin.domain.user.controller; | ||
|
|
||
| import in.koreatech.koin.domain.auth.StudentAuth; | ||
| import in.koreatech.koin.domain.user.model.Student; | ||
| import in.koreatech.koin.domain.user.dto.StudentResponse; | ||
| import in.koreatech.koin.domain.user.service.StudentService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class StudentController { | ||
|
|
||
| private final StudentService studentService; | ||
|
|
||
| @GetMapping("/user/student/me") | ||
| public ResponseEntity<StudentResponse> getStudent(@StudentAuth Student student) { | ||
| StudentResponse studentResponse = studentService.getStudent(student); | ||
| return ResponseEntity.ok().body(studentResponse); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/main/java/in/koreatech/koin/domain/user/dto/StudentResponse.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,33 @@ | ||
| package in.koreatech.koin.domain.user.dto; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import in.koreatech.koin.domain.user.model.Student; | ||
| import in.koreatech.koin.domain.user.model.User; | ||
|
|
||
| @JsonNaming(value = SnakeCaseStrategy.class) | ||
| public record StudentResponse( | ||
| String anonymousNickname, | ||
| String email, | ||
| String gender, | ||
| String major, | ||
| String name, | ||
| String nickname, | ||
| String phoneNumber, | ||
| String studentNumber | ||
| ) { | ||
|
|
||
| public static StudentResponse from(Student student) { | ||
| User user = student.getUser(); | ||
| return new StudentResponse( | ||
| student.getAnonymousNickname(), | ||
| user.getEmail(), | ||
| user.getGender().name(), | ||
| student.getDepartment(), | ||
| user.getName(), | ||
| user.getNickname(), | ||
| user.getPhoneNumber(), | ||
| student.getStudentNumber() | ||
| ); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/in/koreatech/koin/domain/user/exception/UserNotFoundException.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,15 @@ | ||
| package in.koreatech.koin.domain.user.exception; | ||
|
|
||
| public class UserNotFoundException extends RuntimeException { | ||
|
|
||
| private static final String DEFAULT_MESSAGE = "존재하지 않는 사용자입니다."; | ||
|
|
||
| public UserNotFoundException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public static UserNotFoundException withDetail(String detail) { | ||
| String message = String.format("%s %s", DEFAULT_MESSAGE, detail); | ||
| return new UserNotFoundException(message); | ||
| } | ||
| } |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/in/koreatech/koin/domain/user/repository/StudentRepository.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,12 @@ | ||
| package in.koreatech.koin.domain.user.repository; | ||
|
|
||
| import in.koreatech.koin.domain.user.model.Student; | ||
| import java.util.Optional; | ||
| import org.springframework.data.repository.Repository; | ||
|
|
||
| public interface StudentRepository extends Repository<Student, Long> { | ||
|
|
||
| Student save(Student student); | ||
|
|
||
| Optional<Student> findById(Long id); | ||
| } |
21 changes: 21 additions & 0 deletions
21
src/main/java/in/koreatech/koin/domain/user/service/StudentService.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,21 @@ | ||
| package in.koreatech.koin.domain.user.service; | ||
|
|
||
| import in.koreatech.koin.domain.user.exception.UserNotFoundException; | ||
| import in.koreatech.koin.domain.user.model.Student; | ||
| import in.koreatech.koin.domain.user.dto.StudentResponse; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class StudentService { | ||
|
|
||
| public StudentResponse getStudent(Student student) { | ||
| if (student == null || student.getId() == null) { | ||
| throw new UserNotFoundException("학생 정보가 비어있습니다."); | ||
| } | ||
| return StudentResponse.from(student); | ||
| } | ||
| } |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/in/koreatech/koin/global/config/WebConfig.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,20 @@ | ||
| package in.koreatech.koin.global.config; | ||
|
|
||
| import in.koreatech.koin.domain.auth.resolver.StudentArgumentResolver; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
|
||
| @Configuration | ||
| @RequiredArgsConstructor | ||
| public class WebConfig implements WebMvcConfigurer { | ||
|
|
||
| private final StudentArgumentResolver studentArgumentResolver; | ||
|
|
||
| @Override | ||
| public void addArgumentResolvers(final List<HandlerMethodArgumentResolver> resolvers) { | ||
| resolvers.add(studentArgumentResolver); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/in/koreatech/koin/global/exception/ErrorResponse.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 in.koreatech.koin.global.exception; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class ErrorResponse { | ||
|
|
||
| private final int code; | ||
|
Collaborator
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. A
작업 내용에 써있었네요 죄송합니다! |
||
| private final String message; | ||
|
|
||
| private ErrorResponse(int code, String message) { | ||
| this.code = code; | ||
| this.message = message; | ||
| } | ||
|
|
||
| public static ErrorResponse of(int code, String message) { | ||
| return new ErrorResponse(code, message); | ||
| } | ||
|
|
||
| public static ErrorResponse from(String message) { | ||
| return new ErrorResponse(0, message); | ||
| } | ||
| } | ||
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.
R
이번 PR에서의 dto, repository, service 계층 경로가 이상해요 😵💫
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.
패키지 변경작업 전에 stash해놨던게 여기저기 흩뿌려졌네요 ㅠㅠ
꼼꼼한리뷰 감사해요~ 반영하겠습니다!