maj websocket & maj security

This commit is contained in:
pedro 2025-06-03 13:12:39 +02:00
parent 74daf728dd
commit b767a80963
10 changed files with 24 additions and 112 deletions

View file

@ -2,8 +2,10 @@ package fr.teamflash.fencerjudgeback
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication import org.springframework.boot.runApplication
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
@SpringBootApplication @SpringBootApplication
@EnableGlobalMethodSecurity(prePostEnabled = true)
class FencerJudgeBackApplication class FencerJudgeBackApplication
fun main(args: Array<String>) { fun main(args: Array<String>) {

View file

@ -12,10 +12,6 @@ import java.util.Date
@RequestMapping("/matches") @RequestMapping("/matches")
class MatchRestController(private val matchService: MatchService) { class MatchRestController(private val matchService: MatchService) {
companion object {
const val URL_ORIGIN: String = "http://localhost:*"
}
// Lister tous les matchs // Lister tous les matchs
@GetMapping("/") @GetMapping("/")
fun getAll(): ResponseEntity<List<MatchBean>?> { fun getAll(): ResponseEntity<List<MatchBean>?> {

View file

@ -5,6 +5,7 @@ import fr.teamflash.fencerjudgeback.repositories.MatchRepository
import fr.teamflash.fencerjudgeback.websocket.controllers.MatchWebSocketController import fr.teamflash.fencerjudgeback.websocket.controllers.MatchWebSocketController
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy import org.springframework.context.annotation.Lazy
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Service @Service
@ -37,6 +38,7 @@ class MatchService(
} }
// Ajouter un match (admin) // Ajouter un match (admin)
@PreAuthorize("hasRole('ADMIN')")
fun createMatch(newMatch: MatchBean): MatchBean { fun createMatch(newMatch: MatchBean): MatchBean {
println("MatchService.createMatch : $newMatch") println("MatchService.createMatch : $newMatch")
val savedMatch = matchRepository.save(newMatch) val savedMatch = matchRepository.save(newMatch)
@ -47,6 +49,7 @@ class MatchService(
} }
// Modifier un match (admin) // Modifier un match (admin)
@PreAuthorize("hasRole('ADMIN')")
fun updateMatch(id: Long?, newMatch: MatchBean): Int { fun updateMatch(id: Long?, newMatch: MatchBean): Int {
println("MatchService.updateMatch : $newMatch") println("MatchService.updateMatch : $newMatch")
@ -62,6 +65,7 @@ class MatchService(
} }
// Supprimer un match (admin) // Supprimer un match (admin)
@PreAuthorize("hasRole('ADMIN')")
fun deleteMatchById(id: Long?) : Int? { fun deleteMatchById(id: Long?) : Int? {
println("MatchService.deleteMatchById : $id") println("MatchService.deleteMatchById : $id")
@ -91,8 +95,4 @@ class MatchService(
return matchRepository.findAll() return matchRepository.findAll()
.filter { it.country == country } .filter { it.country == country }
} }
fun addMatch(match:MatchBean) {
matchRepository.save(match)
}
} }

View file

@ -6,6 +6,7 @@ import fr.teamflash.fencerjudgeback.websocket.controllers.PlayerWebSocketControl
import fr.teamflash.fencerjudgeback.websocket.models.PlayerUpdateMessage import fr.teamflash.fencerjudgeback.websocket.models.PlayerUpdateMessage
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy import org.springframework.context.annotation.Lazy
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Service @Service
@ -49,6 +50,7 @@ class PlayerService(
} }
// Ajouter un joueur (admin) // Ajouter un joueur (admin)
// @PreAuthorize("hasRole('ADMIN')")
fun createPlayer(player: PlayerBean) : PlayerBean { fun createPlayer(player: PlayerBean) : PlayerBean {
println("PlayerService.createPlayer : $player") println("PlayerService.createPlayer : $player")
val savedPlayer = playerRepository.save(player) val savedPlayer = playerRepository.save(player)
@ -58,6 +60,7 @@ class PlayerService(
} }
// Modifier un joueur (admin) // Modifier un joueur (admin)
@PreAuthorize("hasRole('ADMIN')")
fun updatePlayer(id:Long, newPlayer: PlayerBean) : Int { fun updatePlayer(id:Long, newPlayer: PlayerBean) : Int {
// Vérifier si le joueur existe à l'id renseigné // Vérifier si le joueur existe à l'id renseigné
if (getById(id) == null) { if (getById(id) == null) {
@ -74,6 +77,7 @@ class PlayerService(
} }
// Supprimer un joueur (admin) // Supprimer un joueur (admin)
@PreAuthorize("hasRole('ADMIN')")
fun deletePlayerById(id:Long?): Int { fun deletePlayerById(id:Long?): Int {
println("PlayerService.deletePlayer : $id") println("PlayerService.deletePlayer : $id")
@ -89,8 +93,4 @@ class PlayerService(
playerRepository.deleteById(id!!) playerRepository.deleteById(id!!)
return 1 return 1
} }
fun addPlayer(player:PlayerBean) {
playerRepository.save(player)
}
} }

View file

@ -8,6 +8,7 @@ import fr.teamflash.fencerjudgeback.websocket.controllers.RefereeWebSocketContro
import fr.teamflash.fencerjudgeback.websocket.models.RefereeUpdateMessage import fr.teamflash.fencerjudgeback.websocket.models.RefereeUpdateMessage
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy import org.springframework.context.annotation.Lazy
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
@Service @Service
@ -51,6 +52,7 @@ class RefereeService(
} }
// Ajouter un arbitre (admin) // Ajouter un arbitre (admin)
@PreAuthorize("hasRole('ADMIN')")
fun createReferee(referee: RefereeBean) : RefereeBean { fun createReferee(referee: RefereeBean) : RefereeBean {
println("RefereeService.createReferee : $referee") println("RefereeService.createReferee : $referee")
val savedReferee = refereeRepository.save(referee) val savedReferee = refereeRepository.save(referee)
@ -60,6 +62,7 @@ class RefereeService(
} }
// Modifier un arbitre (admin) // Modifier un arbitre (admin)
@PreAuthorize("hasRole('ADMIN')")
fun updateReferee(id:Long, newReferee: RefereeBean) : Int? { fun updateReferee(id:Long, newReferee: RefereeBean) : Int? {
// Vérifier si l'arbitre existe à l'id renseigné // Vérifier si l'arbitre existe à l'id renseigné
if (getById(id) == null) { if (getById(id) == null) {
@ -74,6 +77,7 @@ class RefereeService(
} }
// Supprimer un arbitre (admin) // Supprimer un arbitre (admin)
@PreAuthorize("hasRole('ADMIN')")
fun deleteRefereeById(id:Long): Int { fun deleteRefereeById(id:Long): Int {
println("RefereeService.deleteReferee : $id") println("RefereeService.deleteReferee : $id")

View file

@ -109,7 +109,7 @@ class MatchWebSocketController(
@MessageMapping("/add") @MessageMapping("/add")
fun addMatchtoMainList(match:MatchBean) { fun addMatchtoMainList(match:MatchBean) {
match.id = mainId; match.id = mainId;
matchService.addMatch(match) matchService.createMatch(match)
messageHistory.add(match) messageHistory.add(match)
broadcastMatchUpdate(match, MatchUpdateMessage.UpdateType.NEW_MATCH) broadcastMatchUpdate(match, MatchUpdateMessage.UpdateType.NEW_MATCH)
mainId = mainId?.plus(1) mainId = mainId?.plus(1)

View file

@ -19,8 +19,6 @@ class PlayerWebSocketController(
private val messagingTemplate: SimpMessagingTemplate private val messagingTemplate: SimpMessagingTemplate
) { ) {
private val messageHistory = ArrayList<PlayerBean>()
@MessageMapping("/player.update") @MessageMapping("/player.update")
@SendTo("/topic/player.updates") @SendTo("/topic/player.updates")
fun handlePlayerUpdate(playerUpdateMessage: PlayerUpdateMessage): PlayerUpdateMessage { fun handlePlayerUpdate(playerUpdateMessage: PlayerUpdateMessage): PlayerUpdateMessage {
@ -65,7 +63,7 @@ class PlayerWebSocketController(
@MessageMapping @MessageMapping
fun addPlayerToMainList(playerBean: PlayerBean) { fun addPlayerToMainList(playerBean: PlayerBean) {
playerService.addPlayer(playerBean) playerService.createPlayer(playerBean)
broadcastPlayerUpdate(playerBean, PlayerUpdateMessage.UpdateType.PLAYER_CREATE) broadcastPlayerUpdate(playerBean, PlayerUpdateMessage.UpdateType.PLAYER_CREATE)
} }
} }

View file

@ -62,10 +62,9 @@
function sendMatch() { function sendMatch() {
const match = { const match = {
id: 1, player1ID: parseInt(document.getElementById("player1Id").value),
player1Id: parseInt(document.getElementById("player1Id").value), player2ID: parseInt(document.getElementById("player2Id").value),
player2Id: parseInt(document.getElementById("player2Id").value), refereeID: parseInt(document.getElementById("refereeId").value),
refereeId: parseInt(document.getElementById("refereeId").value),
score1: parseInt(document.getElementById("score1").value), score1: parseInt(document.getElementById("score1").value),
score2: parseInt(document.getElementById("score2").value), score2: parseInt(document.getElementById("score2").value),
date: document.getElementById("matchDate").value date: document.getElementById("matchDate").value

View file

@ -1,87 +0,0 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Test WebSocket Match</title>
<style>
#messageArea {
width: 100%;
height: 300px;
border: 1px solid #ddd;
overflow-y: auto;
padding: 10px;
margin-bottom: 10px;
font-family: monospace;
}
input, button {
padding: 8px;
margin: 4px;
}
.input-group {
margin-bottom: 10px;
}
</style>
</head>
<body>
<h2>Test WebSocket Match</h2>
<div id="messageArea">Connexion...</div>
<div class="input-group">
<input type="number" id="matchId" placeholder="Match ID">
<input type="number" id="player1Id" placeholder="Joueur 1 ID">
<input type="number" id="player2Id" placeholder="Joueur 2 ID">
<input type="number" id="refereeId" placeholder="Arbitre ID">
</div>
<div class="input-group">
<input type="number" id="score1" placeholder="Score Joueur 1">
<input type="number" id="score2" placeholder="Score Joueur 2">
<input type="datetime-local" id="matchDate">
</div>
<button onclick="sendMatch()">Envoyer Match</button>
<script src="https://cdn.jsdelivr.net/npm/sockjs-client/dist/sockjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
<script>
const stompClient = Stomp.over(new SockJS('/ws/players-app'));
const channel = "/ws/topic/player";
stompClient.connect({}, function () {
stompClient.subscribe(channel, function (message) {
const msg = JSON.parse(message.body);
console.log("Match reçu :", msg);
displayMessage(msg);
});
document.getElementById('messageArea').textContent = 'Connecté au WebSocket';
}, function (error) {
document.getElementById('messageArea').textContent = 'Erreur WebSocket : ' + error;
});
function sendMatch() {
const match = {
matchId: parseInt(document.getElementById("matchId").value),
player1Id: parseInt(document.getElementById("player1Id").value),
player2Id: parseInt(document.getElementById("player2Id").value),
refereeId: parseInt(document.getElementById("refereeId").value),
score1: parseInt(document.getElementById("score1").value),
score2: parseInt(document.getElementById("score2").value),
date: document.getElementById("matchDate").value
};
stompClient.send("/ws/matches/add", {}, JSON.stringify(match));
}
function displayMessage(match) {
const area = document.getElementById("messageArea");
const div = document.createElement("div");
div.textContent = `Match ${match.matchId}: ${match.player1Id} (${match.score1}) vs ${match.player2Id} (${match.score2})`;
area.appendChild(div);
}
</script>
</body>
</html>

View file

@ -1,7 +1,7 @@
### Get all matches ### Get all matches
GET http://localhost:8080/matches/ GET http://localhost:8080/matches/
### Get matches by players --> TODO ### Get matches by players
GET http://localhost:8080/matches/players/1/2 GET http://localhost:8080/matches/players/1/2
### Get matches by date ### Get matches by date
@ -39,7 +39,7 @@ Content-Type: application/json
"score1": 0, "score1": 0,
"score2": 0, "score2": 0,
"date": "2025-06-02", "date": "2025-06-02",
"state": "NOT STARTED" "state": 0
} }
### Update match ### Update match
@ -57,7 +57,7 @@ Content-Type: application/json
"score1": 5, "score1": 5,
"score2": 3, "score2": 3,
"date": "2025-06-02", "date": "2025-06-02",
"state": "OVER" "state": 1
} }
### Delete match ### Delete match
@ -85,7 +85,7 @@ Content-Type: application/json
{ {
"name": "Smith", "name": "Smith",
"firstName": "John", "firstName": "John",
"qualification": "NATIONAL" "level": 1
} }
### Update referee ### Update referee
@ -95,7 +95,7 @@ Content-Type: application/json
{ {
"name": "Smith", "name": "Smith",
"firstName": "John", "firstName": "John",
"qualification": "INTERNATIONAL" "level": "INTERNATIONAL"
} }
### Delete referee ### Delete referee