Add: missing create methods in controllers. Fix: SYSTEM token gets the refresh now. Add: CORS. Add: HTTP to core.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>backlib</artifactId>
|
||||
<groupId>net.miarma</groupId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
|
||||
<properties>
|
||||
<java.version>25</java.version>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package net.miarma.backlib.dto;
|
||||
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ApiValidationErrorDto {
|
||||
private Map<String,String> errors;
|
||||
private Instant timestamp;
|
||||
|
||||
public ApiValidationErrorDto(Map<String,String> errors) {
|
||||
this.errors = errors;
|
||||
this.timestamp = Instant.now();
|
||||
}
|
||||
|
||||
public Map<String,String> getErrors() {
|
||||
return errors;
|
||||
}
|
||||
|
||||
public void setErrors(Map<String,String> errors) {
|
||||
this.errors = errors;
|
||||
}
|
||||
|
||||
public Instant getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(Instant timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return new ObjectMapper().writeValueAsString(this);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,21 @@
|
||||
package net.miarma.backlib.exception;
|
||||
|
||||
public class ValidationException extends RuntimeException {
|
||||
public ValidationException(String message) {
|
||||
private final String field;
|
||||
private final String message;
|
||||
|
||||
public ValidationException(String field, String message) {
|
||||
super(message);
|
||||
this.field = field;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@ package net.miarma.backlib.http;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.miarma.backlib.dto.ApiErrorDto;
|
||||
import net.miarma.backlib.dto.ApiValidationErrorDto;
|
||||
import net.miarma.backlib.exception.*;
|
||||
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 java.util.Map;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(NotFoundException.class)
|
||||
@@ -81,17 +84,9 @@ public class GlobalExceptionHandler {
|
||||
}
|
||||
|
||||
@ExceptionHandler(ValidationException.class)
|
||||
public ResponseEntity<ApiErrorDto> handleValidation(
|
||||
ValidationException ex, HttpServletRequest req) {
|
||||
|
||||
ApiErrorDto error = new ApiErrorDto(
|
||||
HttpStatus.UNPROCESSABLE_CONTENT.value(),
|
||||
HttpStatus.UNPROCESSABLE_CONTENT.getReasonPhrase(),
|
||||
ex.getMessage(),
|
||||
req.getRequestURI()
|
||||
);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_CONTENT).body(error);
|
||||
public ResponseEntity<ApiValidationErrorDto> handleValidation(ValidationException ex) {
|
||||
Map<String, String> errors = Map.of(ex.getField(), ex.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_CONTENT).body(new ApiValidationErrorDto(errors));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
|
||||
@@ -2,15 +2,23 @@ package net.miarma.backlib.security;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Component
|
||||
public class CoreAuthTokenHolder {
|
||||
private volatile String token;
|
||||
private volatile Instant expiresAt;
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
public boolean isExpired() {
|
||||
return expiresAt == null || Instant.now().isAfter(expiresAt.minusSeconds(30));
|
||||
}
|
||||
|
||||
public void setToken(String token, Instant expiresAt) {
|
||||
this.token = token;
|
||||
this.expiresAt = expiresAt;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user