Grizzly服务器的基本身份验证

时间:2012-07-05 11:01:33

标签: java basic-authentication grizzly

我已将此示例应用到我的应用程序中:

https-clientserver-grizzly

来自该链接的 Server.java

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(4463)).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
        WebappContext context = new WebappContext("context");
        ServletRegistration registration = 
                context.addServlet("ServletContainer", ServletContainer.class);
        registration.setInitParameter("com.sun.jersey.config.property.packages",
                "com.sun.jersey.samples.https_grizzly.resource;com.sun.jersey.samples.https_grizzly.auth");

        // add security filter (which handles http basic authentication)
        registration.setInitParameter(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS,
                SecurityFilter.class.getName());

        // Grizzly ssl configuration
        SSLContextConfigurator sslContext = new SSLContextConfigurator();

        // set up security context
        sslContext.setKeyStoreFile("./keystore_server"); // contains server keypair
        sslContext.setKeyStorePass("asdfgh");
        sslContext.setTrustStoreFile("./truststore_server"); // contains client certificate
        sslContext.setTrustStorePass("asdfgh");

        try {

            webServer = GrizzlyServerFactory.createHttpServer(
                    getBaseURI(),
                    null,
                    true,
                    new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(true)
            );

            // start Grizzly embedded server //
            System.out.println("Jersey app started. Try out " + BASE_URI + "\nHit CTRL + C to stop it...");
            context.deploy(webServer);
            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 {
        startServer();

        System.in.read();
    }
}

但是我决定只启用基本身份验证(不需要SSl身份验证)

我改变了那一部分:

UriBuilder.fromUri("http://localhost/").port(getPort(4463)).build();

我评论了那一行的部分:

SSLConfig sslConfig = new SSLConfig();

直到那一行:

System.out.println("Jersey app started. Try out " + BASE_URI + "\nHit CTRL + C to stop it...");    

但是我收到了这个错误:

com.sun.grizzly.DefaultProtocolChain executeProtocolFilter
SEVERE: ProtocolChain exception
java.lang.NullPointerException
    at com.sun.grizzly.filter.SSLReadFilter.newSSLEngine(SSLReadFilter.java:331)
    at com.sun.grizzly.filter.SSLReadFilter.obtainSSLEngine(SSLReadFilter.java:378)
    at com.sun.grizzly.filter.SSLReadFilter.execute(SSLReadFilter.java:147)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:136)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:103)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:89)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:67)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

如何编写一个只需要在grizzly进行基本身份验证的代码?

1 个答案:

答案 0 :(得分:0)

这是关于灰熊的一个错误。任何有关克服该错误的想法都是受欢迎的。然而,这里有一个棘手的解决方案:

http://grizzly.1045725.n5.nabble.com/Grizzly-2-Guice-Jersey-tp5709778p5709787.html