Skip to content
Snippets Groups Projects
Commit a90b386b authored by Daniel Gerhardt's avatar Daniel Gerhardt
Browse files

Catch unauthenticated WebSocket messages

parent 596a007b
No related merge requests found
...@@ -28,6 +28,7 @@ import org.slf4j.Logger; ...@@ -28,6 +28,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable; import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
...@@ -58,14 +59,20 @@ public class WebsocketAuthenticationAspect { ...@@ -58,14 +59,20 @@ public class WebsocketAuthenticationAspect {
final SocketIOClient client, final T message) throws Throwable { final SocketIOClient client, final T message) throws Throwable {
logger.debug("Executing WebsocketAuthenticationAspect for onData event: Session Id: {}, Message Class: {}", logger.debug("Executing WebsocketAuthenticationAspect for onData event: Session Id: {}, Message Class: {}",
client.getSessionId(), message.getClass()); client.getSessionId(), message.getClass());
populateSecurityContext(client.getSessionId()); try {
pjp.proceed(); populateSecurityContext(client.getSessionId());
clearSecurityContext(); pjp.proceed();
} finally {
clearSecurityContext();
}
} }
private void populateSecurityContext(final UUID socketId) { private void populateSecurityContext(final UUID socketId) {
SecurityContext context = SecurityContextHolder.getContext();
UserAuthentication userAuth = userService.getUserToSocketId(socketId); UserAuthentication userAuth = userService.getUserToSocketId(socketId);
if (userAuth == null) {
throw new AccessDeniedException("No user authenticated for WebSocket connection");
}
SecurityContext context = SecurityContextHolder.getContext();
Set<GrantedAuthority> authorities = new HashSet<>(); Set<GrantedAuthority> authorities = new HashSet<>();
authorities.add(WEBSOCKET_AUTHORITY); authorities.add(WEBSOCKET_AUTHORITY);
User user = new User(userAuth, authorities); User user = new User(userAuth, authorities);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment