From c33fdcdd1e972c1db410e02e14cdaa15229b1734 Mon Sep 17 00:00:00 2001 From: jbion Date: Sat, 17 Dec 2016 22:27:23 +0100 Subject: Add default username for unauthenticated users --- .../org/luxons/sevenwonders/WebSocketConfig.java | 28 ----------------- .../config/AnonymousUsersHandshakeHandler.java | 25 ++++++++++++++++ .../sevenwonders/config/WebSocketConfig.java | 35 ++++++++++++++++++++++ .../sevenwonders/controllers/LobbyController.java | 5 +++- .../java/org/luxons/sevenwonders/game/Player.java | 10 +++++++ 5 files changed, 74 insertions(+), 29 deletions(-) delete mode 100644 src/main/java/org/luxons/sevenwonders/WebSocketConfig.java create mode 100644 src/main/java/org/luxons/sevenwonders/config/AnonymousUsersHandshakeHandler.java create mode 100644 src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java (limited to 'src/main') diff --git a/src/main/java/org/luxons/sevenwonders/WebSocketConfig.java b/src/main/java/org/luxons/sevenwonders/WebSocketConfig.java deleted file mode 100644 index b0613e79..00000000 --- a/src/main/java/org/luxons/sevenwonders/WebSocketConfig.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.luxons.sevenwonders; - -import org.springframework.context.annotation.Configuration; -import org.springframework.messaging.simp.config.MessageBrokerRegistry; -import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; -import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; -import org.springframework.web.socket.config.annotation.StompEndpointRegistry; - -@Configuration -@EnableWebSocketMessageBroker -public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { - - @Override - public void configureMessageBroker(MessageBrokerRegistry config) { - // prefixes for all subscriptions - config.enableSimpleBroker("/queue", "/topic"); - config.setUserDestinationPrefix("/user"); - - // prefix for all calls from clients - config.setApplicationDestinationPrefixes("/app"); - } - - @Override - public void registerStompEndpoints(StompEndpointRegistry registry) { - registry.addEndpoint("/seven-wonders-websocket").withSockJS(); - } - -} \ No newline at end of file diff --git a/src/main/java/org/luxons/sevenwonders/config/AnonymousUsersHandshakeHandler.java b/src/main/java/org/luxons/sevenwonders/config/AnonymousUsersHandshakeHandler.java new file mode 100644 index 00000000..1132197d --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/config/AnonymousUsersHandshakeHandler.java @@ -0,0 +1,25 @@ +package org.luxons.sevenwonders.config; + +import java.security.Principal; +import java.util.Map; + +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.server.support.DefaultHandshakeHandler; + +import sun.security.acl.PrincipalImpl; + +class AnonymousUsersHandshakeHandler extends DefaultHandshakeHandler { + + private int playerId = 0; + + @Override + public Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, + Map attributes) { + Principal p = request.getPrincipal(); + if (p == null) { + p = new PrincipalImpl("player" + playerId++); + } + return p; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java b/src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java new file mode 100644 index 00000000..81c90c9b --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java @@ -0,0 +1,35 @@ +package org.luxons.sevenwonders.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; +import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.server.support.DefaultHandshakeHandler; + +@Configuration +@EnableWebSocketMessageBroker +public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { + + @Override + public void configureMessageBroker(MessageBrokerRegistry config) { + // prefixes for all subscriptions + config.enableSimpleBroker("/queue", "/topic"); + config.setUserDestinationPrefix("/user"); + + // prefix for all calls from clients + config.setApplicationDestinationPrefixes("/app"); + } + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/seven-wonders-websocket").setHandshakeHandler(handshakeHandler()).withSockJS(); + } + + @Bean + public DefaultHandshakeHandler handshakeHandler() { + return new AnonymousUsersHandshakeHandler(); + } + +} \ No newline at end of file diff --git a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java index 705e6ef2..4b1f8ed4 100644 --- a/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java +++ b/src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.controllers; +import java.security.Principal; import java.util.HashMap; import java.util.Map; @@ -48,7 +49,8 @@ public class LobbyController { @MessageMapping("/join-game") @SendTo("/topic/players") - public Player joinGame(SimpMessageHeaderAccessor headerAccessor, JoinGameAction joinAction) throws Exception { + public Player joinGame(SimpMessageHeaderAccessor headerAccessor, JoinGameAction joinAction, Principal principal) + throws Exception { Thread.sleep(1000); // simulated delay Player player = (Player)headerAccessor.getSessionAttributes().get("player"); @@ -61,6 +63,7 @@ public class LobbyController { lobby = lobbies.get(joinAction.getGameId()); Player newPlayer = new Player(joinAction.getPlayerName()); + newPlayer.setUserName(principal.getName()); lobby.addPlayer(newPlayer); headerAccessor.getSessionAttributes().put("player", newPlayer); diff --git a/src/main/java/org/luxons/sevenwonders/game/Player.java b/src/main/java/org/luxons/sevenwonders/game/Player.java index ac7067b4..eafabf4a 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Player.java +++ b/src/main/java/org/luxons/sevenwonders/game/Player.java @@ -4,6 +4,8 @@ public class Player { private String name; + private String userName; + public Player(String name) { this.name = name; } @@ -15,4 +17,12 @@ public class Player { public void setName(String name) { this.name = name; } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } } -- cgit