-
Notifications
You must be signed in to change notification settings - Fork 46
[BE][team9] oauth + jwt 를 이용한 토큰 인증방식 구현 #46
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
base: team9
Are you sure you want to change the base?
Changes from all commits
6199cdf
2dc7b5b
db57480
9bc9df5
fa8c1a0
099aa17
e4dcfed
a0f7962
47cb664
82acf9e
e898e47
c8c7673
3cca64c
a87f849
1648b51
483d9ac
e26fd9f
f2627bf
8a0e084
ed7ad9c
c07a15b
17e3525
a8363e0
95c8076
ab4c65a
6a380c7
8bc5204
3ed7565
9671f43
1632a7b
676eceb
bb1b081
b1e9cf9
dab22b6
2b03550
80da780
7c34dfd
548cb08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package team9.baseball.DTO.response.github; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class GithubEmailDTO { | ||
| private String email; | ||
| private boolean primary; | ||
| private boolean verified; | ||
| private String visibility; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package team9.baseball.DTO.response.github; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class GithubTokenDTO { | ||
| @JsonProperty("access_token") | ||
| private String accessToken; | ||
|
|
||
| @JsonProperty("token_type") | ||
| private String tokenType; | ||
|
|
||
| @JsonProperty("scope") | ||
| private String scope; | ||
|
|
||
| public String getAuthorizationValue() { | ||
| return this.tokenType + " " + this.accessToken; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package team9.baseball.config; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.servlet.HandlerInterceptor; | ||
| import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
|
||
| @Configuration | ||
| public class WebMvcConfig implements WebMvcConfigurer { | ||
| private HandlerInterceptor interceptor; | ||
|
|
||
| @Autowired | ||
| public WebMvcConfig(@Qualifier(value = "certificationInterceptor") HandlerInterceptor interceptor) { | ||
| this.interceptor = interceptor; | ||
| } | ||
|
Comment on lines
+12
to
+17
Member
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. 그냥 CertificationInterceptor를 직접 받아도 상관 없다는 생각이 듭니다. |
||
|
|
||
| @Override | ||
| public void addInterceptors(InterceptorRegistry registry) { | ||
| registry.addInterceptor(interceptor) | ||
| .addPathPatterns("/**") | ||
| .excludePathPatterns("/user/**/") | ||
| .excludePathPatterns("/game/list"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import team9.baseball.DTO.response.ApiResult; | ||
| import team9.baseball.service.GameService; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.validation.Valid; | ||
|
|
||
| @RestController | ||
|
|
@@ -27,29 +28,40 @@ public ApiResult getGameDescriptions() { | |
|
|
||
| @PostMapping | ||
| public ApiResult createGame(@RequestBody CreateGameDTO createGameDTO) { | ||
| gameService.createNewGame(1l, createGameDTO.getAwayTeamId(), createGameDTO.getHomeTeamId()); | ||
| return ApiResult.succeed("OK"); | ||
| gameService.createNewGame(createGameDTO.getAwayTeamId(), createGameDTO.getHomeTeamId()); | ||
| return ApiResult.ok(); | ||
| } | ||
|
|
||
| @PostMapping("/joining") | ||
|
Member
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. 저라면, path에 join하려는 게임이 무엇인지도 포함해줬을 것 같아요. |
||
| public ApiResult joinGame(@Valid @RequestBody JoinGameDTO joinGameDTO) { | ||
| gameService.joinGame(1l, joinGameDTO.getGameId(), joinGameDTO.getMyVenue()); | ||
| return ApiResult.succeed("OK"); | ||
| public ApiResult joinGame(@Valid @RequestBody JoinGameDTO joinGameDTO, HttpServletRequest request) { | ||
| long userId = (long) request.getAttribute("userId"); | ||
|
Comment on lines
+36
to
+37
Member
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. ServletRequest에서 필요한 데이터를 바인딩 하는 방법에 대해서 찾아보면 어떨까요?
Member
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. 모든 응답을 ApiResult로 반환하는건 제 생각엔 그다지 좋지 않은 것 같습니다. |
||
| gameService.joinGame(userId, joinGameDTO.getGameId(), joinGameDTO.getMyVenue()); | ||
| return ApiResult.ok(); | ||
| } | ||
|
|
||
| @DeleteMapping("/joining") | ||
| public ApiResult quitGame(HttpServletRequest request) { | ||
|
Comment on lines
+42
to
+43
Member
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. 그냥 quit을 POST 요청 보내는것이 더 직관적인 API일 것 같습니다. |
||
| long userId = (long) request.getAttribute("userId"); | ||
| gameService.quitGame(userId); | ||
| return ApiResult.ok(); | ||
| } | ||
|
|
||
| @GetMapping("/status") | ||
|
Member
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. 무엇의 상태인가요? |
||
| public ApiResult getCurrentGameStatus() { | ||
| return ApiResult.succeed(gameService.getCurrentGameStatus(1l)); | ||
| public ApiResult getCurrentGameStatus(HttpServletRequest request) { | ||
| long userId = (long) request.getAttribute("userId"); | ||
| return ApiResult.succeed(gameService.getCurrentGameStatus(userId)); | ||
| } | ||
|
|
||
| @PostMapping("/status/pitch-result") | ||
| public ApiResult pitch(@RequestBody PitchResultDTO pitchResultDTO) { | ||
| gameService.applyPitchResult(1l, pitchResultDTO.getPitchResult()); | ||
| return ApiResult.succeed("OK"); | ||
| public ApiResult pitch(@RequestBody PitchResultDTO pitchResultDTO, HttpServletRequest request) { | ||
| long userId = (long) request.getAttribute("userId"); | ||
| gameService.applyPitchResult(userId, pitchResultDTO.getPitchResult()); | ||
| return ApiResult.ok(); | ||
| } | ||
|
|
||
| @GetMapping("/history") | ||
| public ApiResult getCurrentGameHistory() { | ||
| return ApiResult.succeed(gameService.getCurrentGameHistory(1l)); | ||
| public ApiResult getCurrentGameHistory(HttpServletRequest request) { | ||
| long userId = (long) request.getAttribute("userId"); | ||
| return ApiResult.succeed(gameService.getCurrentGameHistory(userId)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package team9.baseball.controller; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import team9.baseball.DTO.response.ApiResult; | ||
| import team9.baseball.domain.enums.ResourceServer; | ||
| import team9.baseball.service.OauthService; | ||
| import team9.baseball.service.UserService; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/user") | ||
| public class ApiUserController { | ||
| private final UserService userService; | ||
| private final OauthService oauthService; | ||
|
|
||
| @Autowired | ||
|
Member
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. 굳이 안붙여줘도 되는 애노테이션입니다. |
||
| public ApiUserController(UserService userService, OauthService oauthService) { | ||
| this.userService = userService; | ||
| this.oauthService = oauthService; | ||
| } | ||
|
|
||
| @GetMapping("/login/oauth/github") | ||
| public ApiResult loginUsingGithub(String code) { | ||
| String accessToken = oauthService.getAccessToken(code); | ||
| String email = oauthService.getEmail(accessToken); | ||
|
|
||
| return ApiResult.succeed(userService.signIn(email, ResourceServer.GITHUB)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,10 @@ | |
| import org.springframework.data.annotation.Id; | ||
| import org.springframework.data.relational.core.mapping.MappedCollection; | ||
| import team9.baseball.domain.aggregate.team.Team; | ||
| import team9.baseball.domain.enums.GameStatus; | ||
| import team9.baseball.domain.enums.Halves; | ||
| import team9.baseball.domain.enums.PitchResult; | ||
| import team9.baseball.exception.BadStatusException; | ||
| import team9.baseball.exception.NotFoundException; | ||
|
|
||
| import java.util.HashMap; | ||
|
|
@@ -45,12 +47,17 @@ public class Game { | |
|
|
||
| private int outCount; | ||
|
|
||
| private GameStatus status; | ||
|
|
||
| @MappedCollection(idColumn = "game_id", keyColumn = "key_in_game") | ||
| private Map<String, BattingHistory> battingHistoryMap = new HashMap<>(); | ||
|
|
||
| @MappedCollection(idColumn = "game_id", keyColumn = "key_in_game") | ||
| private Map<String, Inning> inningMap = new HashMap<>(); | ||
|
|
||
| private final int STRIKE_OUT_COUNT = 3; | ||
| private final int INNING_OUT_COUNT = 3; | ||
|
|
||
| public Game(Team awayTeam, Team homeTeam) { | ||
| this.awayTeamId = awayTeam.getId(); | ||
| this.homeTeamId = homeTeam.getId(); | ||
|
|
@@ -63,6 +70,8 @@ public Game(Team awayTeam, Team homeTeam) { | |
| this.currentInning = 1; | ||
| this.currentHalves = Halves.TOP; | ||
| this.inningMap.put(Inning.acquireKeyInGame(currentInning, currentHalves), new Inning(currentInning, currentHalves)); | ||
|
|
||
| this.status = GameStatus.WAITING; | ||
| } | ||
|
|
||
| private void initializeBattingHistory(Team team) { | ||
|
|
@@ -73,30 +82,42 @@ private void initializeBattingHistory(Team team) { | |
| } | ||
| } | ||
|
|
||
| public void checkWaiting() { | ||
| if (this.status != GameStatus.WAITING) { | ||
| throw new BadStatusException("대기중인 게임이 아닙니다."); | ||
| } | ||
| } | ||
|
|
||
| public void checkPlaying() { | ||
| if (this.status != GameStatus.PLAYING) { | ||
| throw new BadStatusException("진행중인 게임이 아닙니다."); | ||
| } | ||
| } | ||
|
Comment on lines
+85
to
+95
Member
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. 👍 |
||
|
|
||
| public void proceedStrike(Team awayTeam, Team homeTeam) { | ||
| //카운트 증가 | ||
| this.strikeCount++; | ||
| //기록할 pitch history 생성 | ||
| PitchHistory pitchHistory = new PitchHistory(acquireDefenseTeamId(), pitcherUniformNumber, | ||
| acquireAttackTeamId(), batterUniformNumber, PitchResult.STRIKE, this.strikeCount, this.ballCount); | ||
| //현재 이닝에 pitch history 기록 | ||
| acquireCurrentInning().pitchHistoryList.add(pitchHistory); | ||
|
|
||
| //카운트 증가 | ||
| this.strikeCount++; | ||
| //삼진 아웃 처리 | ||
| if (strikeCount == 3) { | ||
| if (strikeCount == STRIKE_OUT_COUNT) { | ||
| proceedOut(awayTeam, homeTeam); | ||
| } | ||
| } | ||
|
|
||
| public void proceedBall(Team awayTeam, Team homeTeam) { | ||
| //카운트 증가 | ||
| this.ballCount++; | ||
| //기록할 pitch history 생성 | ||
| PitchHistory pitchHistory = new PitchHistory(acquireDefenseTeamId(), pitcherUniformNumber, | ||
| acquireAttackTeamId(), batterUniformNumber, PitchResult.BALL, this.strikeCount, this.ballCount); | ||
| //현재 이닝에 pitch history 기록 | ||
| acquireCurrentInning().pitchHistoryList.add(pitchHistory); | ||
|
|
||
| //카운트 증가 | ||
| this.ballCount++; | ||
| //볼넷일 경우 출루하고 다음 타자 등판 | ||
| if (ballCount == 4) { | ||
| sendBatterOnBase(); | ||
|
|
@@ -191,7 +212,7 @@ private void proceedOut(Team awayTeam, Team homeTeam) { | |
| battingHistory.plusOut(); | ||
|
|
||
| //3회 아웃이면 다음이닝으로 변경 | ||
| if (outCount == 3) { | ||
| if (outCount == INNING_OUT_COUNT) { | ||
| goToNextInning(awayTeam, homeTeam); | ||
| return; | ||
| } | ||
|
|
@@ -214,10 +235,19 @@ private void sendBatterOnBase() { | |
| } | ||
|
|
||
| private void goToNextInning(Team awayTeam, Team homeTeam) { | ||
| //게임 종료 상황이면 다음이닝으로 넘어가지 않고 게임종료 | ||
| if (isExited()) { | ||
| this.status = GameStatus.EXITED; | ||
|
Member
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. 상태를 바꾸는 부분이 여기 있어도 될까요? |
||
| return; | ||
| } | ||
|
|
||
| //카운트 초기화 | ||
| this.strikeCount = 0; | ||
| this.ballCount = 0; | ||
| this.outCount = 0; | ||
| this.base1UniformNumber = null; | ||
| this.base2UniformNumber = null; | ||
| this.base3UniformNumber = null; | ||
|
|
||
| //다음 이닝으로 변경 | ||
| if (this.currentHalves == Halves.BOTTOM) { | ||
|
|
@@ -240,6 +270,19 @@ private void goToNextInning(Team awayTeam, Team homeTeam) { | |
| sendBatterOnPlate(attackTeam.getId(), nextBatterUniformNumber); | ||
| } | ||
|
|
||
| private boolean isScoreDifferent() { | ||
| return getTotalScore(Halves.TOP) != getTotalScore(Halves.BOTTOM); | ||
| } | ||
|
|
||
| private boolean isExited() { | ||
| if ((currentInning == 9 && currentHalves == Halves.BOTTOM && isScoreDifferent()) | ||
| || (currentInning == 12 && currentHalves == Halves.BOTTOM)) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private void sendBatterOnPlate(int batterTeamId, int nextBatterUniformNumber) { | ||
| //카운트 초기화 | ||
| this.strikeCount = 0; | ||
|
|
||
This file was deleted.
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.
👍