在 grizzly 上设置 websocket SSL [英] Setting websocket SSL on grizzly

查看:29
本文介绍了在 grizzly 上设置 websocket SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 grizzly 容器上使用javax.websocket.server.ServerEndpoint"配置基于 SSL 的 WebSocket.但是我找不到任何方法将 SSL 属性设置为我的端点.

I'm trying to configure a WebSocket over SSL with the "javax.websocket.server.ServerEndpoint" on a grizzly container. However i can't find any way to set the SSL property to my endpoint.

我的端点代码:

import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(
    Value="/ptiWs",
    decoders = {ApiMessage.ApiCoder.class},
)
public class WebsocketEndpoint {

   private static final Logger LOG = LogManager.getLogger(WebsocketEndpoint.class);
   private final ApiVisitorImpl apiVisitor;

    public WebsocketEndpoint(){
    }

    @OnOpen
    public void onOpen(Session session){
        LOG.info("New connection open : " + session.toString());
    }


    @OnMessage
    public void message(Session session, ApiMessage message){
        LOG.info("New message arrive " + message.toString());
    }
}

最后,我使用以下代码将我的端点添加到我的 Grizzly 实例:

Finally, I add my endpoint to my Grizzly instance with the following code :

Server ptiWebsocket = new Server("localhost", 8025, "/", null, WebsocketEndpoint.class);
ptiWebsocket.start();

我已经为 glassfish 完成了这项工作,这很容易,但在这里我找不到任何继续进行的方法.

I have already done this work for glassfish and it's pretty easy, but here i don't find any way to proceed.

和依赖:

   <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.0</version>
        <type>jar</type>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-server</artifactId>
        <version>1.7</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly-server</artifactId>
        <version>1.7</version>
    </dependency> 

谢谢

推荐答案

查看 tyrus 源代码,似乎不支持开箱即用.您需要创建一个与 org.glassfish.tyrus.container.grizzly.server.GrizzlyServerContainer 非常相似的新 ServerContainerFactory.从 Github 上获取代码.您可以制作自己的 GrizzlySSLServerContainer.然后您将在 start 方法中将 SSL 配置添加到 NetworkListener.然后,您可以将新 GrizzlySSLServerContainer 类的完整限定名称添加到 META-INF/services/org.glassfish.tyrus.spi.ServerContainerFactory 和您的 JAR 中,Tyrus 应该会选择它.

Looking at the tyrus source code, it looks like that this isn't supported out of the box. You'll need to make a new ServerContainerFactory much like the org.glassfish.tyrus.container.grizzly.server.GrizzlyServerContainer. Go grab the code out of Github. You can make your own GrizzlySSLServerContainer. And then you'll add the SSL config to the NetworkListener in the start method. You can then add the full qualified name of your new GrizzlySSLServerContainer class into a META-INF/services/org.glassfish.tyrus.spi.ServerContainerFactory with your JAR and Tyrus should pick it up.

这有点笨拙,不得不复制/粘贴代码很糟糕,但它应该可以工作.

It is somewhat hacky, and it sucks to have to copy/paste code but it should work.

1.) 将 GrizzlyServerContainer 复制到新的 GrizzlySSLServerContainer 类.

1.) Copy GrizzlyServerContainer to your new GrizzlySSLServerContainer class.

2.) 添加方法以将 SSL 配置数据提供到您的新容器类中.

2.) Add method to provide SSL configuration data into your new Container class.

3.) 向 NetworkListener 添加数据以实例化 SSL

3.) Add data to NetworkListener to instantiate SSL

4.) 将新类添加到 jar 的 META-INF/serivces 目录中.

4.) Add your new class to the META-INF/serivces directory of your jar.

这篇关于在 grizzly 上设置 websocket SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