99import org .springframework .http .HttpMethod ;
1010import org .springframework .http .HttpStatus ;
1111import org .springframework .security .authentication .AuthenticationManager ;
12- import org .springframework .security .config .Customizer ;
1312import org .springframework .security .config .annotation .authentication .configuration .AuthenticationConfiguration ;
1413import org .springframework .security .config .annotation .method .configuration .EnableMethodSecurity ;
1514import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1918import org .springframework .security .web .SecurityFilterChain ;
2019import org .springframework .security .web .authentication .HttpStatusEntryPoint ;
2120import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
21+ import org .springframework .web .cors .CorsConfiguration ;
22+ import org .springframework .web .cors .CorsConfigurationSource ;
23+ import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
24+
25+ import java .util .List ;
2226
2327@ Configuration
2428@ EnableWebSecurity
@@ -63,7 +67,7 @@ public void init() {
6367 @ Bean
6468 public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
6569 http
66- .cors (Customizer . withDefaults ( ))
70+ .cors (cors -> cors . configurationSource ( corsConfigurationSource () ))
6771 .csrf (CsrfConfigurer ::disable )
6872 .sessionManagement (session -> session .sessionCreationPolicy (SessionCreationPolicy .STATELESS ))
6973 .authorizeHttpRequests (
@@ -83,6 +87,21 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
8387 return http .build ();
8488 }
8589
90+ @ Bean
91+ public CorsConfigurationSource corsConfigurationSource () {
92+ CorsConfiguration config = new CorsConfiguration ();
93+ config .setAllowedOriginPatterns (List .of ("http://rentplace.online" , "http://admin.rentplace.online" ));
94+ config .setAllowedMethods (List .of ("GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" ));
95+ config .setAllowedHeaders (List .of ("*" ));
96+ config .setExposedHeaders (List .of ("Authorization" , "Cache-Control" , "Content-Type" , "Set-Cookie" ));
97+ config .setAllowCredentials (true );
98+ config .setMaxAge (3600L );
99+
100+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
101+ source .registerCorsConfiguration ("/**" , config );
102+ return source ;
103+ }
104+
86105 @ Bean
87106 public AuthenticationManager authenticationManager (AuthenticationConfiguration configuration ) throws Exception {
88107 return configuration .getAuthenticationManager ();
0 commit comments