diff --git a/src/main/java/com/example/fercoganbackend/component/ContadorWebSocketHandler.java b/src/main/java/com/example/fercoganbackend/component/ContadorWebSocketHandler.java index f4413f5..dbeaaba 100644 --- a/src/main/java/com/example/fercoganbackend/component/ContadorWebSocketHandler.java +++ b/src/main/java/com/example/fercoganbackend/component/ContadorWebSocketHandler.java @@ -107,19 +107,19 @@ public class ContadorWebSocketHandler extends TextWebSocketHandler { } private String extraerRoomKey(WebSocketSession session) { - String path = session.getUri().getPath(); + String path = session.getUri().getPath(); // /ws/puja/2/3 String[] segments = path.split("/"); - // Para /ws/remate1/lote1 los segmentos son: ["", "ws", "remate1", "lote1"] - if (segments.length >= 4) { - String remate = segments[2]; // índice 2 = remate1 - String lote = segments[3]; // índice 3 = lote1 + // segments: ["", "ws", "puja", "2", "3"] + if (segments.length >= 5) { + String remate = segments[3]; // índice 3 = 2 + String lote = segments[4]; // índice 4 = 3 return generarRoomKey(remate, lote); } - return "default-default"; } + private String generarRoomKey(String remate, String lote) { return remate + "-" + lote; } diff --git a/src/main/java/com/example/fercoganbackend/configuration/SecurityConfig.java b/src/main/java/com/example/fercoganbackend/configuration/SecurityConfig.java index 92ff392..a0554c8 100644 --- a/src/main/java/com/example/fercoganbackend/configuration/SecurityConfig.java +++ b/src/main/java/com/example/fercoganbackend/configuration/SecurityConfig.java @@ -5,6 +5,7 @@ import jakarta.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; @@ -32,32 +33,24 @@ public class SecurityConfig { return authConfig.getAuthenticationManager(); } - @Bean - public WebSecurityCustomizer webSecurityCustomizer() { - return (web) -> web.ignoring().requestMatchers("/ws/**"); - } - @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http + .securityMatcher("/**") .csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests(auth -> auth + .requestMatchers("/ws/**").permitAll() // WebSocket .requestMatchers("/auth/**").permitAll() - .requestMatchers("/favicon.ico", "/error", "/static/**","/contador/**", "/api/**").permitAll() - .requestMatchers("/ws/**").permitAll() // permitir WS sin auth - .requestMatchers("/admin/**").hasAnyAuthority("SUPER_USUARIO","ADMIN") + .requestMatchers("/favicon.ico", "/error", "/static/**", "/contador/**", "/api/**").permitAll() .anyRequest().authenticated() ) - .httpBasic(httpBasic -> httpBasic - .authenticationEntryPoint((request, response, authException) -> { - // Log del fallo de autenticación - System.out.println("Fallo de autenticación: " + authException.getMessage()); - response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage()); - }) - ); + .httpBasic(Customizer.withDefaults()); + return http.build(); } + } diff --git a/src/main/java/com/example/fercoganbackend/configuration/WebSocketConfig.java b/src/main/java/com/example/fercoganbackend/configuration/WebSocketConfig.java index b47b5a2..e9abc2e 100644 --- a/src/main/java/com/example/fercoganbackend/configuration/WebSocketConfig.java +++ b/src/main/java/com/example/fercoganbackend/configuration/WebSocketConfig.java @@ -21,7 +21,7 @@ public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { - registry.addHandler(webSocketHandler, "/ws/{remate}/{lote}") + registry.addHandler(webSocketHandler, "/ws/puja/{remate}/{lote}") .setAllowedOrigins("*"); registry.addHandler(eventoWebSocketHandler, "/ws/eventos/{remate}") .setAllowedOrigins("*"); diff --git a/src/main/java/com/example/fercoganbackend/controller/ContadorController.java b/src/main/java/com/example/fercoganbackend/controller/ContadorController.java index 9b8d4d1..c082492 100644 --- a/src/main/java/com/example/fercoganbackend/controller/ContadorController.java +++ b/src/main/java/com/example/fercoganbackend/controller/ContadorController.java @@ -1,6 +1,7 @@ package com.example.fercoganbackend.controller; import com.example.fercoganbackend.component.ContadorWebSocketHandler; +import com.example.fercoganbackend.dto.CorregirRequest; import com.example.fercoganbackend.service.ContadorService; import org.springframework.web.bind.annotation.*; @@ -44,4 +45,21 @@ public class ContadorController { webSocketHandler.broadcastToRoom(remate, lote, String.valueOf(valor)); return valor; } + + @PostMapping("/corregir/{remate}/{lote}") + public int corregirValor( + @PathVariable String remate, + @PathVariable String lote, + @RequestBody CorregirRequest request) { + + String roomKey = remate + "-" + lote; + int nuevoValor = request.getNuevoValor(); + + contadorService.setContador(roomKey, nuevoValor); + webSocketHandler.broadcastToRoom(remate, lote, String.valueOf(nuevoValor)); + + return nuevoValor; + } + + } \ No newline at end of file diff --git a/src/main/java/com/example/fercoganbackend/dto/CorregirRequest.java b/src/main/java/com/example/fercoganbackend/dto/CorregirRequest.java new file mode 100644 index 0000000..585af8d --- /dev/null +++ b/src/main/java/com/example/fercoganbackend/dto/CorregirRequest.java @@ -0,0 +1,8 @@ +package com.example.fercoganbackend.dto; + +public class CorregirRequest { + private int nuevoValor; + + public int getNuevoValor() { return nuevoValor; } + public void setNuevoValor(int nuevoValor) { this.nuevoValor = nuevoValor; } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 03fcd84..1b1d6d5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ spring.application.name=fercoganbackend -spring.profiles.active=prod +spring.profiles.active=dev spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update