Skip to content

Commit

Permalink
fix: cors?
Browse files Browse the repository at this point in the history
  • Loading branch information
BettyB979 committed Dec 19, 2023
1 parent e23448b commit a15d071
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Optional;

@Configuration
@Getter
Expand Down Expand Up @@ -35,8 +34,8 @@ public class ApplicationConfig {
@Value("${fr.insee.datacollectionmanagement.auth.mode}")
private String authType;

@Value("${fr.insee.datacollectionmanagement.cors.allowedOrigin}")
private Optional<String> allowedOrigin;
@Value("${fr.insee.datacollectionmanagement.cors.allowedOrigins}")
private String[] allowedOrigins;

@Value("${fr.insee.datacollectionmanagement.auth.realm}")
private String keycloakRealm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@RequiredArgsConstructor
public class CorsGlobalConfig {

Expand All @@ -19,7 +17,7 @@ public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
String ao = applicationConfig.getAllowedOrigin().orElse("*");
String[] ao = applicationConfig.getAllowedOrigins();
registry.addMapping("/**")
.allowedOrigins(ao)
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -121,6 +122,14 @@ public ResponseEntity<ApiError> notFoundException(NotFoundException e, WebReques
return processException(e, HttpStatus.NOT_FOUND, request);
}

@ExceptionHandler(EntityNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ApiError> entityNotFoundException(NotFoundException e, WebRequest request) {
log.error(e.getMessage(), e);
return processException(e, HttpStatus.NOT_FOUND, request);
}


@ExceptionHandler(NotMatchException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<ApiError> notMatchException(NotMatchException e, WebRequest request) {
Expand Down

0 comments on commit a15d071

Please sign in to comment.