Skip to content

Commit 7da4a39

Browse files
committed
update
1 parent 7fcf779 commit 7da4a39

File tree

7 files changed

+28
-147
lines changed

7 files changed

+28
-147
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<artifactId>http-server</artifactId>
4141
<version>1.0.0</version>
4242
</dependency>
43+
<dependency>
44+
<groupId>info.unterrainer.commons</groupId>
45+
<artifactId>oauth-token-manager</artifactId>
46+
<version>1.0.7</version>
47+
</dependency>
4348
</dependencies>
4449

4550
<build>

src/main/java/info/unterrainer/websocketserver/JwtTokenHandler.java

Lines changed: 0 additions & 136 deletions
This file was deleted.

src/main/java/info/unterrainer/websocketserver/WebsocketServer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import org.jetbrains.annotations.NotNull;
77

8+
import info.unterrainer.oauthtokenmanager.OauthTokenManager;
89
import io.javalin.Javalin;
910
import io.javalin.websocket.WsHandler;
1011
import lombok.extern.slf4j.Slf4j;
@@ -14,7 +15,7 @@ public class WebsocketServer {
1415

1516
private String keycloakHost;
1617
private String realm;
17-
private JwtTokenHandler tokenHandler;
18+
private OauthTokenManager tokenManager;
1819

1920
private Javalin wss;
2021
private boolean isOauthEnabled = false;
@@ -28,8 +29,8 @@ public WebsocketServer(String keycloakHost, String keycloakRealm) {
2829
this.realm = keycloakRealm;
2930

3031
try {
31-
tokenHandler = new JwtTokenHandler(this.keycloakHost, this.realm);
32-
tokenHandler.initPublicKey();
32+
tokenManager = new OauthTokenManager(this.keycloakHost, this.realm);
33+
tokenManager.initPublicKey();
3334
wss = Javalin.create();
3435
isOauthEnabled = true;
3536
} catch (Exception e) {
@@ -54,7 +55,7 @@ public WebsocketServer wsOauth(@NotNull String path, @NotNull WsOauthHandlerBase
5455
throw new IllegalStateException("Websocket server is not configured for OAuth2/JWT support.");
5556
}
5657

57-
handler.setTokenHandler(tokenHandler);
58+
handler.setTokenHandler(tokenManager);
5859
wss.ws(path, ws -> {
5960
ws.onConnect(handler::onConnect);
6061
ws.onMessage(handler::onMessage);

src/main/java/info/unterrainer/websocketserver/WsOauthHandlerBase.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package info.unterrainer.websocketserver;
22

3+
import java.io.EOFException;
4+
import java.io.IOException;
5+
6+
import info.unterrainer.oauthtokenmanager.OauthTokenManager;
37
import io.javalin.websocket.WsCloseContext;
48
import io.javalin.websocket.WsConnectContext;
59
import io.javalin.websocket.WsErrorContext;
@@ -9,16 +13,16 @@
913
@Slf4j
1014
public class WsOauthHandlerBase extends WsHandlerBase {
1115

12-
private JwtTokenHandler tokenHandler;
16+
private OauthTokenManager tokenHandler;
1317

14-
void setTokenHandler(JwtTokenHandler tokenHandler) {
18+
void setTokenHandler(OauthTokenManager tokenHandler) {
1519
this.tokenHandler = tokenHandler;
1620
}
1721

1822
@Override
1923
public void onConnect(WsConnectContext ctx) throws Exception {
2024
log.debug("New client tries to connect: [{}]", ctx.session.getRemoteAddress());
21-
String token = ctx.queryParam("token");
25+
String token = ctx.header("Authorization");
2226
log.debug("New client token: [{}]", token);
2327
try {
2428
tokenHandler.checkAccess(token);
@@ -40,6 +44,11 @@ public void onClose(WsCloseContext ctx) throws Exception {
4044

4145
@Override
4246
public void onError(WsErrorContext ctx) throws Exception {
43-
log.error("Error on [{}]: [{}]", ctx.session.getRemoteAddress(), ctx.error());
47+
Throwable t = ctx.error();
48+
if (t instanceof EOFException || t instanceof IOException) {
49+
log.debug("Client disconnected [{}].", ctx.session.getRemoteAddress());
50+
} else {
51+
log.error("Unexpected error on [{}].", ctx.session.getRemoteAddress(), t);
52+
}
4453
}
4554
}

src/main/resources/log4j.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
1111

1212
# Print only info for imported libraries.
1313
log4j.logger.io.netty=WARN
14-
log4j.logger.org.eclipse.milo=WARN
14+
log4j.logger.org.eclipse.milo=WARN
15+
log4j.logger.org.eclipse.jetty=WARN

src/test/java/info/unterrainer/websocketserver/WebSocketServerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static void main(String[] args) {
88

99
aiComm = new AiComm();
1010

11-
server.wsOauth("/wss", aiComm);
11+
server.wsOauth("/jwt", aiComm);
1212
server.ws("/ws", ws -> {
1313
ws.onConnect(ctx -> ctx.send("Welcome to our websocket-server!"));
1414
ws.onMessage(ctx -> ctx.send("Echo from server: [" + ctx.message() + "]"));

src/test/resources/log4j.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
1111

1212
# Print only info for imported libs.
1313
log4j.logger.io.netty=WARN
14-
log4j.logger.org.eclipse.milo=WARN
14+
log4j.logger.org.eclipse.milo=WARN
15+
log4j.logger.org.eclipse.jetty=WARN

0 commit comments

Comments
 (0)