diff --git a/FencerJudgeBack/build.gradle.kts b/FencerJudgeBack/build.gradle.kts index f64df6d..e72e48d 100644 --- a/FencerJudgeBack/build.gradle.kts +++ b/FencerJudgeBack/build.gradle.kts @@ -33,7 +33,7 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-websocket") // SECURITY -// implementation("org.springframework.boot:spring-boot-starter-security") + implementation("org.springframework.boot:spring-boot-starter-security") // H2 Database runtimeOnly("com.h2database:h2") diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/ConfigSecurity.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/ConfigSecurity.kt new file mode 100644 index 0000000..373883d --- /dev/null +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/ConfigSecurity.kt @@ -0,0 +1,56 @@ +package fr.teamflash.fencerjudgeback.config + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder +import org.springframework.security.config.annotation.web.builders.HttpSecurity +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity +import org.springframework.security.core.userdetails.User +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder +import org.springframework.security.provisioning.InMemoryUserDetailsManager +import org.springframework.security.web.SecurityFilterChain + +@Configuration +@EnableWebSecurity +class SecurityConfig { + + @Autowired + open fun configureGlobal(auth: AuthenticationManagerBuilder) { + val encoder = BCryptPasswordEncoder() + + //Créer des utilisateurs fixes + auth.inMemoryAuthentication() + .passwordEncoder(encoder) + .withUser("aaa") + .password(encoder.encode("bbb")) + .roles("USER") + .and() + .withUser("Admin") + .password(encoder.encode("Admin")) + .roles("ADMIN") + } + + @Bean + open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { + http.authorizeHttpRequests { authorize -> + authorize.requestMatchers("/ws/*").authenticated() + .anyRequest().permitAll() + } + .httpBasic { } + .formLogin { } + .csrf { it.disable() } + return http.build() + } + + @Bean + fun userDetailsService(): UserDetailsService { + val user = User.withUsername("user") + .password("{noop}password") // {noop} = pas de hash pour dev + .roles("USER") + .build() + + return InMemoryUserDetailsManager(user) + } +} diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt deleted file mode 100644 index 012e601..0000000 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/PlayerWebSocketConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package fr.teamflash.fencerjudgeback.config - -import org.springframework.context.annotation.Configuration -import org.springframework.messaging.simp.config.MessageBrokerRegistry -import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker -import org.springframework.web.socket.config.annotation.StompEndpointRegistry -import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer - -const val CHANNEL_PLAYER_NAME: String = "/ws/topic/match" - -@Configuration -@EnableWebSocketMessageBroker -open class PlayerWebSocketConfig : WebSocketMessageBrokerConfigurer { - - override fun configureMessageBroker(registry: MessageBrokerRegistry) { - // Enable a simple memory-based message broker to send messages to clients - // Prefix for messages FROM server TO client - registry.enableSimpleBroker(CHANNEL_PLAYER_NAME) - - // Prefix for messages FROM client TO server - registry.setApplicationDestinationPrefixes("/ws") - } - - override fun registerStompEndpoints(registry: StompEndpointRegistry) { - // Register the "/ws" endpoint, enabling SockJS fallback options - registry.addEndpoint("/ws/players-app") - .setAllowedOriginPatterns("*") // Allow connections from any origin (adjust for production) - .withSockJS() - } -} \ No newline at end of file diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/RefereeWebSocketConfig.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/RefereeWebSocketConfig.kt deleted file mode 100644 index 9d4cf59..0000000 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/RefereeWebSocketConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package fr.teamflash.fencerjudgeback.config - -import org.springframework.context.annotation.Configuration -import org.springframework.messaging.simp.config.MessageBrokerRegistry -import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker -import org.springframework.web.socket.config.annotation.StompEndpointRegistry -import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer - -const val CHANNEL_REFEREE_NAME: String = "/ws/topic/referee" - -@Configuration -@EnableWebSocketMessageBroker -open class RefereeWebSocketConfig : WebSocketMessageBrokerConfigurer { - - override fun configureMessageBroker(registry: MessageBrokerRegistry) { - // Enable a simple memory-based message broker to send messages to clients - // Prefix for messages FROM server TO client - registry.enableSimpleBroker(CHANNEL_REFEREE_NAME) - - // Prefix for messages FROM client TO server - registry.setApplicationDestinationPrefixes("/ws") - } - - override fun registerStompEndpoints(registry: StompEndpointRegistry) { - // Register the "/ws" endpoint, enabling SockJS fallback options - registry.addEndpoint("/ws/referees-app") - .setAllowedOriginPatterns("*") // Allow connections from any origin (adjust for production) - .withSockJS() - } -} \ No newline at end of file diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/MatchWebSocketConfig.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/WebSocketConfig.kt similarity index 89% rename from FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/MatchWebSocketConfig.kt rename to FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/WebSocketConfig.kt index 5138d75..ec13cd2 100644 --- a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/MatchWebSocketConfig.kt +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/config/WebSocketConfig.kt @@ -5,6 +5,7 @@ import org.springframework.messaging.simp.config.MessageBrokerRegistry import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker import org.springframework.web.socket.config.annotation.StompEndpointRegistry import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer +import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor const val CHANNEL_MATCH_NAME: String = "/ws/topic/match" @@ -24,6 +25,7 @@ open class MatchWebSocketConfig : WebSocketMessageBrokerConfigurer { override fun registerStompEndpoints(registry: StompEndpointRegistry) { // Register the "/ws" endpoint, enabling SockJS fallback options registry.addEndpoint("/ws/matches-app") + .addInterceptors(HttpSessionHandshakeInterceptor()) .setAllowedOriginPatterns("*") // Allow connections from any origin (adjust for production) .withSockJS() } diff --git a/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/restControllers/MainController.kt b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/restControllers/MainController.kt new file mode 100644 index 0000000..5dbfd7a --- /dev/null +++ b/FencerJudgeBack/src/main/kotlin/fr/teamflash/fencerjudgeback/restControllers/MainController.kt @@ -0,0 +1,12 @@ +package fr.teamflash.fencerjudgeback.restControllers + +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +class MainController { + @GetMapping("/") + fun main() { + return print("Hello") + } +} \ No newline at end of file