forked from cho-log/spring-learning-test
-
Notifications
You must be signed in to change notification settings - Fork 5
[spring-auth-1] gib.son(손수환) 미션 제출합니다. #66
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
swandevson
wants to merge
3
commits into
Japring-Study:gib/spring-auth-1
Choose a base branch
from
swandevson:gib/spring-auth-1
base: gib/spring-auth-1
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
56 changes: 29 additions & 27 deletions
56
spring-auth-1/initial/src/main/java/cholog/auth/ui/BasicLoginController.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 |
|---|---|---|
| @@ -1,44 +1,46 @@ | ||
| package cholog.auth.ui; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import cholog.auth.application.AuthService; | ||
| import cholog.auth.application.AuthorizationException; | ||
| import cholog.auth.dto.AuthInfo; | ||
| import cholog.auth.dto.MemberResponse; | ||
| import cholog.auth.infrastructure.AuthorizationExtractor; | ||
| import cholog.auth.infrastructure.BasicAuthorizationExtractor; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| public class BasicLoginController { | ||
| private final AuthService authService; | ||
| private final AuthorizationExtractor<AuthInfo> authorizationExtractor; | ||
| private final AuthService authService; | ||
| private final AuthorizationExtractor<AuthInfo> authorizationExtractor; | ||
|
|
||
| public BasicLoginController(AuthService authService) { | ||
| this.authService = authService; | ||
| this.authorizationExtractor = new BasicAuthorizationExtractor(); | ||
| } | ||
| public BasicLoginController(AuthService authService) { | ||
| this.authService = authService; | ||
| this.authorizationExtractor = new BasicAuthorizationExtractor(); | ||
| } | ||
|
|
||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * GET /members/me/basic HTTP/1.1 | ||
| * authorization: Basic ZW1haWxAZW1haWwuY29tOjEyMzQ= | ||
| * accept: application/json | ||
| */ | ||
| @GetMapping("/members/me/basic") | ||
| public ResponseEntity<MemberResponse> findMyInfo(HttpServletRequest request) { | ||
| // TODO: authorization 헤더의 Basic 값에 있는 email과 password 추출 (hint: authorizationExtractor 사용) | ||
| String email = ""; | ||
| String password = ""; | ||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * GET /members/me/basic HTTP/1.1 | ||
| * authorization: Basic ZW1haWxAZW1haWwuY29tOjEyMzQ= | ||
| * accept: application/json | ||
| */ | ||
| @GetMapping("/members/me/basic") | ||
| public ResponseEntity<MemberResponse> findMyInfo(HttpServletRequest request) { | ||
| AuthInfo authInfo = authorizationExtractor.extract(request); | ||
|
|
||
| String email = authInfo.getEmail(); | ||
| String password = authInfo.getPassword(); | ||
|
|
||
| if (authService.checkInvalidLogin(email, password)) { | ||
| throw new AuthorizationException(); | ||
| } | ||
| if (authService.checkInvalidLogin(email, password)) { | ||
| throw new AuthorizationException(); | ||
| } | ||
|
|
||
| MemberResponse member = authService.findMember(email); | ||
| return ResponseEntity.ok().body(member); | ||
| } | ||
| MemberResponse member = authService.findMember(email); | ||
| return ResponseEntity.ok().body(member); | ||
| } | ||
| } |
106 changes: 52 additions & 54 deletions
106
spring-auth-1/initial/src/main/java/cholog/auth/ui/SessionLoginController.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 |
|---|---|---|
| @@ -1,65 +1,63 @@ | ||
| package cholog.auth.ui; | ||
|
|
||
| import cholog.auth.application.AuthService; | ||
| import cholog.auth.application.AuthorizationException; | ||
| import cholog.auth.dto.MemberResponse; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpSession; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.Map; | ||
| import cholog.auth.application.AuthService; | ||
| import cholog.auth.application.AuthorizationException; | ||
| import cholog.auth.dto.MemberResponse; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpSession; | ||
|
|
||
| @RestController | ||
| public class SessionLoginController { | ||
| private static final String SESSION_KEY = "USER"; | ||
| private static final String USERNAME_FIELD = "email"; | ||
| private static final String PASSWORD_FIELD = "password"; | ||
|
|
||
| private final AuthService authService; | ||
|
|
||
| public SessionLoginController(AuthService authService) { | ||
| this.authService = authService; | ||
| } | ||
|
|
||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * POST /login/session HTTP/1.1 | ||
| * content-type: application/x-www-form-urlencoded; charset=ISO-8859-1 | ||
| * host: localhost:55477 | ||
| * <p> | ||
| * email=email@email.com&password=1234 | ||
| */ | ||
| @PostMapping("/login/session") | ||
| public ResponseEntity<Void> sessionLogin(HttpServletRequest request, HttpSession session) { | ||
| // TODO: HttpRequest로 받은 email과 password 추출 | ||
| String email = ""; | ||
| String password = ""; | ||
|
|
||
| if (authService.checkInvalidLogin(email, password)) { | ||
| throw new AuthorizationException(); | ||
| } | ||
|
|
||
| // TODO: Session에 인증 정보 저장 (key: SESSION_KEY, value: email값) | ||
|
|
||
| return ResponseEntity.ok().build(); | ||
| } | ||
|
|
||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * GET /members/me/session HTTP/1.1 | ||
| * cookie: JSESSIONID=E7263AC9557EF658C888F02EEF840A19 | ||
| * accept: application/json | ||
| */ | ||
| @GetMapping("/members/me/session") | ||
| public ResponseEntity<MemberResponse> findMyInfo(HttpSession session) { | ||
| // TODO: Session을 통해 인증 정보 조회 (key: SESSION_KEY) | ||
| String email = ""; | ||
| MemberResponse member = authService.findMember(email); | ||
| return ResponseEntity.ok().body(member); | ||
| } | ||
| private static final String SESSION_KEY = "USER"; | ||
| private static final String USERNAME_FIELD = "email"; | ||
| private static final String PASSWORD_FIELD = "password"; | ||
|
|
||
| private final AuthService authService; | ||
|
|
||
| public SessionLoginController(AuthService authService) { | ||
| this.authService = authService; | ||
| } | ||
|
|
||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * POST /login/session HTTP/1.1 | ||
| * content-type: application/x-www-form-urlencoded; charset=ISO-8859-1 | ||
| * host: localhost:55477 | ||
| * <p> | ||
| * email=email@email.com&password=1234 | ||
| */ | ||
| @PostMapping("/login/session") | ||
| public ResponseEntity<Void> sessionLogin(HttpServletRequest request, HttpSession session) { | ||
| String email = request.getParameter("email"); | ||
| String password = request.getParameter("password"); | ||
|
|
||
| if (authService.checkInvalidLogin(email, password)) { | ||
| throw new AuthorizationException(); | ||
| } | ||
|
|
||
| session.setAttribute("SESSION_KEY", email); | ||
|
|
||
| return ResponseEntity.ok().build(); | ||
| } | ||
|
|
||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * GET /members/me/session HTTP/1.1 | ||
| * cookie: JSESSIONID=E7263AC9557EF658C888F02EEF840A19 | ||
| * accept: application/json | ||
| */ | ||
| @GetMapping("/members/me/session") | ||
| public ResponseEntity<MemberResponse> findMyInfo(HttpSession session) { | ||
| String email = (String)session.getAttribute("SESSION_KEY"); | ||
| MemberResponse member = authService.findMember(email); | ||
|
|
||
| return ResponseEntity.ok().body(member); | ||
| } | ||
| } | ||
89 changes: 46 additions & 43 deletions
89
spring-auth-1/initial/src/main/java/cholog/auth/ui/TokenLoginController.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 |
|---|---|---|
| @@ -1,59 +1,62 @@ | ||
| package cholog.auth.ui; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import cholog.auth.application.AuthService; | ||
| import cholog.auth.dto.MemberResponse; | ||
| import cholog.auth.dto.TokenRequest; | ||
| import cholog.auth.dto.TokenResponse; | ||
| import cholog.auth.infrastructure.AuthorizationExtractor; | ||
| import cholog.auth.infrastructure.BearerAuthorizationExtractor; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| public class TokenLoginController { | ||
| private final AuthService authService; | ||
| private final AuthorizationExtractor<String> authorizationExtractor; | ||
| private final AuthService authService; | ||
| private final AuthorizationExtractor<String> authorizationExtractor; | ||
|
|
||
| public TokenLoginController(AuthService authService) { | ||
| this.authService = authService; | ||
| this.authorizationExtractor = new BearerAuthorizationExtractor(); | ||
| } | ||
|
|
||
| public TokenLoginController(AuthService authService) { | ||
| this.authService = authService; | ||
| this.authorizationExtractor = new BearerAuthorizationExtractor(); | ||
| } | ||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * POST /login/token HTTP/1.1 | ||
| * accept: application/json | ||
| * content-type: application/json; charset=UTF-8 | ||
| * <p> | ||
| * { | ||
| * "email": "email@email.com", | ||
| * "password": "1234" | ||
| * } | ||
| */ | ||
| @PostMapping("/login/token") | ||
| public ResponseEntity<TokenResponse> tokenLogin(@RequestBody TokenRequest tokenRequest) { | ||
| // TODO: email, password 정보를 가진 TokenRequest 값을 메서드 파라미터로 받아오기 (hint: @RequestBody) | ||
| TokenResponse tokenResponse = authService.createToken(tokenRequest); | ||
|
|
||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * POST /login/token HTTP/1.1 | ||
| * accept: application/json | ||
| * content-type: application/json; charset=UTF-8 | ||
| * <p> | ||
| * { | ||
| * "email": "email@email.com", | ||
| * "password": "1234" | ||
| * } | ||
| */ | ||
| @PostMapping("/login/token") | ||
| public ResponseEntity<TokenResponse> tokenLogin() { | ||
| // TODO: email, password 정보를 가진 TokenRequest 값을 메서드 파라미터로 받아오기 (hint: @RequestBody) | ||
| TokenRequest tokenRequest = null; | ||
| TokenResponse tokenResponse = authService.createToken(tokenRequest); | ||
| return ResponseEntity.ok().body(tokenResponse); | ||
| } | ||
| return ResponseEntity.ok().body(tokenResponse); | ||
| } | ||
|
|
||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * GET /members/me/token HTTP/1.1 | ||
| * authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJlbWFpbEBlbWFpbC5jb20iLCJpYXQiOjE2MTAzNzY2NzIsImV4cCI6MTYxMDM4MDI3Mn0.Gy4g5RwK1Nr7bKT1TOFS4Da6wxWh8l97gmMQDgF8c1E | ||
| * accept: application/json | ||
| */ | ||
| @GetMapping("/members/me/token") | ||
| public ResponseEntity<MemberResponse> findMyInfo(HttpServletRequest request) { | ||
| // TODO: authorization 헤더의 Bearer 값을 추출 (hint: authorizationExtractor 사용) | ||
| String token = ""; | ||
| MemberResponse member = authService.findMemberByToken(token); | ||
| return ResponseEntity.ok().body(member); | ||
| } | ||
| /** | ||
| * ex) request sample | ||
| * <p> | ||
| * GET /members/me/token HTTP/1.1 | ||
| * authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJlbWFpbEBlbWFpbC5jb20iLCJpYXQiOjE2MTAzNzY2NzIsImV4cCI6MTYxMDM4MDI3Mn0.Gy4g5RwK1Nr7bKT1TOFS4Da6wxWh8l97gmMQDgF8c1E | ||
| * accept: application/json | ||
| */ | ||
| @GetMapping("/members/me/token") | ||
| public ResponseEntity<MemberResponse> findMyInfo(HttpServletRequest request) { | ||
| // TODO: authorization 헤더의 Bearer 값을 추출 (hint: authorizationExtractor 사용) | ||
| String token = authorizationExtractor.extract(request); | ||
| MemberResponse member = authService.findMemberByToken(token); | ||
|
|
||
| return ResponseEntity.ok().body(member); | ||
| } | ||
| } |
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.
이것도 되나요?
쌍따옴표를 빼야할 것 같습니다!
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.
아 상수에
SESSION_KEY=“user”로 선언되어있었군요해당 세션 key에 value를 세팅하는거라, 가져올 때도 동일한 키면 문제없긴합니다