-
Notifications
You must be signed in to change notification settings - Fork 78
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
6주차 과제 - 로그인 구현하기 #70
Merged
Merged
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
50766c1
Controller , Service 주석 작성
jdalma 6974c98
SessionController login메소드 추가
jdalma 3f6ec78
jwtUtil 추가
jdalma e500054
InvalidTokenException advice 추가
jdalma a3bb4f6
상품 등록 시 JWT 및 사용자 확인
jdalma 6caff11
findUser public으로 변경
jdalma c32ae97
ProductController 테스트 코드 작성
jdalma df8f01e
JwtUtil 테스트 코드 작성
jdalma a1ecba0
JwtUtil null 체크 숨기기
jdalma 93ff275
JwtUtil 주석, 테스트 코드 추가
jdalma 2c04eae
ddl-auto create로 수정
jdalma 90f9e60
ProductController 테스트 코드 수정
jdalma a8d90b9
JwtUtil 테스트 코드 수정
jdalma 05db189
AuthenticationService tokenValidation메소드 추가
JwtUtil 메소드 추가 및 수정
jdalma 7efccfe
decode메소드 검증 부분 수정
jdalma c833de7
JwtUtil.encode() 테스트 코드 수정
jdalma c793d25
Session 응답 DTO 추가
jdalma 7930ef9
Session spec 사용자 이메일 수정
jdalma a64d3c4
로그인 시 사용자 email , password를 확인하여 사용자 검증 및 JWT 반환 추가
jdalma 324f9db
final 선언 및 스코프 줄이기
jdalma 042eaaa
빈 검증 실패 시 에러 메시지 추가
jdalma ea2588a
수정 및 ExceptionHandler 테스트 코드 추가
jdalma 0699e11
JwtUtil decode 유효성 검사 수정
jdalma 1f1b99f
Product 경로 인증 인터셉터 추가
jdalma 1888b60
UserLoginValidator 임시 추가
jdalma 522ac8f
Update app/src/main/java/com/codesoom/assignment/application/Authenti…
jdalma 7760d3f
AuthenticationService 구현 숨기기
jdalma 5cb2b86
인터페이스 추가
jdalma File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
app/src/main/java/com/codesoom/assignment/application/AuthenticationService.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,56 @@ | ||
package com.codesoom.assignment.application; | ||
|
||
import com.codesoom.assignment.domain.User; | ||
import com.codesoom.assignment.dto.UserLoginData; | ||
import com.codesoom.assignment.errors.InvalidTokenException; | ||
import com.codesoom.assignment.errors.UserNotFoundException; | ||
import com.codesoom.assignment.errors.WrongPasswordException; | ||
import com.codesoom.assignment.utils.JwtUtil; | ||
import com.github.dozermapper.core.Mapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Slf4j | ||
public class AuthenticationService { | ||
|
||
private final JwtUtil jwtUtil; | ||
private final UserService userService; | ||
private final Mapper mapper; | ||
|
||
public AuthenticationService(JwtUtil jwtUtil, UserService userService , Mapper dozerMapper) { | ||
this.jwtUtil = jwtUtil; | ||
this.userService = userService; | ||
this.mapper = dozerMapper; | ||
|
||
} | ||
|
||
/** | ||
* JWT 토큰을 반환한다. | ||
* | ||
* @param loginData 로그인 정보 | ||
* @throws UserNotFoundException 로그인 정보에 해당하는 사용자가 존재하지 않을 경우 | ||
* @throws WrongPasswordException 사용자의 패스워드 정보가 일치하지 않은 경우 | ||
* @return JWT 반환 | ||
*/ | ||
public String login(UserLoginData loginData){ | ||
User user = mapper.map(loginData , User.class); | ||
User findUser = userService.findByEmail(user.getEmail()); | ||
if(!findUser.authenticate(user.getPassword())){ | ||
throw new WrongPasswordException(); | ||
} | ||
return jwtUtil.encode(findUser.getId()); | ||
} | ||
|
||
/** | ||
* JWT를 검증한다. | ||
* | ||
* @param token JWT | ||
* @throws InvalidTokenException 토큰 정보가 null 또는 사이즈가 0이거나 첫 글자가 공백 , 유효하지 않은 토큰이라면 예외를 던진다. | ||
* @throws UserNotFoundException 페이로드에 담긴 식별자에 해당하는 사용자가 없는 경우 | ||
*/ | ||
public void tokenValidation(String token){ | ||
Long id = jwtUtil.getUserIdFromToken(token); | ||
userService.findUser(id); | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/codesoom/assignment/controllers/GlobalExceptionHandler.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,32 @@ | ||
package com.codesoom.assignment.controllers; | ||
|
||
import com.codesoom.assignment.dto.ErrorResponse; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@Override | ||
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { | ||
|
||
List<String> errors = ex.getBindingResult().getFieldErrors() | ||
.stream() | ||
.map(e -> String.format("[%s] %s" , e.getField() , e.getDefaultMessage())) | ||
.collect(Collectors.toList()); | ||
ErrorResponse response = ErrorResponse.builder() | ||
.errors(errors) | ||
.status(HttpStatus.BAD_REQUEST) | ||
.build(); | ||
|
||
return new ResponseEntity<>(response, response.getStatus()); | ||
} | ||
} |
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.
이런식으로 메소드를 추출해본다면 어떨까요? 구현을 감추면 좀 더 가독성이 좋을 것 같아요!
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.
데이터를 요청하지 말고 작업을 요청해라 명심하겠습니다.