-
Notifications
You must be signed in to change notification settings - Fork 3
Chore: Interceptor 계층 및 Role 추가 #17
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
Open
hjun-park
wants to merge
3
commits into
CHABAK-1.0.0
Choose a base branch
from
fix/member_3
base: CHABAK-1.0.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.sikhye.chabak.base.entity; | ||
|
|
||
| public enum BaseRole { | ||
| ROLE_USER, ROLE_ADMIN | ||
| } |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/sikhye/chabak/interceptor/JwtAdminInterceptor.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,51 @@ | ||
| package com.sikhye.chabak.interceptor; | ||
|
|
||
| import static com.sikhye.chabak.base.BaseResponseStatus.*; | ||
| import static com.sikhye.chabak.base.entity.BaseRole.*; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.servlet.HandlerInterceptor; | ||
|
|
||
| import com.sikhye.chabak.base.entity.BaseRole; | ||
| import com.sikhye.chabak.base.exception.BaseException; | ||
| import com.sikhye.chabak.utils.JwtTokenService; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class JwtAdminInterceptor implements HandlerInterceptor { | ||
|
|
||
| private final JwtTokenService jwtTokenService; | ||
|
|
||
| public JwtAdminInterceptor(JwtTokenService jwtTokenService) { | ||
| this.jwtTokenService = jwtTokenService; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws | ||
| Exception { | ||
|
|
||
| String requestURI = request.getRequestURI(); | ||
|
|
||
| // places에서 GET 방식이라면 Admin 기능이 아닌 Member도 사용 가능 | ||
| if (request.getMethod().equals("GET")) { | ||
| return true; | ||
| } | ||
|
|
||
| log.info("인증 체크 인터셉터 실행 {}", requestURI); | ||
|
|
||
| // 관리자 권한이 아닌 경우 api 요청 불가 | ||
| BaseRole memberRole = jwtTokenService.getMemberRole(); | ||
|
|
||
| if (!memberRole.equals(ROLE_ADMIN)) { | ||
| throw new BaseException(INVALID_USER_JWT); | ||
| } | ||
|
|
||
| return true; | ||
|
|
||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/sikhye/chabak/interceptor/JwtMemberInterceptor.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,38 @@ | ||
| package com.sikhye.chabak.interceptor; | ||
|
|
||
| import static com.sikhye.chabak.base.BaseResponseStatus.*; | ||
| import static com.sikhye.chabak.utils.JwtValue.*; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.servlet.HandlerInterceptor; | ||
|
|
||
| import com.sikhye.chabak.base.exception.BaseException; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class JwtMemberInterceptor implements HandlerInterceptor { | ||
|
|
||
| @Override | ||
| public boolean preHandle(HttpServletRequest request, HttpServletResponse response, | ||
| Object handler) throws Exception { | ||
|
|
||
| String requestURI = request.getRequestURI(); | ||
| log.info("토큰명 : {}", X_ACCESS_TOKEN.toString()); | ||
| String accessToken = request.getHeader(X_ACCESS_TOKEN.toString()); | ||
|
|
||
| log.info("인증 체크 인터셉터 실행 {}", requestURI); | ||
|
|
||
| if (accessToken == null || accessToken.isBlank()) { | ||
| log.info("미인증 JWT 요청"); | ||
| throw new BaseException(EMPTY_JWT); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| } |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/sikhye/chabak/interceptor/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,40 @@ | ||
| package com.sikhye.chabak.interceptor; | ||
|
|
||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
| import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
|
||
| @Configuration | ||
| public class WebConfig implements WebMvcConfigurer { | ||
|
|
||
| private final JwtMemberInterceptor jwtMemberInterceptor; | ||
| private final JwtAdminInterceptor jwtAdminInterceptor; | ||
|
|
||
| public WebConfig(JwtMemberInterceptor jwtMemberInterceptor, JwtAdminInterceptor jwtAdminInterceptor) { | ||
| this.jwtMemberInterceptor = jwtMemberInterceptor; | ||
| this.jwtAdminInterceptor = jwtAdminInterceptor; | ||
| } | ||
|
|
||
| // 인터셉터 등록 | ||
| @Override | ||
| public void addInterceptors(InterceptorRegistry registry) { | ||
| registry.addInterceptor(jwtMemberInterceptor) // 인터셉터 등록 | ||
| .order(1) // 인터셉터 호출 우선순위 | ||
| .addPathPatterns("/**") // 인터셉터 적용할 URI 패턴 | ||
| .excludePathPatterns("/error", "/members/**", "/auth/**"); // 인터셉터에서 제외할 URI 패턴 | ||
|
|
||
| registry.addInterceptor(jwtAdminInterceptor) | ||
| .order(2) | ||
| .addPathPatterns("/places/**") | ||
| .excludePathPatterns("/places/comments/**"); | ||
| } | ||
|
|
||
| @Override | ||
| public void addCorsMappings(CorsRegistry registry) { | ||
| registry.addMapping("/**") | ||
| .allowedOrigins("http://localhost:3000") | ||
| .allowedMethods("GET", "POST", "OPTIONS", "PUT", "PATCH"); | ||
| } | ||
|
|
||
| } |
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.
현재 스크립트 시나리오, 항상 prod 을 가지고 있는데요,
dev 의 경우에는 배포를 따로할까요?
보통 외부에서 환경에 대한 변수 dev, prod 등을 받고 내부에서 이것을 결정하게 하는 방법을 사용하고 있는데요,
지금의 배포 환경을 점검해보고 변경이 좋다면 변경하면 좋을거 같습니다.