-
Notifications
You must be signed in to change notification settings - Fork 33
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
2-2 로그인 리팩토링 #50
Open
dyrlqhffo
wants to merge
9
commits into
next-step:dyrlqhffo
Choose a base branch
from
dyrlqhffo:step02
base: dyrlqhffo
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
2-2 로그인 리팩토링 #50
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c61c67d
전체적인 충돌 해결
dyrlqhffo 52f045d
feat : 2-2 로그인 리팩터링 페이지 수정
dyrlqhffo 1813438
2-2 로그인 리팩토링 HandlerMethodArgumentResolver
dyrlqhffo ad85cdd
테마 더미 데이터 추가로 테스트 코드 수정
dyrlqhffo 81f9699
예약시간 더미 데이터 추가로 테스트 코드 수정
dyrlqhffo 7df7c1c
테스트 코드 정리
dyrlqhffo 192cb01
2-2 로그인 리팩터링 예약 생성 기능 변경 - 사용자 완료
dyrlqhffo a457e02
feat : 전체적인 코드 수정
dyrlqhffo 20f10d2
feat : 2-2 로그인 리팩터링 예약 생성 기능 변경 - 관리자 완료
dyrlqhffo 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
40 changes: 40 additions & 0 deletions
40
src/main/java/roomescape/argumentresolver/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,40 @@ | ||
package roomescape.argumentresolver; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.util.StringUtils; | ||
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; | ||
import roomescape.dto.auth.AuthCheckResponse; | ||
import roomescape.exception.ErrorCode; | ||
import roomescape.exception.custom.AuthorizationException; | ||
import roomescape.jwt.JwtTokenProvider; | ||
import roomescape.service.AuthService; | ||
import roomescape.util.CookieUtil; | ||
|
||
import java.util.Optional; | ||
|
||
|
||
public class LoginArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
private final AuthService authService; | ||
public LoginArgumentResolver(AuthService authService) { | ||
this.authService = authService; | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(LoginUser.class); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { | ||
|
||
HttpServletRequest request = (HttpServletRequest)webRequest.getNativeRequest(); | ||
String token = CookieUtil.extractTokenFromCookie(request.getCookies()) | ||
.orElseThrow(() -> new AuthorizationException(ErrorCode.UNAUTHORIZED_USER, "로그인한 유저만 이용가능합니다.")); | ||
return authService.findUserFromToken(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package roomescape.argumentresolver; | ||
|
||
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 LoginUser { | ||
} |
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,24 @@ | ||
package roomescape.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import roomescape.argumentresolver.LoginArgumentResolver; | ||
import roomescape.jwt.JwtTokenProvider; | ||
import roomescape.service.AuthService; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
private final AuthService authService; | ||
|
||
public WebConfig(JwtTokenProvider jwtTokenProvider, AuthService authService) { | ||
this.authService = authService; | ||
} | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(new LoginArgumentResolver(authService)); | ||
} | ||
} |
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,15 +7,20 @@ | |
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import roomescape.argumentresolver.LoginUser; | ||
import roomescape.domain.User; | ||
import roomescape.dto.auth.AuthCheckResponse; | ||
import roomescape.dto.auth.AuthLoginRequest; | ||
import roomescape.dto.auth.AuthUserResponse; | ||
import roomescape.dto.auth.UserResponse; | ||
import roomescape.exception.ErrorCode; | ||
import roomescape.exception.custom.AuthorizationException; | ||
import roomescape.exception.custom.CookieNotFoundException; | ||
import roomescape.service.AuthService; | ||
import roomescape.util.CookieUtil; | ||
|
||
import java.util.Arrays; | ||
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. 불필요한 import 문과 공백들은 제거 부탁드려요! |
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RestController | ||
|
@@ -28,36 +33,32 @@ public AuthController(AuthService authService) { | |
this.authService = authService; | ||
} | ||
|
||
@GetMapping("/users") | ||
public ResponseEntity<List<UserResponse>> findUsers() { | ||
return ResponseEntity.ok().body(authService.findUsers()); | ||
} | ||
|
||
@PostMapping("/login") | ||
public ResponseEntity<Void> authLogin(@RequestBody AuthLoginRequest request, | ||
HttpServletResponse response) { | ||
String token = authService.authLogin(request); | ||
System.out.println(token); | ||
Cookie cookie = CookieUtil.createCookie(token); | ||
response.addCookie(cookie); | ||
return ResponseEntity.ok().build(); | ||
|
||
} | ||
|
||
@GetMapping("/login/check") | ||
public ResponseEntity<AuthCheckResponse> checkLogin(HttpServletRequest request) { | ||
Cookie[] cookies = request.getCookies(); | ||
String accessToken = CookieUtil.extractTokenFromCookie(cookies) | ||
.orElseThrow(() -> new AuthorizationException(ErrorCode.UNAUTHORIZED_USER, "다시 로그인 해주세요.")); | ||
AuthCheckResponse userResponse = authService.findUserFromToken(accessToken); | ||
return ResponseEntity.ok(userResponse); | ||
public ResponseEntity<AuthCheckResponse> checkLogin(@LoginUser User user) { | ||
return ResponseEntity.ok(authService.checkUser(user)); | ||
} | ||
|
||
@PostMapping("/logout") | ||
public ResponseEntity<Cookie> logout(HttpServletRequest request, HttpServletResponse response) { | ||
Cookie findCookie = Arrays.stream(request.getCookies()) | ||
.filter(cookie -> cookie.getName().equals("token")) | ||
.findFirst() | ||
.orElseThrow(()-> new CookieNotFoundException(ErrorCode.COOKIE_NOT_FOUND, "로그인이 되어 있지 않습니다.")); | ||
|
||
findCookie.setMaxAge(0); | ||
response.addCookie(findCookie); | ||
return ResponseEntity.ok(findCookie); | ||
public ResponseEntity<Cookie> logout(@LoginUser User user, HttpServletResponse response) { | ||
Cookie cookie = CookieUtil.createCookie(""); | ||
cookie.setMaxAge(0); | ||
response.addCookie(cookie); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,18 +1,23 @@ | ||
package roomescape.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import roomescape.argumentresolver.LoginUser; | ||
import roomescape.domain.Role; | ||
import roomescape.domain.User; | ||
import roomescape.dto.reservation.ReservationsResponse; | ||
import roomescape.dto.reservation.create.ReservationCreateRequest; | ||
import roomescape.dto.reservation.create.ReservationCreateResponse; | ||
import roomescape.exception.ErrorCode; | ||
import roomescape.exception.custom.AuthorizationException; | ||
import roomescape.service.ReservationService; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/reservations") | ||
@RequestMapping | ||
public class ReservationController { | ||
|
||
private final ReservationService reservationService; | ||
|
@@ -21,21 +26,36 @@ public ReservationController(ReservationService reservationService) { | |
this.reservationService = reservationService; | ||
} | ||
|
||
@GetMapping | ||
@GetMapping("/reservations") | ||
public ResponseEntity<List<ReservationsResponse>> findReservations() { | ||
List<ReservationsResponse> list = reservationService.findReservations(); | ||
return ResponseEntity.ok().body(list); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<ReservationCreateResponse> create(@Valid @RequestBody ReservationCreateRequest dto) { | ||
@PostMapping("/reservations") | ||
public ResponseEntity<ReservationCreateResponse> create(@Valid @RequestBody ReservationCreateRequest dto, | ||
@LoginUser User user) { | ||
dto.addUserName(user.getName()); | ||
ReservationCreateResponse reservation = reservationService.createReservation(dto); | ||
return ResponseEntity.ok().body(reservation); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@DeleteMapping("/reservations/{id}") | ||
public ResponseEntity<Void> delete(@PathVariable Long id) { | ||
reservationService.deleteReservation(id); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PostMapping("/admin/reservations") | ||
public ResponseEntity<ReservationCreateResponse> createAdminReservation( | ||
@Valid @RequestBody ReservationCreateRequest requestDto, | ||
@LoginUser User user){ | ||
|
||
if(user.getRole() != Role.ADMIN){ | ||
throw new AuthorizationException(ErrorCode.UNAUTHORIZED_ADMIN, "관리자 권한이 필요합니다."); | ||
} | ||
Comment on lines
+54
to
+56
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. 이번 단계에서 어느정도 admin 기능에 대한 권한 제어를 해주셨네요 👍 |
||
|
||
ReservationCreateResponse reservation = reservationService.createReservation(requestDto); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(reservation); | ||
} | ||
} |
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
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,5 @@ | ||
package roomescape.domain; | ||
|
||
public enum Role { | ||
USER, ADMIN | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package roomescape.dto.auth; | ||
|
||
import roomescape.domain.Role; | ||
import roomescape.domain.User; | ||
|
||
public class AuthUserResponse { | ||
private String name; | ||
private String email; | ||
private String password; | ||
private Role role; | ||
|
||
public AuthUserResponse(String name, String email, String password, Role role) { | ||
this.name = name; | ||
this.email = email; | ||
this.password = password; | ||
this.role = role; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public Role getRole() { | ||
return role; | ||
} | ||
} |
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.
ArgumentResolver 잘 활용해주셨네요 👍