Skip to content

Commit

Permalink
Merge branch 'new-module-authorization-code'
Browse files Browse the repository at this point in the history
  • Loading branch information
patternhelloworld committed Nov 17, 2024
2 parents dc75d3d + 97f43f7 commit ac957a0
Show file tree
Hide file tree
Showing 97 changed files with 6,068 additions and 4,298 deletions.
436 changes: 242 additions & 194 deletions README.md

Large diffs are not rendered by default.

635 changes: 322 additions & 313 deletions client/pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.patternknife.securityhelper.oauth2.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.util.TimeZone;


@SpringBootApplication(scanBasePackages = {"com.patternknife.securityhelper.oauth2.client", "io.github.patternknife.securityhelper.oauth2.api"})
public class SpringSecurityOauth2PasswordJpaImplApplication {

@PostConstruct
void init() {
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
}

public static void main(String[] args) {
SpringApplication.run(SpringSecurityOauth2PasswordJpaImplApplication.class, args);
}

}
package com.patternknife.securityhelper.oauth2.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;
import java.util.TimeZone;


@SpringBootApplication(scanBasePackages = {"com.patternknife.securityhelper.oauth2.client", "io.github.patternknife.securityhelper.oauth2.api"})
public class SpringSecurityOauth2PasswordJpaImplApplication {

@PostConstruct
void init() {
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));
}

