Created
March 15, 2020 12:10
-
-
Save balvinder294/277cc48b5997fbc46f2de598e06d85b5 to your computer and use it in GitHub Desktop.
Sample confiuration for Websockets Configuration in Spring Boot - Tekraze.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tekraze.kafka.config; | |
import com.tekraze.kafka.security.AuthoritiesConstants; | |
import java.security.Principal; | |
import java.util.*; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.server.*; | |
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | |
import org.springframework.security.authentication.AnonymousAuthenticationToken; | |
import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
import org.springframework.web.socket.WebSocketHandler; | |
import org.springframework.web.socket.config.annotation.*; | |
import org.springframework.web.socket.server.HandshakeInterceptor; | |
import org.springframework.web.socket.server.support.DefaultHandshakeHandler; | |
import io.github.jhipster.config.JHipsterProperties; | |
@Configuration | |
@EnableWebSocketMessageBroker // TO Enable Broker that sends recieves messages over websocket | |
public class WebsocketConfiguration implements WebSocketMessageBrokerConfigurer { | |
public static final String IP_ADDRESS = "IP_ADDRESS"; | |
private final JHipsterProperties jHipsterProperties; | |
public WebsocketConfiguration(JHipsterProperties jHipsterProperties) { | |
this.jHipsterProperties = jHipsterProperties; | |
} | |
// Here configure the message broker path/endpoint | |
@Override | |
public void configureMessageBroker(MessageBrokerRegistry config) { | |
config.enableSimpleBroker("/topic"); | |
} | |
@Override | |
public void registerStompEndpoints(StompEndpointRegistry registry) { | |
String[] allowedOrigins = Optional.ofNullable(jHipsterProperties.getCors().getAllowedOrigins()).map(origins -> origins.toArray(new String[0])).orElse(new String[0]); | |
registry.addEndpoint("/websocket/tracker") // base endpint for connecting all sockets | |
.setHandshakeHandler(defaultHandshakeHandler()) // handhsake policy for security | |
.setAllowedOrigins(allowedOrigins) // origins to allow | |
.withSockJS() | |
.setInterceptors(httpSessionHandshakeInterceptor()); | |
} | |
@Bean | |
public HandshakeInterceptor httpSessionHandshakeInterceptor() { | |
return new HandshakeInterceptor() { | |
@Override | |
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception { | |
if (request instanceof ServletServerHttpRequest) { | |
ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; | |
attributes.put(IP_ADDRESS, servletRequest.getRemoteAddress()); | |
} | |
return true; | |
} | |
@Override | |
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) { | |
} | |
}; | |
} | |
`private DefaultHandshakeHandler defaultHandshakeHandler() { | |
`return new DefaultHandshakeHandler() { | |
@Override | |
protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) { | |
Principal principal = request.getPrincipal(); | |
if (principal == null) { | |
Collection<SimpleGrantedAuthority> authorities = new ArrayList<>(); | |
authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.ANONYMOUS)); | |
principal = new AnonymousAuthenticationToken("WebsocketConfiguration", "anonymous", authorities); | |
} | |
return principal; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment