Skip to content

Commit 24d1763

Browse files
committed
allowed cors for localhost:3000 and added configurations in api gateway properties and updated authConfig
1 parent a8e16fc commit 24d1763

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

‎api-gateway/src/main/resources/application.properties‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ server.port=8082
33

44
# 1. Eureka Configuration
55
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
6+
# CORS Configuration for Gateway
7+
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedOrigins=http://localhost:3000
8+
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedMethods=GET,POST,PUT,DELETE,OPTIONS
9+
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowedHeaders=*
10+
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowCredentials=true
611

712
# 2. Event Service Route + Security Filter
813
spring.cloud.gateway.routes[0].id=event-service
Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.streamseat.authservice.config;
22

3-
43
import org.springframework.context.annotation.Bean;
54
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.http.HttpMethod;
66
import org.springframework.security.authentication.AuthenticationManager;
77
import org.springframework.security.authentication.AuthenticationProvider;
88
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
9+
import org.springframework.security.config.Customizer;
910
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
1011
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1112
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -14,40 +15,62 @@
1415
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1516
import org.springframework.security.crypto.password.PasswordEncoder;
1617
import 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
2027
public 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+
}

‎auth-service/src/main/java/com/streamseat/authservice/config/CustomUserDetailsService.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class CustomUserDetailsService implements UserDetailsService {
1717

1818
@Override
1919
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
20+
System.out.println("CustomUserDetailsService called for: " + username);
2021
Optional<UserCredential> credential = repository.findByName(username);
2122
return credential.map(CustomUserDetails::new).orElseThrow(() -> new UsernameNotFoundException("User not found"));
2223
}

0 commit comments

Comments
 (0)