[Minor] Adding remoteIp to ThreadLocal and using in authentication

This commit is contained in:
Robert von Burg 2019-03-12 15:25:35 +01:00
parent 0f8f94dc10
commit 7c11292199
3 changed files with 18 additions and 3 deletions

View File

@ -144,7 +144,7 @@ public class WebSocketClient implements MessageHandler.Whole<String> {
try {
StrolchSessionHandler sessionHandler = RestfulStrolchComponent.getInstance().getSessionHandler();
this.certificate = sessionHandler.validate(authToken);
this.certificate = sessionHandler.validate(authToken, WebSocketRemoteIp.get());
if (!this.certificate.getUsername().equals(username)) {
logger.error("Invalid authentication for " + username);
close(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "Invalid authentication");

View File

@ -20,8 +20,9 @@ public class WebSocketFilter implements Filter {
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
logger.info("Remote IP: " + getRemoteIp(httpRequest) + ": " + httpRequest.getMethod() + " " + httpRequest
.getRequestURI());
String remoteIp = getRemoteIp(httpRequest);
logger.info("Remote IP: " + remoteIp + ": " + httpRequest.getMethod() + " " + httpRequest.getRequestURI());
WebSocketRemoteIp.set(remoteIp);
chain.doFilter(request, response);
}

View File

@ -0,0 +1,14 @@
package li.strolch.websocket;
public class WebSocketRemoteIp {
private static final ThreadLocal<String> threadLocal = ThreadLocal.withInitial(() -> "notset");
public static String get() {
return threadLocal.get();
}
public static void set(String remoteIp) {
threadLocal.set(remoteIp);
}
}