-
Notifications
You must be signed in to change notification settings - Fork 23
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
9주차 미션 / 서버 1조 김상준 #1
Open
drbug2000
wants to merge
2
commits into
Konkuk-KUIT:master
Choose a base branch
from
drbug2000:kimsangjune
base: master
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 1 commit
Commits
Show all changes
2 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 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/kuit3/backend/common/argument_resolver/JwtAuthrize.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 kuit3.backend.common.argument_resolver; | ||
|
||
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 JwtAuthrize { | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/kuit3/backend/common/exception/ShopException.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 kuit3.backend.common.exception; | ||
|
||
|
||
import kuit3.backend.common.response.status.ResponseStatus; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ShopException extends RuntimeException { | ||
|
||
private final ResponseStatus exceptionStatus; | ||
|
||
public ShopException(ResponseStatus exceptionStatus) { | ||
super(exceptionStatus.getMessage()); | ||
this.exceptionStatus = exceptionStatus; | ||
} | ||
|
||
public ShopException(ResponseStatus exceptionStatus, String message) { | ||
super(message); | ||
this.exceptionStatus = exceptionStatus; | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import kuit3.backend.common.exception.jwt.bad_request.JwtNoTokenException; | ||
import kuit3.backend.common.exception.jwt.bad_request.JwtUnsupportedTokenException; | ||
import kuit3.backend.jwt.JwtProvider; | ||
import kuit3.backend.jwt.JwtUtil; | ||
import kuit3.backend.service.AuthService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
@@ -24,47 +25,48 @@ public class JwtAuthInterceptor implements HandlerInterceptor { | |
private static final String JWT_TOKEN_PREFIX = "Bearer "; | ||
|
||
private final JwtProvider jwtProvider; | ||
private final JwtUtil jwtUtil; | ||
private final AuthService authService; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
|
||
String accessToken = resolveAccessToken(request); | ||
validateAccessToken(accessToken); | ||
String accessToken = jwtUtil.resolveAccessToken(request); | ||
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. 👍 |
||
jwtUtil.validateAccessToken(accessToken); | ||
|
||
String email = jwtProvider.getPrincipal(accessToken); | ||
validatePayload(email); | ||
jwtUtil.validatePayload(email); | ||
|
||
long userId = authService.getUserIdByEmail(email); | ||
request.setAttribute("userId", userId); | ||
return true; | ||
} | ||
|
||
private String resolveAccessToken(HttpServletRequest request) { | ||
String token = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
validateToken(token); | ||
return token.substring(JWT_TOKEN_PREFIX.length()); | ||
} | ||
|
||
private void validateToken(String token) { | ||
if (token == null) { | ||
throw new JwtNoTokenException(TOKEN_NOT_FOUND); | ||
} | ||
if (!token.startsWith(JWT_TOKEN_PREFIX)) { | ||
throw new JwtUnsupportedTokenException(UNSUPPORTED_TOKEN_TYPE); | ||
} | ||
} | ||
|
||
private void validateAccessToken(String accessToken) { | ||
if (jwtProvider.isExpiredToken(accessToken)) { | ||
throw new JwtExpiredTokenException(EXPIRED_TOKEN); | ||
} | ||
} | ||
|
||
private void validatePayload(String email) { | ||
if (email == null) { | ||
throw new JwtInvalidTokenException(INVALID_TOKEN); | ||
} | ||
} | ||
// private String resolveAccessToken(HttpServletRequest request) { | ||
// String token = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
// validateToken(token); | ||
// return token.substring(JWT_TOKEN_PREFIX.length()); | ||
// } | ||
// | ||
// private void validateToken(String token) { | ||
// if (token == null) { | ||
// throw new JwtNoTokenException(TOKEN_NOT_FOUND); | ||
// } | ||
// if (!token.startsWith(JWT_TOKEN_PREFIX)) { | ||
// throw new JwtUnsupportedTokenException(UNSUPPORTED_TOKEN_TYPE); | ||
// } | ||
// } | ||
// | ||
// private void validateAccessToken(String accessToken) { | ||
// if (jwtProvider.isExpiredToken(accessToken)) { | ||
// throw new JwtExpiredTokenException(EXPIRED_TOKEN); | ||
// } | ||
// } | ||
// | ||
// private void validatePayload(String email) { | ||
// if (email == null) { | ||
// throw new JwtInvalidTokenException(INVALID_TOKEN); | ||
// } | ||
// } | ||
|
||
} |
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
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
77 changes: 77 additions & 0 deletions
77
src/main/java/kuit3/backend/controller/ShopController.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,77 @@ | ||
package kuit3.backend.controller; | ||
|
||
|
||
import com.fasterxml.jackson.databind.annotation.JsonValueInstantiator; | ||
import kuit3.backend.common.exception.ShopException; | ||
import kuit3.backend.common.response.BaseResponse; | ||
import kuit3.backend.dto.shop.*; | ||
import kuit3.backend.service.ShopService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.hibernate.validator.constraints.Range; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
import static kuit3.backend.common.response.status.BaseExceptionResponseStatus.INVALID_SHOP_VALUE; | ||
import static kuit3.backend.util.BindingResultUtils.getErrorMessages; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/shop") | ||
public class ShopController { | ||
private final ShopService shopService; | ||
|
||
/** | ||
* shop 추가 | ||
* */ | ||
@PostMapping("") | ||
public BaseResponse<PostShopResponse> signUp(@Validated @RequestBody PostShopRequest postShopRequest, BindingResult bindingResult) { | ||
log.info("[ShopController.addShop]"); | ||
if (bindingResult.hasErrors()) { | ||
throw new ShopException(INVALID_SHOP_VALUE, getErrorMessages(bindingResult)); | ||
} | ||
return new BaseResponse<>(shopService.addShop(postShopRequest)); | ||
} | ||
@GetMapping("/list") | ||
public BaseResponse<List<Shop>> getShops(@RequestParam(required = false) String category, @RequestParam(required = false) String address) { | ||
if (category != null && address != null) { | ||
// 카테고리와 주소로 가게 검색 | ||
return new BaseResponse<>(shopService.getShopsByCategoryAndAddress(category, address)); | ||
|
||
} else if (category != null) { | ||
// 카테고리로 가게 검색 | ||
return new BaseResponse<>(shopService.getShopsByCategory(category)); | ||
} else if (address != null) { | ||
// 주소로 가게 검색 | ||
return new BaseResponse<>(shopService.getShopsByAddress(address)); | ||
} else { | ||
// 카테고리와 주소가 주어지지 않은 경우 모든 가게 목록 반환 | ||
return new BaseResponse<>( shopService.getAllShops()); | ||
} | ||
} | ||
@GetMapping("/list-page") | ||
public BaseResponse<GetShopListResponse> getShopPage(@Validated @RequestBody GetShopListRequest getShopListRequest) { | ||
return new BaseResponse<>(shopService.getShopList(getShopListRequest)); | ||
} | ||
@GetMapping("/detail") | ||
public BaseResponse<List<Shop>> getShopDetail(@Validated @RequestParam long shopId, BindingResult bindingResult) { | ||
if (bindingResult.hasErrors()) { | ||
throw new ShopException(INVALID_SHOP_VALUE, getErrorMessages(bindingResult)); | ||
} | ||
return new BaseResponse<>(shopService.getShopById(shopId)); | ||
} | ||
|
||
@GetMapping("/food-categories") | ||
public List<FoodCategory> getAllFoodCategories() { | ||
return shopService.getAllFoodCategories(); | ||
} | ||
|
||
@PostMapping("/food-categories/{foodCategory}") | ||
public BaseResponse<String> postAllFoodCategories(@Range(min=1,max=10) @PathVariable String foodCategory) { | ||
return new BaseResponse<>(shopService.addFoodCategory(foodCategory)); | ||
} | ||
} |
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.
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.
토큰을 생성할 때부터 payload에 userId를 담아 생성한다면 ArgumentResolver에서 Service를 호출하는 행위를 방지할 수 있을 것 같아요:) 레이어드 아키텍처의 의존 방향을 생각해보시면 좋을 것 같습니다! (사실 9주차 미션 제공 코드에는 이미 payload에 userId를 담아둡니다..!! 서비스 단으로 갈 필요 없이 바로 추출해서 사용하면 돼요)