Unauthenticated socket works

This commit is contained in:
Claudio Maggioni 2020-03-11 16:23:58 +01:00
parent 589ef8c3cc
commit 707291e637
8 changed files with 100 additions and 76 deletions

View file

@ -23,6 +23,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-mail' implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework:spring-websocket:5.2.4.RELEASE'
implementation 'io.jsonwebtoken:jjwt:0.9.1' implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'org.springframework.security:spring-security-web' implementation 'org.springframework.security:spring-security-web'
implementation 'org.postgresql:postgresql' implementation 'org.postgresql:postgresql'

39
gradle.yml Normal file
View file

@ -0,0 +1,39 @@
# vim: set ts=2 sw=2 et tw=80:
image: gradle:jdk13
stages:
- build
- test
- deploy
smarthut_build:
stage: build
script:
- gradle assemble
artifacts:
paths:
- build/libs/*.jar
expire_in: 1 week
smarthut_test:
stage: test
script:
- gradle check
smarthut_deploy:
stage: deploy
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay
before_script:
- docker version
- docker info
- docker login -u smarthutsm -p $CI_DOCKER_PASS
script:
- "docker build -t smarthutsm/smarthut:${CI_COMMIT_BRANCH} --pull ."
- "docker push smarthutsm/smarthut:${CI_COMMIT_BRANCH}"
after_script:
- docker logout

View file

@ -51,6 +51,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// dont authenticate this particular request // dont authenticate this particular request
.authorizeRequests() .authorizeRequests()
.antMatchers( .antMatchers(
"/sensor-socket",
"/auth/login", "/auth/login",
"/swagger-ui.html", "/swagger-ui.html",
"/register", "/register",

View file

@ -1,7 +1,12 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket; package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
import javax.websocket.server.ServerEndpointConfig; import javax.websocket.server.ServerEndpointConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServerEndpointRegistration;
@Configuration
public class SensorSocketConfig extends ServerEndpointConfig.Configurator { public class SensorSocketConfig extends ServerEndpointConfig.Configurator {
public static SensorSocketEndpoint getInstance() { public static SensorSocketEndpoint getInstance() {
@ -10,6 +15,16 @@ public class SensorSocketConfig extends ServerEndpointConfig.Configurator {
private static SensorSocketEndpoint instance = new SensorSocketEndpoint(); private static SensorSocketEndpoint instance = new SensorSocketEndpoint();
@Bean
public ServerEndpointRegistration sensorSocketEndpoint() {
return new ServerEndpointRegistration("/sensor-socket", instance);
}
@Bean
public ServerEndpointExporter endpointExporter() {
return new ServerEndpointExporter();
}
@Override @Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException { public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
try { try {

View file

@ -1,32 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import javax.websocket.*;
public class SensorSocketDecoder implements Decoder.Text<JsonObject> {
private Gson decoder;
@Override
public void init(EndpointConfig endpointConfig) {
decoder = new Gson();
}
@Override
public void destroy() {}
@Override
public JsonObject decode(String s) throws DecodeException {
try {
return decoder.fromJson(s, JsonObject.class);
} catch (JsonSyntaxException e) {
throw new DecodeException(s, "Cannot decode sensor message", e);
}
}
@Override
public boolean willDecode(String s) {
return true;
}
}

View file

@ -1,24 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
public class SensorSocketEncoder implements Encoder.Text<JsonObject> {
private Gson encoder;
@Override
public String encode(JsonObject object) throws EncodeException {
return encoder.toJson(object);
}
@Override
public void init(EndpointConfig endpointConfig) {
encoder = new Gson();
}
@Override
public void destroy() {}
}

View file

@ -1,19 +1,14 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket; package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.*;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.*; import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint( public class SensorSocketEndpoint extends Endpoint {
value = "/service",
configurator = SensorSocketConfig.class, private Gson gson = new Gson();
encoders = SensorSocketEncoder.class,
decoders = SensorSocketDecoder.class)
public class SensorSocketEndpoint {
private Set<Session> clients = Collections.synchronizedSet(new HashSet<>()); private Set<Session> clients = Collections.synchronizedSet(new HashSet<>());
@ -21,16 +16,6 @@ public class SensorSocketEndpoint {
return clients; return clients;
} }
@OnOpen
public void onOpen(Session userSession) {
clients.add(userSession);
}
@OnClose
public void onClose(Session userSession) {
clients.remove(userSession);
}
public int broadcast(JsonObject message) throws IOException, EncodeException { public int broadcast(JsonObject message) throws IOException, EncodeException {
for (Session session : clients) { for (Session session : clients) {
System.out.println(message); System.out.println(message);
@ -38,4 +23,16 @@ public class SensorSocketEndpoint {
} }
return clients.size(); return clients.size();
} }
@Override
public void onOpen(Session session, EndpointConfig config) {
final JsonObject test = new JsonObject();
test.addProperty("ciao", "mamma");
try {
session.getBasicRemote().sendText(gson.toJson(test));
clients.add(session);
} catch (IOException e) {
e.printStackTrace();
}
}
} }

26
test.html Normal file
View file

@ -0,0 +1,26 @@
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
let connection = new WebSocket("ws://localhost:8080/sensor-socket", ["access_token", "malusa"]);
console.log("***CREATED WEBSOCKET");
connection.onopen = function(evt) {
console.log("***ONOPEN", evt);
connection.send({ciao: "mamma"});
};
connection.onmessage = function(evt) {
console.log("***ONMESSAGE", evt);
};
connection.onerror = function(evt) {
console.error("***ONERROR", evt);
};
console.log("***CREATED all");
</script>
</body>
</html>