Merge pull request 'Sp Security' (#16) from back-sp-security into feature/all

Reviewed-on: #16
This commit is contained in:
ExostFlash 2025-06-03 08:55:45 +00:00
commit c306eaac28
6 changed files with 71 additions and 61 deletions

View file

@ -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")

View file

@ -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)
}
}

View file

@ -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()
}
}

View file

@ -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()
}
}

View file

@ -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()
}

View file

@ -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")
}
}