Skip to content

Commit

Permalink
replace CORSFilter implementation by modernn CorsRegistry configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisvang committed Nov 12, 2024
1 parent d0ca93f commit 8ac2170
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 72 deletions.
68 changes: 0 additions & 68 deletions src/main/java/org/fairdatapoint/api/filter/CORSFilter.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@ public class FilterConfigurer extends

private final JwtTokenFilter jwtTokenFilter;

private final CORSFilter corsFilter;

private final LoggingFilter loggingFilter;

@Override
public void configure(HttpSecurity http) {
http.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class);
http.addFilterBefore(corsFilter, JwtTokenFilter.class);
http.addFilterBefore(loggingFilter, CORSFilter.class);
http.addFilterBefore(loggingFilter, JwtTokenFilter.class);
}

}
18 changes: 18 additions & 0 deletions src/main/java/org/fairdatapoint/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

Expand Down Expand Up @@ -85,4 +87,20 @@ public ObjectMapper objectMapper() {
public InternalResourceViewResolver defaultViewResolver() {
return new InternalResourceViewResolver();
}

// https://docs.spring.io/spring-framework/reference/web/webmvc-cors.html
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE") // todo: what about OPTIONS?
.allowedHeaders(
HttpHeaders.ORIGIN,
HttpHeaders.AUTHORIZATION,
HttpHeaders.ACCEPT,
HttpHeaders.CONTENT_TYPE
)
.exposedHeaders(HttpHeaders.LOCATION, HttpHeaders.LINK);
}

}

0 comments on commit 8ac2170

Please sign in to comment.