11package com .streamseat .authservice .config ;
22
3-
43import org .springframework .context .annotation .Bean ;
54import org .springframework .context .annotation .Configuration ;
5+ import org .springframework .http .HttpMethod ;
66import org .springframework .security .authentication .AuthenticationManager ;
77import org .springframework .security .authentication .AuthenticationProvider ;
88import org .springframework .security .authentication .dao .DaoAuthenticationProvider ;
9+ import org .springframework .security .config .Customizer ;
910import org .springframework .security .config .annotation .authentication .configuration .AuthenticationConfiguration ;
1011import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
1112import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
1415import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
1516import org .springframework .security .crypto .password .PasswordEncoder ;
1617import org .springframework .security .web .SecurityFilterChain ;
18+ import org .springframework .web .cors .CorsConfiguration ;
19+ import org .springframework .web .cors .CorsConfigurationSource ;
20+ import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
21+
22+ import java .util .Arrays ;
23+ import java .util .List ;
1724
1825@ Configuration
1926@ EnableWebSecurity
2027public class AuthConfig {
2128
22- // 1. Password Encoder (used by AuthService to hash passwords)
2329 @ Bean
2430 public PasswordEncoder passwordEncoder () {
2531 return new BCryptPasswordEncoder ();
2632 }
27-
28- // 2. Security Filter Chain (Defines which routes are open)
2933 @ Bean
3034 public SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
31- return http .csrf (AbstractHttpConfigurer ::disable )
35+ return http
36+ // 1. Link the CORS configuration explicitly
37+ .csrf (AbstractHttpConfigurer ::disable )
3238 .authorizeHttpRequests (auth -> auth
39+ // 2. EXPLICITLY permit OPTIONS requests for all paths
40+ .requestMatchers (HttpMethod .OPTIONS , "/**" ).permitAll ()
3341 .requestMatchers ("/auth/register" , "/auth/token" , "/auth/validate" ).permitAll ()
3442 .anyRequest ().authenticated ()
3543 )
3644 .build ();
3745 }
3846
39- // 3. Authentication Manager (Used in AuthController to verify login)
47+ @ Bean
48+ public CorsConfigurationSource corsConfigurationSource () {
49+ CorsConfiguration configuration = new CorsConfiguration ();
50+
51+ // Allow all origins, headers, and methods for development
52+ configuration .setAllowedOriginPatterns (Arrays .asList ("*" ));
53+ configuration .setAllowedMethods (Arrays .asList ("GET" , "POST" , "PUT" , "DELETE" , "OPTIONS" ));
54+ configuration .setAllowedHeaders (Arrays .asList ("*" ));
55+
56+ // MUST be set to true if you ever use credentials/cookies
57+ configuration .setAllowCredentials (true );
58+
59+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
60+ source .registerCorsConfiguration ("/**" , configuration );
61+ return source ;
62+ }
63+
4064 @ Bean
4165 public AuthenticationManager authenticationManager (AuthenticationConfiguration config ) throws Exception {
4266 return config .getAuthenticationManager ();
4367 }
4468
45- // 4. Link with your CustomUserDetailsService
4669 @ Bean
4770 public AuthenticationProvider authenticationProvider (UserDetailsService userDetailsService ) {
4871 DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider ();
4972 authenticationProvider .setUserDetailsService (userDetailsService );
5073 authenticationProvider .setPasswordEncoder (passwordEncoder ());
5174 return authenticationProvider ;
5275 }
53- }
76+ }
0 commit comments