public static void main(String[] args) {
SpringApplication.run(SpringSecurityOauth2PasswordJpaImplApplication.class, args);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ResponseErrorLogConfig {
private static final Logger logger = LoggerFactory.getLogger(ResponseErrorLogConfig.class);


@AfterReturning(pointcut = ("within(com.patternknife.securityhelper.oauth2.client.config.response.error..*)"),
@AfterReturning(pointcut = ("within(com.patternknife.securityhelper.oauth2.client.config.response.error..*) || within(io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.handler..*)"),
returning = "returnValue")
public void endpointAfterExceptionReturning(JoinPoint p, Object returnValue) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
package com.patternknife.securityhelper.oauth2.client.config.response.error;


import com.patternknife.securityhelper.oauth2.client.config.response.error.message.GeneralErrorMessage;
import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.util.ExceptionKnifeUtils;
import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.dto.SecurityKnifeErrorResponsePayload;
import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.exception.KnifeOauth2AuthenticationException;
import io.github.patternknife.securityhelper.oauth2.api.config.security.message.DefaultSecurityUserExceptionMessage;
import io.github.patternknife.securityhelper.oauth2.api.config.security.message.ISecurityUserExceptionMessageService;
import io.github.patternknife.securityhelper.oauth2.api.config.security.util.OrderConstants;
import lombok.RequiredArgsConstructor;;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.context.request.WebRequest;


/*
*
* Customize the exception payload by implementing this, which replaces
* 'io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.handler.SecurityKnifeExceptionHandler'
*
* Once you create 'GlobalExceptionHandler', you should insert the following two (authenticationException, authorizationException) as default. Otherwise, 'unhandledExceptionHandler' is prior to 'io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.handler.SecurityKnifeExceptionHandler'.
* "OrderConstants.SECURITY_KNIFE_EXCEPTION_HANDLER_ORDER - 1" means this is prior to "SecurityKnifeExceptionHandler"
* */
@Order(OrderConstants.SECURITY_KNIFE_EXCEPTION_HANDLER_ORDER - 1)
@ControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {

private final ISecurityUserExceptionMessageService iSecurityUserExceptionMessageService;

// 401 : Authentication
@ExceptionHandler({AuthenticationException.class})
public ResponseEntity<?> authenticationException(Exception ex, WebRequest request) {
SecurityKnifeErrorResponsePayload errorResponsePayload;
if(ex instanceof KnifeOauth2AuthenticationException && ((KnifeOauth2AuthenticationException) ex).getErrorMessages() != null) {
errorResponsePayload = new SecurityKnifeErrorResponsePayload(((KnifeOauth2AuthenticationException) ex).getErrorMessages(),
ex, request.getDescription(false), ExceptionKnifeUtils.getAllStackTraces(ex),
ExceptionKnifeUtils.getAllCauses(ex), null);
}else {
errorResponsePayload = new SecurityKnifeErrorResponsePayload(ExceptionKnifeUtils.getAllCauses(ex), request.getDescription(false), iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_LOGIN_FAILURE),
ex.getMessage(), ex.getStackTrace()[0].toString());
}
return new ResponseEntity<>(errorResponsePayload, HttpStatus.UNAUTHORIZED);
}

// 403 : Authorization
@ExceptionHandler({ AccessDeniedException.class })
public ResponseEntity<?> authorizationException(Exception ex, WebRequest request) {
SecurityKnifeErrorResponsePayload errorResponsePayload = new SecurityKnifeErrorResponsePayload(ex.getMessage() != null ? ex.getMessage() : ExceptionKnifeUtils.getAllCauses(ex), request.getDescription(false),
ex.getMessage() == null || ex.getMessage().equals("Access Denied") ? iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHORIZATION_FAILURE) : ex.getMessage(), ex.getStackTrace()[0].toString());
return new ResponseEntity<>(errorResponsePayload, HttpStatus.FORBIDDEN);
}

// Unhandled
@ExceptionHandler(Exception.class)
public ResponseEntity<?> unhandledExceptionHandler(Exception ex, WebRequest request) {
SecurityKnifeErrorResponsePayload errorResponsePayload = new SecurityKnifeErrorResponsePayload(ex.getMessage(), request.getDescription(false), GeneralErrorMessage.UNHANDLED_ERROR.getUserMessage(),
CustomExceptionUtils.getAllStackTraces(ex), CustomExceptionUtils.getAllCauses(ex));
return new ResponseEntity<>(errorResponsePayload, HttpStatus.INTERNAL_SERVER_ERROR);
}

}
package com.patternknife.securityhelper.oauth2.client.config.response.error;


import com.patternknife.securityhelper.oauth2.client.config.response.error.message.GeneralErrorMessage;
import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.util.ExceptionKnifeUtils;
import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.dto.SecurityKnifeErrorResponsePayload;
import io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.exception.KnifeOauth2AuthenticationException;
import io.github.patternknife.securityhelper.oauth2.api.config.security.message.DefaultSecurityUserExceptionMessage;
import io.github.patternknife.securityhelper.oauth2.api.config.security.message.ISecurityUserExceptionMessageService;
import io.github.patternknife.securityhelper.oauth2.api.config.util.OrderConstants;
import lombok.RequiredArgsConstructor;;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.context.request.WebRequest;


/*
*
* Customize the exception payload by implementing this, which replaces
* 'io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.handler.SecurityKnifeExceptionHandler'
*
* Once you create 'GlobalExceptionHandler', you should insert the following two (authenticationException, authorizationException) as default. Otherwise, 'unhandledExceptionHandler' is prior to 'io.github.patternknife.securityhelper.oauth2.api.config.security.response.error.handler.SecurityKnifeExceptionHandler'.
* "OrderConstants.SECURITY_KNIFE_EXCEPTION_HANDLER_ORDER - 1" means this is prior to "SecurityKnifeExceptionHandler"
* */
@Order(OrderConstants.SECURITY_KNIFE_EXCEPTION_HANDLER_ORDER - 1)
@ControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {

private final ISecurityUserExceptionMessageService iSecurityUserExceptionMessageService;

// 401 : Authentication
@ExceptionHandler({AuthenticationException.class})
public ResponseEntity<?> authenticationException(Exception ex, WebRequest request) {
SecurityKnifeErrorResponsePayload errorResponsePayload;
if(ex instanceof KnifeOauth2AuthenticationException && ((KnifeOauth2AuthenticationException) ex).getErrorMessages() != null) {
errorResponsePayload = new SecurityKnifeErrorResponsePayload(((KnifeOauth2AuthenticationException) ex).getErrorMessages(),
ex, request.getDescription(false), ExceptionKnifeUtils.getAllStackTraces(ex),
ExceptionKnifeUtils.getAllCauses(ex), null);
}else {
errorResponsePayload = new SecurityKnifeErrorResponsePayload(ExceptionKnifeUtils.getAllCauses(ex), request.getDescription(false), iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHENTICATION_LOGIN_FAILURE),
ex.getMessage(), ex.getStackTrace()[0].toString());
}
return new ResponseEntity<>(errorResponsePayload, HttpStatus.UNAUTHORIZED);
}

// 403 : Authorization
@ExceptionHandler({ AccessDeniedException.class })
public ResponseEntity<?> authorizationException(Exception ex, WebRequest request) {
SecurityKnifeErrorResponsePayload errorResponsePayload = new SecurityKnifeErrorResponsePayload(ex.getMessage() != null ? ex.getMessage() : ExceptionKnifeUtils.getAllCauses(ex), request.getDescription(false),
ex.getMessage() == null || ex.getMessage().equals("Access Denied") ? iSecurityUserExceptionMessageService.getUserMessage(DefaultSecurityUserExceptionMessage.AUTHORIZATION_FAILURE) : ex.getMessage(), ex.getStackTrace()[0].toString());
return new ResponseEntity<>(errorResponsePayload, HttpStatus.FORBIDDEN);
}

// Unhandled
/* @ExceptionHandler(Exception.class)
public ResponseEntity<?> unhandledExceptionHandler(Exception ex, WebRequest request) {
SecurityKnifeErrorResponsePayload errorResponsePayload = new SecurityKnifeErrorResponsePayload(ex.getMessage(), request.getDescription(false), GeneralErrorMessage.UNHANDLED_ERROR.getUserMessage(),
CustomExceptionUtils.getAllStackTraces(ex), CustomExceptionUtils.getAllCauses(ex));
return new ResponseEntity<>(errorResponsePayload, HttpStatus.INTERNAL_SERVER_ERROR);
}*/

}
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
package com.patternknife.securityhelper.oauth2.client.config.securityimpl.message;

