users@grizzly.java.net

Re: SSL how to?

From: kevinb <kbrisso_at_gmail.com>
Date: Mon, 9 Jan 2012 20:57:13 -0800 (PST)

Here is my update and some code that I did get working. I used the keys and
code provided with the sample link in this thread. I modified the code so it
does not use the SecurityFilter.java. The code below will allow SSL
connections to the Jersey/REST and the static HTML files that I use. I
verified this with WireShark.

Thanks Everyone for your help!

package com.frk.mw.trillium;
       
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.ResourceConfig;
//import com.sun.jersey.samples.https_grizzly.auth.SecurityFilter;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.ServletHandler;
import org.glassfish.grizzly.ssl.SSLContextConfigurator;
import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
import org.glassfish.grizzly.http.server.StaticHttpHandler;

import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import pkgs.natives.TrilTGenClient;
import com.frk.mw.trillium.factory.*;


public class Server {

    private static HttpServer webServer;

    public static final URI BASE_URI = getBaseURI();
    public static final String CONTENT = "JERSEY HTTPS EXAMPLE\n";

    private static URI getBaseURI() {
        
         
        return
UriBuilder.fromUri("https://localhost/").port(getPort(443)).build();
    }

    private static int getPort(int defaultPort) {
        String port = System.getProperty("jersey.test.port");
        if (null != port) {
            try {
                return Integer.parseInt(port);
            } catch (NumberFormatException e) {
            }
        }
        return defaultPort;
    }

    protected static void startServer() {

        // add Jersey resource servlet

        ServletHandler jerseyAdapter = new ServletHandler();
       
jerseyAdapter.addInitParameter("com.sun.jersey.config.property.packages",
"com.frk.mw.trillium");
        jerseyAdapter.setContextPath("/");
        jerseyAdapter.setServletInstance(new ServletContainer());

        // add security filter (which handles http basic authentication)

       
//jerseyAdapter.addInitParameter(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS,
SecurityFilter.class.getName());

        // Grizzly ssl configuration

        SSLContextConfigurator sslContext = new SSLContextConfigurator();
        
        // set up security context
       
sslContext.setKeyStoreFile("C:\\Projects\\MWTrilliumPOC\\keys\\keystore_server");
// contains server keypair
        sslContext.setKeyStorePass("asdfgh");
       
sslContext.setTrustStoreFile("C:\\Projects\\MWTrilliumPOC\\keys\\truststore_server");
// contains client certificate
        sslContext.setTrustStorePass("asdfgh");

        try {

            webServer = GrizzlyServerFactory.createHttpServer(
                    getBaseURI(),
                    jerseyAdapter,
                    true,
                    new
SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(false)
            );
           
            
            webServer.getServerConfiguration().addHttpHandler(new
StaticHttpHandler("C:\\Projects\\MWTrilliumPOC\\www\\"), "/main");
            webServer.getServerConfiguration().addHttpHandler(new
StaticHttpHandler("C:\\Projects\\MWTrilliumPOC\\www\\js\\"), "/main");


            // start Grizzly embedded server //
            System.out.println("Jersey app started. Try out " + BASE_URI +
"\nHit CTRL + C to stop it...");
            webServer.start();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    protected static void stopServer() {
        webServer.stop();
    }

    public static void main(String[] args) throws InterruptedException,
IOException {
        
       TrilTGenClient TrilTGenClient =
TrilClientCacheFactory.getCacheInstance();
        
        startServer();

        System.in.read();
    }
}






--
View this message in context: http://grizzly.1045725.n5.nabble.com/SSL-how-to-tp5126906p5133324.html
Sent from the Grizzly - Users mailing list archive at Nabble.com.