update security module

This commit is contained in:
2024-09-10 17:21:09 +10:00
parent 41102b7453
commit cefddb61e1
10 changed files with 260 additions and 1 deletions

View File

@@ -0,0 +1,12 @@
package sydney.cheng.microservice.commons.exceptions.feign;
import lombok.Builder;
import lombok.Getter;
import java.util.Map;
@Builder
@Getter
public class FeignBadRequestException extends RuntimeException {
private Map<String, String> errors;
}

View File

@@ -0,0 +1,18 @@
package sydney.cheng.microservice.commons.exceptions.feign;
import lombok.Builder;
import lombok.Getter;
import org.springframework.http.HttpStatus;
@Builder
@Getter
public class FeignErrorException extends RuntimeException {
private final String message;
private final HttpStatus httpStatus;
public FeignErrorException(String message, HttpStatus httpStatus) {
super(message);
this.message = message;
this.httpStatus = httpStatus;
}
}

View File

@@ -0,0 +1,22 @@
package sydney.cheng.microservice.commons.exceptions.handlers;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import sydney.cheng.microservice.commons.exceptions.auth.WrongCredentialsException;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class AuthExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(WrongCredentialsException.class)
public ResponseEntity<?> usernameOrPasswordInvalidException(WrongCredentialsException exception) {
Map<String, String> errors = new HashMap<>();
errors.put("error", exception.getMessage());
return new ResponseEntity<>(errors, HttpStatus.UNAUTHORIZED);
}
}

View File

@@ -0,0 +1,26 @@
package sydney.cheng.microservice.commons.exceptions.handlers;
import sydney.cheng.microservice.commons.exceptions.feign.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class FeignExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(FeignErrorException.class)
public ResponseEntity<?> genericError(FeignErrorException exception) {
Map<String, String> errors = new HashMap<>();
errors.put("error", exception.getMessage());
return new ResponseEntity<>(errors, exception.getHttpStatus());
}
@ExceptionHandler(FeignBadRequestException.class)
public ResponseEntity<?> validationException(FeignBadRequestException exception) {
return ResponseEntity.badRequest().body(exception.getErrors());
}
}

View File

@@ -0,0 +1,21 @@
package sydney.cheng.microservice.commons.exceptions.handlers;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
public class GenericExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public final ResponseEntity<?> handleAllException(Exception ex) {
Map<String, String> errors = new HashMap<>();
errors.put("error", ex.getMessage());
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}
}