Setting websocket SSL on grizzly

2020-08-09 03:56发布

问题:

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.

My endpoint code :

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());
    }
}

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

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

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

And the dependency :

   <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> 

Thanks

回答1:

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.) Copy GrizzlyServerContainer to your new GrizzlySSLServerContainer class.

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

3.) Add data to NetworkListener to instantiate SSL

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