summaryrefslogtreecommitdiff
path: root/src/main/java/org/luxons/sevenwonders
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2016-12-17 22:27:23 +0100
committerjbion <joffrey.bion@amadeus.com>2016-12-17 22:27:23 +0100
commitc33fdcdd1e972c1db410e02e14cdaa15229b1734 (patch)
tree5fdf04e638bffd733095fdda7f933997f9cbe487 /src/main/java/org/luxons/sevenwonders
parentAdd skeleton for game turns and card playing (diff)
downloadseven-wonders-c33fdcdd1e972c1db410e02e14cdaa15229b1734.tar.gz
seven-wonders-c33fdcdd1e972c1db410e02e14cdaa15229b1734.tar.bz2
seven-wonders-c33fdcdd1e972c1db410e02e14cdaa15229b1734.zip
Add default username for unauthenticated users
Diffstat (limited to 'src/main/java/org/luxons/sevenwonders')
-rw-r--r--src/main/java/org/luxons/sevenwonders/config/AnonymousUsersHandshakeHandler.java25
-rw-r--r--src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java (renamed from src/main/java/org/luxons/sevenwonders/WebSocketConfig.java)11
-rw-r--r--src/main/java/org/luxons/sevenwonders/controllers/LobbyController.java5
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Player.java10
4 files changed, 48 insertions, 3 deletions
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<String, Object> attributes) {
+ Principal p = request.getPrincipal();
+ if (p == null) {
+ p = new PrincipalImpl("player" + playerId++);
+ }
+ return p;
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/WebSocketConfig.java b/src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java
index b0613e79..81c90c9b 100644
--- a/src/main/java/org/luxons/sevenwonders/WebSocketConfig.java
+++ b/src/main/java/org/luxons/sevenwonders/config/WebSocketConfig.java
@@ -1,10 +1,12 @@
-package org.luxons.sevenwonders;
+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
@@ -22,7 +24,12 @@ public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
- registry.addEndpoint("/seven-wonders-websocket").withSockJS();
+ 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;
+ }
}
bgstack15