Skip to content

Commit

Permalink
Merge pull request #5 from ho0010/dev
Browse files Browse the repository at this point in the history
Feat: setting
  • Loading branch information
ho0010 authored Jul 11, 2024
2 parents 272446e + dd98aaa commit f96a5c1
Show file tree
Hide file tree
Showing 30 changed files with 759 additions and 17 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// JWT
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2', 'io.jsonwebtoken:jjwt-jackson:0.11.2'

}

tasks.named('test') {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/store/itpick/backend/BackendApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

@SpringBootApplication
public class BackendApplication {
// cicd test 3


public static void main(String[] args) {
SpringApplication.run(BackendApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package store.itpick.backend.common.argument_resolver;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import java.util.Arrays;

@Slf4j
@Component
public class JwtAuthHandlerArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
boolean hasAnnotation = parameter.hasParameterAnnotation(PreAuthorize.class);
log.info(Arrays.toString(parameter.getParameterAnnotations()));
boolean hasType = long.class.isAssignableFrom(parameter.getParameterType());
log.info("hasAnnotation={}, hasType={}, hasAnnotation && hasType={}", hasAnnotation, hasType, hasAnnotation&&hasType);
return hasAnnotation && hasType;
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
log.info("userId={}", request.getAttribute("userId"));
return request.getAttribute("userId");

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package store.itpick.backend.common.argument_resolver;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface PreAuthorize {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class BadRequestException extends RuntimeException {

private final ResponseStatus exceptionStatus;

public BadRequestException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class DatabaseException extends RuntimeException{

private final ResponseStatus exceptionStatus;

public DatabaseException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class InternalServerErrorException extends RuntimeException {

private final ResponseStatus exceptionStatus;

public InternalServerErrorException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception.jwt.bad_request;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class JwtBadRequestException extends RuntimeException {

private final ResponseStatus exceptionStatus;

public JwtBadRequestException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception.jwt.bad_request;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class JwtNoTokenException extends JwtBadRequestException {

private final ResponseStatus exceptionStatus;

public JwtNoTokenException(ResponseStatus exceptionStatus) {
super(exceptionStatus);
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception.jwt.bad_request;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class JwtUnsupportedTokenException extends JwtBadRequestException {

private final ResponseStatus exceptionStatus;

public JwtUnsupportedTokenException(ResponseStatus exceptionStatus) {
super(exceptionStatus);
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception.jwt.unauthorized;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class JwtExpiredTokenException extends JwtUnauthorizedTokenException {

private final ResponseStatus exceptionStatus;

public JwtExpiredTokenException(ResponseStatus exceptionStatus) {
super(exceptionStatus);
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception.jwt.unauthorized;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class JwtInvalidTokenException extends JwtUnauthorizedTokenException {

private final ResponseStatus exceptionStatus;

public JwtInvalidTokenException(ResponseStatus exceptionStatus) {
super(exceptionStatus);
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception.jwt.unauthorized;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class JwtMalformedTokenException extends JwtUnauthorizedTokenException {

private final ResponseStatus exceptionStatus;

public JwtMalformedTokenException(ResponseStatus exceptionStatus) {
super(exceptionStatus);
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package store.itpick.backend.common.exception.jwt.unauthorized;

import kuit3.backend.common.response.status.ResponseStatus;
import lombok.Getter;

@Getter
public class JwtUnauthorizedTokenException extends RuntimeException {

private final ResponseStatus exceptionStatus;

public JwtUnauthorizedTokenException(ResponseStatus exceptionStatus) {
super(exceptionStatus.getMessage());
this.exceptionStatus = exceptionStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package store.itpick.backend.common.exception_handler;

import jakarta.validation.ConstraintViolationException;
import kuit3.backend.common.exception.BadRequestException;
import kuit3.backend.common.exception.InternalServerErrorException;
import kuit3.backend.common.response.BaseErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import static kuit3.backend.common.response.status.BaseExceptionResponseStatus.*;

@Slf4j
@RestControllerAdvice
public class BaseExceptionControllerAdvice {

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({BadRequestException.class, NoHandlerFoundException.class, TypeMismatchException.class})
public BaseErrorResponse handle_BadRequest(Exception e) {
log.error("[handle_BadRequest]", e);
return new BaseErrorResponse(URL_NOT_FOUND);
}

// 위와 동일 (return ResponseEntity<>)
/*
@ExceptionHandler({BadRequestException.class, NoHandlerFoundException.class, TypeMismatchException.class, HttpRequestMethodNotSupportedException.class})
public ResponseEntity<BaseErrorResponse> handle_BadRequest(BadRequestException e) {
log.error("[handle_BadRequest]", e);
return ResponseEntity.badRequest().body(new BaseErrorResponse(e.getExceptionStatus()));
}
*/

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public BaseErrorResponse handle_HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
log.error("[handle_HttpRequestMethodNotSupportedException]", e);
return new BaseErrorResponse(METHOD_NOT_ALLOWED);
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public BaseErrorResponse handle_ConstraintViolationException(ConstraintViolationException e) {
log.error("[handle_ConstraintViolationException]", e);
return new BaseErrorResponse(BAD_REQUEST, e.getMessage());
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(InternalServerErrorException.class)
public BaseErrorResponse handle_InternalServerError(InternalServerErrorException e) {
log.error("[handle_InternalServerError]", e);
return new BaseErrorResponse(e.getExceptionStatus());
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(RuntimeException.class)
public BaseErrorResponse handle_RuntimeException(Exception e) {
log.error("[handle_RuntimeException]", e);
return new BaseErrorResponse(SERVER_ERROR);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package store.itpick.backend.common.exception_handler;

import jakarta.annotation.Priority;
import kuit3.backend.common.response.BaseErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static kuit3.backend.common.response.status.BaseExceptionResponseStatus.BAD_SQL_GRAMMAR;
import static kuit3.backend.common.response.status.BaseExceptionResponseStatus.DATABASE_ERROR;

@Slf4j
@Priority(0)
@RestControllerAdvice
public class DatabaseExceptionControllerAdvice {

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(BadSqlGrammarException.class)
public BaseErrorResponse handle_BadSqlGrammarException(BadSqlGrammarException e) {
log.error("[handle_BadSqlGrammarException]", e);
return new BaseErrorResponse(BAD_SQL_GRAMMAR);
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(DataAccessException.class)
public BaseErrorResponse handle_DataAccessException(DataAccessException e) {
log.error("[handle_DataAccessException]", e);
return new BaseErrorResponse(DATABASE_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package store.itpick.backend.common.exception_handler;

import jakarta.annotation.Priority;
import kuit3.backend.common.exception.jwt.bad_request.JwtBadRequestException;
import kuit3.backend.common.exception.jwt.unauthorized.JwtUnauthorizedTokenException;
import kuit3.backend.common.response.BaseErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@Priority(0)
@RestControllerAdvice
public class JwtExceptionControllerAdvice {

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(JwtBadRequestException.class)
public BaseErrorResponse handle_JwtBadRequestException(JwtBadRequestException e) {
log.error("[handle_JwtBadRequestException]", e);
return new BaseErrorResponse(e.getExceptionStatus());
}

@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(JwtUnauthorizedTokenException.class)
public BaseErrorResponse handle_JwtUnauthorizedException(JwtUnauthorizedTokenException e) {
log.error("[handle_JwtUnauthorizedException]", e);
return new BaseErrorResponse(e.getExceptionStatus());
}
}
Loading

0 comments on commit f96a5c1

Please sign in to comment.