SSL与Grizzly和Jersey [英] SSL with Grizzly and Jersey

查看:252
本文介绍了SSL与Grizzly和Jersey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让灰熊使用SSL加密,并且仍可以使用Jersey。我看过整个互联网,我发现了与Grizzly和Jersey的各种不同尝试。似乎有不同的方法,取决于您使用的版本,以及您决定如何实现它。我还没有能够使用任何示例来处理我的代码。



以下是我启动服务器的方式:

  static HttpServer startSecureServer()抛出IOException {
ResourceConfig rc = new PackagesResourceConfig(server.grizzlyresources);
SSLContextConfigurator sslCon = new SSLContextConfigurator();

sslCon.setKeyStoreFile(ConfigLoader.getKeystoreLocation()); //包含服务器密钥对
sslCon.setKeyStorePass(ConfigLoader.getKeystorePassword());

System.out.println(在端口上启动服务器+ ConfigLoader.getHttpsServerPort());
HttpServer secure = GrizzlyServerFactory.createHttpServer(BASE_URI_SECURED,rc);
secure.stop();

HashSet< NetworkListener> lists = new HashSet< NetworkListener>(secure.getListeners());
for(NetworkListener listener:lists){
listener.setSecure(true);
SSLEngineConfigurator ssle = new SSLEngineConfigurator(sslCon);
listener.setSSLEngineConfig(ssle);
secure.addListener(listener);
System.out.println(listener);
}

secure.start();
返回安全;
}

私有静态URI getBaseURISecured(){
返回UriBuilder.fromUri(https://0.0.0.0/)。port(ConfigLoader.getHttpsServerPort())。建立();
}

私有静态最终URI BASE_URI_SECURED = getBaseURISecured();

ConfigLoader从配置文件中加载信息。当我运行这段代码时,它会启动服务器,它会在server.grizzlyresources包中找到资源,而且效果很好!除了一件事。服务器不安全。我可以telnet到它并以纯文本形式发送我的一个资源的HTTP请求,它将返回它。所以代码适用于启动服务器,但它的整个SSL部分只是被绕过了。任何想法如何解决这个或为什么它可能会这样做?



以下是我运行时输出到控制台:

 在端口9999上启动服务器
2014年1月13日上午9:51:08 com.sun.jersey.api.core.PackagesResourceConfig init
INFO :扫描包中的根资源和提供者类:
server.grizzlyresources
2014年1月13日上午9:51:08 com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO :找到根资源类:
class server.grizzlyresources.SessionResource
class server.grizzlyresources.LoginResource
2014年1月13日上午9:51:08 com.sun.jersey.api.core。 ScanningResourceConfig init
INFO:找不到提供程序类。
2014年1月13日上午9:51:08 com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO:启动Jersey应用程序,版本'Jersey:1.12 02/15/2012 04: 51 PM'
2014年1月13日上午9:51:09 org.glassfish.grizzly.http.server.NetworkListener start
INFO:已启动侦听器绑定到[0.0.0.0:9999]
2014年1月13日上午9:51:09 org.glassfish.grizzly.http.server.HttpServer start
INFO:[HttpServer]已启动。
2014年1月13日上午9:51:09 org.glassfish.grizzly.http.server.NetworkListener stop
INFO:已停止的侦听器绑定到[0.0.0.0:9999]
NetworkListener {name ='grizzly',host ='0.0.0.0',port = 9999,secure = true}
2014年1月13日上午9:51:09 org.glassfish.grizzly.http.server.NetworkListener start
INFO:已启动侦听器绑定到[0.0.0.0:9999]
2014年1月13日上午9:51:09 org.glassfish.grizzly.http.server.HttpServer启动
INFO:[HttpServer]已启动。

我正在使用Grizzly 2.2.1和Jersey 1.12。



非常感谢!

解决方案

IMO可以使用不同的Factory方法初始化安全的Grizzly HttpServer:

  HttpServer secure = GrizzlyServerFactory.createHttpServer(BASE_URI_SECURED,
ContainerFactory.createContainer(HttpHandler.class,rc),
true,
new SSLEngineConfigurator(sslCon));

如果像这样初始化服务器,则无需再次停止并重新配置它。 / p>

希望这会有所帮助。


I'm trying to get grizzly to use SSL encryption and still work fine with Jersey. I've looked all over the Internet, and I find all kinds of different attempts at SSL with Grizzly and Jersey. Seems like there are different ways of doing it depending on which version you are using, and how you decided to implement it. I haven't been able to get any examples to work with my code yet.

Here's how I start up my server:

static HttpServer startSecureServer() throws IOException{
        ResourceConfig rc=new PackagesResourceConfig("server.grizzlyresources");
        SSLContextConfigurator sslCon=new SSLContextConfigurator();

        sslCon.setKeyStoreFile(ConfigLoader.getKeystoreLocation()); // contains server keypair
        sslCon.setKeyStorePass(ConfigLoader.getKeystorePassword());

        System.out.println("Starting server on port "+ConfigLoader.getHttpsServerPort());
        HttpServer secure=GrizzlyServerFactory.createHttpServer(BASE_URI_SECURED, rc);
        secure.stop();

        HashSet<NetworkListener> lists=new HashSet<NetworkListener>(secure.getListeners());
        for (NetworkListener listener : lists){
            listener.setSecure(true);
            SSLEngineConfigurator ssle=new SSLEngineConfigurator(sslCon);
            listener.setSSLEngineConfig(ssle);
            secure.addListener(listener);
            System.out.println(listener);
        }

        secure.start();
        return secure;
}

private static URI getBaseURISecured(){
    return UriBuilder.fromUri("https://0.0.0.0/").port(ConfigLoader.getHttpsServerPort()).build();
}

private static final URI BASE_URI_SECURED = getBaseURISecured();

ConfigLoader loads in information from a config file. When I run this code, it starts up the server, it finds the resources in the server.grizzlyresources package, and it works great! Except for one thing. The server isn't secured. I can telnet into it and send an HTTP request in plain text for one of my resources, and it will return it. So the code works for starting up the server, but the whole SSL part of it is just being bypassed. Any ideas how to fix this or why it might be doing this?

Here's the output to the console when I run it:

Starting server on port 9999
Jan 13, 2014 9:51:08 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  server.grizzlyresources
Jan 13, 2014 9:51:08 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class server.grizzlyresources.SessionResource
  class server.grizzlyresources.LoginResource
Jan 13, 2014 9:51:08 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Jan 13, 2014 9:51:08 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.12 02/15/2012 04:51 PM'
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [0.0.0.0:9999]
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.NetworkListener stop
INFO: Stopped listener bound to [0.0.0.0:9999]
NetworkListener{name='grizzly', host='0.0.0.0', port=9999, secure=true}
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [0.0.0.0:9999]
Jan 13, 2014 9:51:09 AM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.

I'm using Grizzly 2.2.1, and Jersey 1.12.

Thanks a bunch!

解决方案

IMO you can use different Factory method to initialize secured Grizzly HttpServer:

HttpServer secure = GrizzlyServerFactory.createHttpServer(BASE_URI_SECURED,
                        ContainerFactory.createContainer(HttpHandler.class, rc),
                        true,
                        new SSLEngineConfigurator(sslCon));

If you initialize the server like this, you don't need to stop and reconfigure it again.

Hope this will help.

这篇关于SSL与Grizzly和Jersey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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