import io.github.patternknife.securityhelper.oauth2.api.config.security.message.ExceptionMessageInterface;

public enum CustomSecurityUserExceptionMessage implements ExceptionMessageInterface {

AUTHENTICATION_LOGIN_FAILURE("1Authentication information is not valid. Please check and try again."),
AUTHENTICATION_LOGIN_ERROR("1An error occurred during authentication. If the problem persists, please contact customer service."),
AUTHENTICATION_TOKEN_FAILURE("1The authentication token has expired. Please log in again."),
AUTHENTICATION_TOKEN_ERROR("1There was a problem verifying the authentication token. Please log in again."),
AUTHORIZATION_FAILURE("1You do not have access permissions. Please request this from the administrator."),
AUTHORIZATION_ERROR("1An error occurred with access permissions. If the problem persists, please contact customer service."),

// ID PASSWORD
AUTHENTICATION_ID_NO_EXISTS("1The specified ID does not exist."),
AUTHENTICATION_WRONG_ID_PASSWORD("1User information could not be verified. Please check your ID or password. If the problem persists, please contact customer service."),
AUTHENTICATION_PASSWORD_FAILED_EXCEEDED("1The number of password attempts has been exceeded."),

// CLIENT ID, SECRET
AUTHENTICATION_WRONG_CLIENT_ID_SECRET("1Client information is not verified."),

// GRANT TYPE
AUTHENTICATION_WRONG_GRANT_TYPE("1Wrong Grant Type detected.");

private String message;

@Override
public String getMessage() {
return message;
}

CustomSecurityUserExceptionMessage(String message) {
this.message = message;
}

}
package com.patternknife.securityhelper.oauth2.client.config.securityimpl.message;

import io.github.patternknife.securityhelper.oauth2.api.config.security.message.ExceptionMessageInterface;

public enum CustomSecurityUserExceptionMessage implements ExceptionMessageInterface {

AUTHENTICATION_LOGIN_FAILURE("1Authentication information is not valid. Please check and try again."),
AUTHENTICATION_LOGIN_ERROR("1An error occurred during authentication. If the problem persists, please contact customer service."),
AUTHENTICATION_TOKEN_FAILURE("1The authentication token has expired. Please log in again."),
AUTHENTICATION_TOKEN_ERROR("1There was a problem verifying the authentication token. Please log in again."),
AUTHORIZATION_FAILURE("1You do not have access permissions. Please request this from the administrator."),
AUTHORIZATION_ERROR("1An error occurred with access permissions. If the problem persists, please contact customer service."),

// ID PASSWORD
AUTHENTICATION_ID_NO_EXISTS("1The specified ID does not exist."),
AUTHENTICATION_WRONG_ID_PASSWORD("1User information could not be verified. Please check your ID or password. If the problem persists, please contact customer service."),
AUTHENTICATION_PASSWORD_FAILED_EXCEEDED("1The number of password attempts has been exceeded."),

// Wrong Authorization Code
AUTHORIZATION_CODE_NO_EXISTS("1The specified Authorization code does not exist."),

// CLIENT ID, SECRET
AUTHENTICATION_WRONG_CLIENT_ID_SECRET("1Client information is not verified."),

// GRANT TYPE
AUTHENTICATION_WRONG_GRANT_TYPE("1Wrong Grant Type detected.");

private String message;

@Override
public String getMessage() {
return message;
}

CustomSecurityUserExceptionMessage(String message) {
this.message = message;
}

}
Loading

0 comments on commit ac957a0

Please sign in to comment.