gpt4 book ai didi

java - Spring Boot Raw WebSocket 设置主体

转载 作者:行者123 更新时间:2023-12-01 19:41:02 25 4
gpt4 key购买 nike

我想用Spring Boot实时开发通知发送器,但我不能使用Stomp和sockjs,所以我需要实现原始WebSocket,但我不知道如何在WebSocket连接中设置Principal,因为我想进行身份验证使用 JWT token 。那么,我在哪里或如何设置本金。我正在使用这些;

WebSocketConfig.java:

@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {

@Autowired
WebSocketNotificationSenderService senderService;

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry
webSocketHandlerRegistry) {

webSocketHandlerRegistry.addHandler(createHandler(),
"/handler").addInterceptors(new HttpSessionHandshakeInterceptor()
{
@Override
public void afterHandshake(ServerHttpRequest request,
ServerHttpResponse response, WebSocketHandler wsHandler,
@Nullable Exception ex) {

super.afterHandshake(request, response, wsHandler, ex);

}

@Override
public boolean beforeHandshake(ServerHttpRequest request,
ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) throws Exception {

return super.beforeHandshake(request, response,
wsHandler, attributes)
}

});
}

@Bean
public WebSocketHandler createHandler() {

return new MyHandler(senderService);

}
}

MyHandler.java:

@Component
public class MyHandler extends TextWebSocketHandler {

WebSocketNotificationSenderService senderService;

public MyHandler(WebSocketNotificationSenderService senderService){
this.senderService = senderService;
}

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
senderService.addToSession(session);
}
}

WebSocketNotificationSenderService.java:

@Service
public class WebSocketNotificationSenderService implements
IWebSocketSenderService<WrapperWsNotification> {

private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
private ObjectMapper mapper = new ObjectMapper();

public void addToSession(WebSocketSession session) {
sessions.add(session);
}

@Override
public void convertAndSend(WrapperWsNotification payload) throws JsonProcessingException {
String payloadString = mapper.writeValueAsString(payload);
sessions.stream().forEach(session -> {
try {
session.sendMessage(new TextMessage(payloadString));
} catch (IOException e) {
e.printStackTrace();
}

});
}

@Override
public void convertAndSendToUser(String user, WrapperWsNotification payload) throws
JsonProcessingException {
String payloadString = mapper.writeValueAsString(payload);
sessions.forEach(session -> {
if (session.getPrincipal().getName().equals(user)) {
try {
session.sendMessage(new TextMessage(payloadString));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}

我的通知发送者到 websocket;

@Component
public class NotificationConsumer {

@Autowired
WebSocketNotificationSenderService webSocket;

private Logger logger = LoggerFactory.getLogger(NotificationConsumer.class);

public void onReceiveNotification(String object) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
WrapperWsNotification wrapperWsNotification= objectMapper.readValue(object, WrapperWsNotification.class);
logger.info("User where coming from redis " + wrapperWsNotification.getUser().getUsername());
webSocket.convertAndSendToUser(wrapperWsNotification.getUser().getUsername(), wrapperWsNotification);
}


}

最佳答案

我找到了解决方案并添加了 example

关于java - Spring Boot Raw WebSocket 设置主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59173868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com