diff --git a/pom.xml b/pom.xml index c28b894..0cb46c6 100644 --- a/pom.xml +++ b/pom.xml @@ -24,22 +24,39 @@ + org.springframework.boot spring-boot-starter-data-mongodb + + org.springframework.boot spring-boot-starter-oauth2-client + + org.springframework.boot spring-boot-starter-web + + org.springframework.boot spring-boot-starter-websocket + + + + org.projectlombok + lombok + 1.18.24 + provided + + + org.springframework.boot spring-boot-starter-test @@ -50,21 +67,11 @@ spring-security-test test - - org.projectlombok - lombok - 1.18.24 - provided - - - org.springframework.data - spring-data-mongodb - - + org.apache.maven.plugins maven-compiler-plugin @@ -74,7 +81,7 @@ - + org.springframework.boot spring-boot-maven-plugin diff --git a/src/main/java/pintudos/game/config/SecurityConfig.java b/src/main/java/pintudos/game/config/SecurityConfig.java index 700ebb8..51f4de4 100644 --- a/src/main/java/pintudos/game/config/SecurityConfig.java +++ b/src/main/java/pintudos/game/config/SecurityConfig.java @@ -8,30 +8,30 @@ @Configuration public class SecurityConfig { - @Bean - public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { - http - .cors() - .and() - .csrf() - .disable() // Necesario para SockJS - .authorizeHttpRequests(authz -> - authz - .requestMatchers( - "/game/**", // SockJS handshake y WebSocket transport - "/ws/**", // Si usas /ws como endpoint de registro STOMP - "/topic/**", // Canal de suscripciones - "/app/**" // Canal de envío desde el cliente - ) - .permitAll() - .anyRequest() - .authenticated() // El resto necesita auth - ) - .formLogin() - .disable() - .httpBasic() - .disable(); + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http + .cors() + .and() + .csrf() + .disable() + .authorizeHttpRequests(authz -> authz + // Permite acceso sin autenticación a los endpoints SockJS + .requestMatchers( + "/game", "/game/**", "/game/info/**" + ).permitAll() + // Archivos públicos + .requestMatchers( + "/", "/login/", "/error", "/css/", "/js/" + ).permitAll() + // Endpoints que requieren autenticación + .requestMatchers( + "/app/**", "/topic/**" + ).authenticated() + .anyRequest().authenticated() + ) + .oauth2Login(); - return http.build(); - } + return http.build(); + } } diff --git a/src/main/java/pintudos/game/config/WebConfig.java b/src/main/java/pintudos/game/config/WebConfig.java index 08f9942..6d09379 100644 --- a/src/main/java/pintudos/game/config/WebConfig.java +++ b/src/main/java/pintudos/game/config/WebConfig.java @@ -14,10 +14,12 @@ public WebMvcConfigurer corsConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry - .addMapping("/**") - .allowedOrigins("http://localhost:5173", "http://localhost:3000") // Especificar frontend - .allowedMethods("*") - .allowedHeaders("*"); // Habilitar credenciales + .addMapping("/**") + .allowedOrigins("http://localhost:5173", "http://localhost:3000") + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*") + .exposedHeaders("Set-Cookie", "Authorization") + .allowCredentials(true); } }; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 438f7c9..5f6db6e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,13 @@ +# Nombre de la aplicación y conexión a Mongo (ya existente) spring.application.name=game spring.data.mongodb.uri=mongodb+srv://diego:marzo245@universidad.lavtxfi.mongodb.net/pintudos?retryWrites=true&w=majority + +# Configuración de autenticación con Google +spring.security.oauth2.client.registration.google.client-id=186447712086-73urgfm1lll5069lh18ed9venvnsr5an.apps.googleusercontent.com +spring.security.oauth2.client.registration.google.client-secret=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 +spring.security.oauth2.client.registration.google.scope=profile,email + +spring.security.oauth2.client.provider.google.authorization-uri=https://accounts.google.com/o/oauth2/auth +spring.security.oauth2.client.provider.google.token-uri=https://oauth2.googleapis.com/token +spring.security.oauth2.client.provider.google.user-info-uri=https://www.googleapis.com/oauth2/v3/userinfo +spring.security.oauth2.client.provider.google.user-name-attribute=sub