Browse Source

websocket configs

cloud-keycloak
aidar 3 years ago
parent
commit
4691a0021b
3 changed files with 45 additions and 8 deletions
  1. +6
    -6
      demo-client-app/src/App.js
  2. +38
    -1
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/config/WebSocketConfig.java
  3. +1
    -1
      demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/web/controller/UserInfoController.java

+ 6
- 6
demo-client-app/src/App.js View File

@ -6,16 +6,16 @@ import "./App.css";
let stomp let stomp
const connect = async () => {
const connect = async (token) => {
if (stomp != null) return; if (stomp != null) return;
console.info("Connect to Websocket"); console.info("Connect to Websocket");
const sock = new SockJS("/api/ws"); const sock = new SockJS("/api/ws");
stomp = Stomp.over(sock); stomp = Stomp.over(sock);
stomp.connect( stomp.connect(
{},
() => {
console.log("Connected");
{"X-Authorization": token},
(frame) => {
console.log("Connected", frame);
}, },
error => { error => {
console.error("Disconnected"); console.error("Disconnected");
@ -38,8 +38,8 @@ function App({ keycloak }) {
}, [keycloak.token]); }, [keycloak.token]);
useEffect(() => { useEffect(() => {
connect();
}, []);
connect(keycloak.token);
}, [keycloak.token]);
return ( return (
<div className="App"> <div className="App">


+ 38
- 1
demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/config/WebSocketConfig.java View File

@ -1,12 +1,25 @@
package ru.digitalbanana.demoresourceserver.config; package ru.digitalbanana.demoresourceserver.config;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration; import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
/** /**
* Web Socket configuration * Web Socket configuration
* Created by dima on 8/12/16. * Created by dima on 8/12/16.
@ -15,6 +28,30 @@ import org.springframework.web.socket.config.annotation.WebSocketTransportRegist
@EnableWebSocketMessageBroker @EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Autowired
private JwtDecoder jwtDecoder;
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(new ChannelInterceptor() {
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor =
MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
List<String> authorization = accessor.getNativeHeader("X-Authorization");
String accessToken = authorization.get(0);//.split(" ")[1];
Jwt jwt = jwtDecoder.decode(accessToken);
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
Authentication authentication = converter.convert(jwt);
accessor.setUser(authentication);
}
return message;
}
});
}
@Override @Override
public void configureMessageBroker(MessageBrokerRegistry registry) { public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app"); registry.setApplicationDestinationPrefixes("/app");


+ 1
- 1
demo-resource-server/src/main/java/ru/digitalbanana/demoresourceserver/web/controller/UserInfoController.java View File

@ -27,7 +27,7 @@ public class UserInfoController {
@GetMapping(value = "/userinfo") @GetMapping(value = "/userinfo")
public String userinfo(@AuthenticationPrincipal Jwt principal) { public String userinfo(@AuthenticationPrincipal Jwt principal) {
return principal.getClaimAsString("email");
return principal.getClaimAsString("sub");
} }
@GetMapping(value = "/users") @GetMapping(value = "/users")


Loading…
Cancel
Save