forked from Kernel360/f1-JDON-Backend
-
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.
Bugfix: OAuth2 서버로부터 유저의 정보가 null로 들어왔을 때 에러처리하기 (Kernel360#172)
* Feature: Add exception handling in security-filter logic * Fix: api-에러코드를 사용하는 패키지로 이동 * Refactor: securityConfig에 구현했던 handler 분리 * Fix: 변수명 수정 & 필요없는 예외 클래스 삭제
- Loading branch information
Showing
12 changed files
with
208 additions
and
69 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
module-api/src/main/java/kernel/jdon/auth/dto/AuthExceptionResponse.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,22 @@ | ||
package kernel.jdon.auth.dto; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import kernel.jdon.util.DateParserUtil; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class AuthExceptionResponse { | ||
|
||
private String timestamp; | ||
private int status; | ||
private String message; | ||
private String path; | ||
|
||
public AuthExceptionResponse(int status, String message, String path) { | ||
this.timestamp = DateParserUtil.dateTimeToString(LocalDateTime.now()); | ||
this.status = status; | ||
this.message = message; | ||
this.path = path; | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
module-api/src/main/java/kernel/jdon/config/auth/JdonAuthExceptionHandler.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,54 @@ | ||
package kernel.jdon.config.auth; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import kernel.jdon.auth.dto.AuthExceptionResponse; | ||
import kernel.jdon.auth.error.AuthErrorCode; | ||
import kernel.jdon.error.ErrorCode; | ||
import kernel.jdon.global.exception.UnAuthorizedException; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.security.web.authentication.AuthenticationFailureHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
@Component | ||
public class JdonAuthExceptionHandler implements AuthenticationEntryPoint, AccessDeniedHandler, AuthenticationFailureHandler { | ||
|
||
private void throwAuthException(HttpServletResponse response, ErrorCode authErrorCode, String redirectUri) throws | ||
IOException { | ||
AuthExceptionResponse exceptionResponse = new AuthExceptionResponse( | ||
authErrorCode.getHttpStatus().value(), authErrorCode.getMessage(), redirectUri); | ||
String exceptionResponseJson = new ObjectMapper().writeValueAsString(exceptionResponse); | ||
response.setStatus(authErrorCode.getHttpStatus().value()); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setCharacterEncoding("utf-8"); | ||
PrintWriter write = response.getWriter(); | ||
write.write(exceptionResponseJson); | ||
write.flush(); | ||
} | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { | ||
throwAuthException(response, AuthErrorCode.UNAUTHORIZED, "/"); | ||
} | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { | ||
throwAuthException(response, AuthErrorCode.FORBIDDEN, "/"); | ||
} | ||
|
||
@Override | ||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { | ||
if (exception instanceof UnAuthorizedException) { | ||
throwAuthException(response, ((UnAuthorizedException) exception).getErrorCode(), "/"); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
module-api/src/main/java/kernel/jdon/config/auth/JdonOAuth2AuthenticationSuccessHandler.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,53 @@ | ||
package kernel.jdon.config.auth; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import kernel.jdon.auth.dto.JdonOAuth2User; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
import static kernel.jdon.auth.encrypt.AesUtil.encryptAESCBC; | ||
import static kernel.jdon.auth.encrypt.HmacUtil.generateHMAC; | ||
import static kernel.jdon.util.StringUtil.createQueryString; | ||
import static kernel.jdon.util.StringUtil.joinToString; | ||
|
||
@Component | ||
@Slf4j | ||
public class JdonOAuth2AuthenticationSuccessHandler implements AuthenticationSuccessHandler{ | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { | ||
JdonOAuth2User jdonOAuth2User = (JdonOAuth2User)authentication.getPrincipal(); | ||
if (isTemporaryUser(jdonOAuth2User)) { | ||
String query = createUserInfoString(jdonOAuth2User.getEmail(), jdonOAuth2User.getSocialProviderType()); | ||
String encodedQueryString = createEncryptQueryString(query); | ||
response.sendRedirect(joinToString("http://localhost:3000/oauth/info?", encodedQueryString)); | ||
} | ||
} | ||
|
||
private boolean isTemporaryUser(JdonOAuth2User jdonOAuth2User) { | ||
return jdonOAuth2User.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_TEMPORARY_USER")); | ||
} | ||
|
||
private String createUserInfoString(String email, String provider) { | ||
return joinToString(createQueryString("email", email), createQueryString("provider", provider)); | ||
} | ||
|
||
private String createEncryptQueryString(String info) { | ||
String encoded = null; | ||
try { | ||
encoded = encryptAESCBC(info); | ||
encoded = joinToString(createQueryString("value", encoded), | ||
createQueryString("hmac", generateHMAC(encoded))); | ||
} catch (Exception e) { | ||
log.warn(e.getMessage(), e); | ||
} | ||
return encoded; | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
module-api/src/main/java/kernel/jdon/global/exception/UnAuthorizedException.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,15 @@ | ||
package kernel.jdon.global.exception; | ||
|
||
import kernel.jdon.error.ErrorCode; | ||
import lombok.Getter; | ||
import org.springframework.security.core.AuthenticationException; | ||
|
||
@Getter | ||
public class UnAuthorizedException extends AuthenticationException { | ||
private final transient ErrorCode errorCode; | ||
|
||
public UnAuthorizedException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../error/code/api/JobCategoryErrorCode.java → ...bcategory/error/JobCategoryErrorCode.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
2 changes: 1 addition & 1 deletion
2
.../jdon/error/code/api/MemberErrorCode.java → ...el/jdon/member/error/MemberErrorCode.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
2 changes: 1 addition & 1 deletion
2
...l/jdon/error/code/api/SkillErrorCode.java → ...rnel/jdon/skill/error/SkillErrorCode.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
15 changes: 15 additions & 0 deletions
15
module-common/src/main/java/kernel/jdon/util/DateParserUtil.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,15 @@ | ||
package kernel.jdon.util; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class DateParserUtil { | ||
|
||
public static String dateTimeToString(LocalDateTime localDateTime) { | ||
return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | ||
} | ||
} |