11package com .sterul .opencookbookapiserver .configurations .security ;
22
33import java .util .Arrays ;
4+ import java .util .Collections ;
45import java .util .List ;
56
67import org .springframework .beans .factory .annotation .Autowired ;
78import org .springframework .context .annotation .Bean ;
89import org .springframework .context .annotation .Configuration ;
10+ import org .springframework .http .HttpMethod ;
911import org .springframework .security .authentication .AuthenticationManager ;
1012import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
1113import org .springframework .security .config .annotation .method .configuration .EnableMethodSecurity ;
1820import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
1921import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
2022import org .springframework .security .web .util .matcher .RequestMatcher ;
23+ import org .springframework .web .cors .CorsConfiguration ;
2124
2225import com .sterul .opencookbookapiserver .configurations .security .requestfilters .JwtRequestFilter ;
2326
@@ -70,7 +73,7 @@ private RequestMatcher allowedPathRequestMatcher() {
7073 public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
7174
7275 // Cors and csrf not needed in an api server
73- http .cors (configurer -> configurer .disable ( ));
76+ http .cors (configurer -> configurer .configurationSource ( c -> allowAllCorsConfig () ));
7477 http .csrf (conf -> conf .disable ());
7578
7679 // Allow frames needed for h2 console
@@ -93,7 +96,21 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
9396 return http .build ();
9497 }
9598
96- @ Bean
99+ private CorsConfiguration allowAllCorsConfig () {
100+ List <String > permittedCorsMethods = Collections .unmodifiableList (Arrays .asList (
101+ HttpMethod .GET .name (),
102+ HttpMethod .HEAD .name (),
103+ HttpMethod .POST .name (),
104+ HttpMethod .PUT .name (),
105+ HttpMethod .DELETE .name ()));
106+
107+ var corsConfiguration = new CorsConfiguration ().applyPermitDefaultValues ();
108+ corsConfiguration .setAllowedMethods (permittedCorsMethods );
109+ return corsConfiguration ;
110+
111+ }
112+
113+ @ Bean
97114 public AuthenticationManager authenticationManager (HttpSecurity http )
98115 throws Exception {
99116 return http .getSharedObject (AuthenticationManagerBuilder .class )
0 commit comments