Skip to content

Instantly share code, notes, and snippets.

@balvinder294
Created March 15, 2020 12:21
Show Gist options
  • Save balvinder294/cb6a02a3eab1e33958274204007be5a6 to your computer and use it in GitHub Desktop.
Save balvinder294/cb6a02a3eab1e33958274204007be5a6 to your computer and use it in GitHub Desktop.
Sample configuration for Security of web sockets In Spring - Tekraze
package com.tekraze.kafka.config;
import com.tekraze.kafka.security.AuthoritiesConstants;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.security.config.annotation.web.messaging.MessageSecurityMetadataSourceRegistry;
import org.springframework.security.config.annotation.web.socket.AbstractSecurityWebSocketMessageBrokerConfigurer;
@Configuration
public class WebsocketSecurityConfiguration extends AbstractSecurityWebSocketMessageBrokerConfigurer
// TO set authentication for endpoints, you can also add endpoint for public also, if you require
@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
messages
.nullDestMatcher().authenticated()
.simpDestMatchers("/topic/tracker").hasAuthority(AuthoritiesConstants.ADMIN)
// matches any destination that starts with /topic/
// (i.e. cannot send messages directly to /topic/)
// (i.e. cannot subscribe to /topic/messages/* to get messages sent to
// /topic/messages-user<id>)
.simpDestMatchers("/topic/**").authenticated()
// message types other than MESSAGE and SUBSCRIBE
.simpTypeMatchers(SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE).denyAll()
// catch all
.anyMessage().denyAll();
}
/**
* Disables CSRF for Websockets.
*/
@Override
protected boolean sameOriginDisabled() {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment