-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] #11스프링시큐리티 예외처리 및 JWT 로직 일부 구현
- Loading branch information
Showing
30 changed files
with
588 additions
and
62 deletions.
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
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/leets/X/domain/user/dto/request/UserInitializeRequest.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,13 @@ | ||
package com.leets.X.domain.user.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record UserInitializeRequest( | ||
// 이 둘은 필수 입력이기 떄문에 NotBlank 제약 | ||
@NotNull LocalDate birth, // 날짜의 경우 @NotNull이 적용 x | ||
@NotBlank String customId | ||
) { | ||
} |
4 changes: 0 additions & 4 deletions
4
src/main/java/com/leets/X/domain/user/dto/request/UserSaveRequest.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/main/java/com/leets/X/domain/user/dto/request/UserSocialLoginRequest.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,8 @@ | ||
package com.leets.X.domain.user.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
|
||
public record UserSocialLoginRequest( | ||
@NotBlank String authCode | ||
) { | ||
} |
4 changes: 0 additions & 4 deletions
4
src/main/java/com/leets/X/domain/user/dto/response/UserResponse.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
src/main/java/com/leets/X/domain/user/dto/response/UserSocialLoginResponse.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,13 @@ | ||
package com.leets.X.domain.user.dto.response; | ||
|
||
import com.leets.X.domain.user.service.LoginStatus; | ||
import com.leets.X.global.auth.jwt.dto.JwtResponse; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record UserSocialLoginResponse( | ||
Long id, | ||
LoginStatus status, | ||
JwtResponse jwtToken | ||
) { | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/com/leets/X/domain/user/exception/UserNotFoundException.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
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
5 changes: 5 additions & 0 deletions
5
src/main/java/com/leets/X/domain/user/service/LoginStatus.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,5 @@ | ||
package com.leets.X.domain.user.service; | ||
|
||
public enum LoginStatus { | ||
LOGIN, REGISTER | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/java/com/leets/X/domain/user/service/UserService.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,95 @@ | ||
package com.leets.X.domain.user.service; | ||
|
||
import com.leets.X.domain.user.domain.User; | ||
import com.leets.X.domain.user.dto.request.UserInitializeRequest; | ||
import com.leets.X.domain.user.dto.response.UserSocialLoginResponse; | ||
import com.leets.X.domain.user.exception.UserNotFoundException; | ||
import com.leets.X.domain.user.repository.UserRepository; | ||
import com.leets.X.global.auth.google.AuthService; | ||
import com.leets.X.global.auth.google.dto.GoogleTokenResponse; | ||
import com.leets.X.global.auth.google.dto.GoogleUserInfoResponse; | ||
import com.leets.X.global.auth.jwt.JwtProvider; | ||
import com.leets.X.global.auth.jwt.dto.JwtResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static com.leets.X.domain.user.service.LoginStatus.LOGIN; | ||
import static com.leets.X.domain.user.service.LoginStatus.REGISTER; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
|
||
private final AuthService authService; | ||
private final JwtProvider jwtProvider; | ||
private final UserRepository userRepository; | ||
|
||
/* | ||
* 소셜 로그인 | ||
*/ | ||
@Transactional | ||
public UserSocialLoginResponse authenticate(String authCode) { | ||
GoogleTokenResponse token = authService.getGoogleAccessToken(authCode); | ||
GoogleUserInfoResponse userInfo = authService.getGoogleUserInfo(token.access_token()); | ||
|
||
String email = userInfo.email(); | ||
|
||
// 가입된 유저라면 로그인 | ||
if (existUser(email)){ | ||
return loginUser(userInfo.email()); | ||
} | ||
// 아니라면 회원가입 | ||
return registerUser(userInfo); | ||
} | ||
|
||
/* | ||
* 회원가입 시 초기 정보 입력 | ||
*/ | ||
@Transactional | ||
public void initProfile(UserInitializeRequest dto, String email){ | ||
User user = find(email); | ||
|
||
user.initProfile(dto); | ||
} | ||
|
||
private UserSocialLoginResponse loginUser(String email) { | ||
User user = find(email); | ||
|
||
return new UserSocialLoginResponse(user.getId(), LOGIN, generateToken(email)); | ||
} | ||
|
||
private UserSocialLoginResponse registerUser(GoogleUserInfoResponse userInfo) { | ||
User user = User.builder() | ||
.name(userInfo.name()) | ||
.email(userInfo.email()) | ||
.build(); | ||
|
||
userRepository.save(user); | ||
|
||
return new UserSocialLoginResponse(user.getId(), REGISTER, generateToken(user.getEmail())); | ||
} | ||
|
||
private JwtResponse generateToken (String email){ | ||
return JwtResponse.builder() | ||
.accessToken(jwtProvider.generateAccessToken(email)) | ||
.refreshToken(jwtProvider.generateRefreshToken()) | ||
.build(); | ||
} | ||
|
||
/* | ||
* userRepository에서 사용자를 검색하는 메서드 | ||
* 공통으로 사용되는 부분이 많기 때문에 별도로 분리 | ||
*/ | ||
public User find(String email){ | ||
return userRepository.findByEmail(email) | ||
.orElseThrow(UserNotFoundException::new); | ||
} | ||
|
||
public boolean existUser(String email){ | ||
return userRepository.existsByEmail(email); | ||
} | ||
|
||
} |
12 changes: 0 additions & 12 deletions
12
src/main/java/com/leets/X/domain/user/servie/UserService.java
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
src/main/java/com/leets/X/global/auth/authentication/CustomAuthenticationEntryPoint.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,52 @@ | ||
package com.leets.X.global.auth.authentication; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.leets.X.global.common.response.ResponseDto; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
import static com.leets.X.global.auth.exception.ErrorMessage.INVALID_TOKEN; | ||
import static com.leets.X.global.auth.exception.ErrorMessage.UNAUTHORIZED; | ||
|
||
@Slf4j | ||
@Component | ||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
private static final String LOG_FORMAT = "Class : {}, Code : {}, Message : {}"; | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
Integer exceptionCode = (Integer) request.getAttribute("jwtException"); | ||
|
||
/* | ||
* exceptionCode가 null이 아니라면 토큰 유효성 검사를 실패한 것이기 때문에 따로 처리 | ||
* exceptionCode가 null이라면 인증 정보가 없는 것이기 때문에 따로 처리 | ||
*/ | ||
if (exceptionCode != null) { | ||
if (exceptionCode == INVALID_TOKEN.getCode()){ | ||
setResponse(response, INVALID_TOKEN.getCode(), INVALID_TOKEN.getMessage()); | ||
} | ||
} else { | ||
setResponse(response, UNAUTHORIZED.getCode(), UNAUTHORIZED.getMessage()); | ||
} | ||
} | ||
|
||
// 발생한 예외에 맞게 status를 설정하고 message를 반환 | ||
private void setResponse(HttpServletResponse response, int code, String message) throws IOException { | ||
response.setStatus(code); | ||
response.setContentType("application/json"); | ||
response.setCharacterEncoding("UTF-8"); | ||
|
||
String json = new ObjectMapper().writeValueAsString(ResponseDto.errorResponse(code, message)); | ||
response.getWriter().write(json); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.