| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 |
<!-- |
| 3 |
|
| 4 |
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 5 |
|
| 6 |
Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 7 |
|
| 8 |
The contents of this file are subject to the terms of either the GNU |
| 9 |
General Public License Version 2 only ("GPL") or the Common Development |
| 10 |
and Distribution License("CDDL") (collectively, the "License"). You |
| 11 |
may not use this file except in compliance with the License. You can |
| 12 |
obtain a copy of the License at |
| 13 |
http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 14 |
or packager/legal/LICENSE.txt. See the License for the specific |
| 15 |
language governing permissions and limitations under the License. |
| 16 |
|
| 17 |
When distributing the software, include this License Header Notice in each |
| 18 |
file and include the License file at packager/legal/LICENSE.txt. |
| 19 |
|
| 20 |
GPL Classpath Exception: |
| 21 |
Oracle designates this particular file as subject to the "Classpath" |
| 22 |
exception as provided by Oracle in the GPL Version 2 section of the License |
| 23 |
file that accompanied this code. |
| 24 |
|
| 25 |
Modifications: |
| 26 |
If applicable, add the following below the License Header, with the fields |
| 27 |
enclosed by brackets [] replaced by your own identifying information: |
| 28 |
"Portions Copyright [year] [name of copyright owner]" |
| 29 |
|
| 30 |
Contributor(s): |
| 31 |
If you wish your version of this file to be governed by only the CDDL or |
| 32 |
only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 33 |
elects to include this software in this distribution under the [CDDL or GPL |
| 34 |
Version 2] license." If you don't indicate a single choice of license, a |
| 35 |
recipient has the option to distribute your version of this file under |
| 36 |
either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 37 |
its licensees as provided above. However, if you add GPL Version 2 code |
| 38 |
and therefore, elected the GPL Version 2 license, then the option applies |
| 39 |
only if the new code is made subject to such option by the copyright |
| 40 |
holder. |
| 41 |
|
| 42 |
--> |
| 43 |
<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><title>HTTPS Client/Server Example - Grizzly</title></head> |
| 44 |
|
| 45 |
|
| 46 |
<body style="width: 50em"> |
| 47 |
<h1>HTTPS Client/Server Example - Grizzly</h1> |
| 48 |
|
| 49 |
<p>This example demonstrates how to develop RESTful HTTPS Server using |
| 50 |
Grizzly and how to implement HTTPS Client using Jersey with server authentication.</p> |
| 51 |
|
| 52 |
<h2>Contents</h2> |
| 53 |
<h3>Server side</h3> |
| 54 |
|
| 55 |
<p>This example consists of just one resource - RootResource, which is |
| 56 |
basically copy of HelloWorld Resource from corresponding sample with little |
| 57 |
improvement.</p> |
| 58 |
|
| 59 |
<p>Other classes are used to start Grizzly embedded server and set up its |
| 60 |
authentication and authorization mechanism and keystore and truststore. |
| 61 |
</p> |
| 62 |
|
| 63 |
<h3>Client side</h3> |
| 64 |
<p>Client side is implemented as a test case, see class <b>com.sun.jersey.samples.https_grizzly.MainTest</b>, |
| 65 |
and its method <code>testSSLWithAuth</code> (others are just tests for invalid authorization). |
| 66 |
First thing you have to do if you want to communicate with service via |
| 67 |
https is set up <b>SSLContext</b> which is basically providing keystore and |
| 68 |
truststore. Keystore is used for storing own keys and truststore is used for storing certificates to |
| 69 |
which you have decided to trust. For more informations see <a href="#JSSERefGuide">[1]</a>.</p> |
| 70 |
<p>To set <b>SSLContext</b> on Jersey client you have to create instance of <b>com.sun.jersey.client.urlconnection.HTTPSProperties</b> |
| 71 |
and set it as a property to the client instance: |
| 72 |
|
| 73 |
<pre> HTTPSProperties prop = new HTTPSProperties(null, context); |
| 74 |
|
| 75 |
DefaultClientConfig dcc = new DefaultClientConfig(); |
| 76 |
dcc.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, prop); |
| 77 |
|
| 78 |
Client c = Client.create(dcc);</pre> |
| 79 |
|
| 80 |
|
| 81 |
<h3>Certificates setup</h3> |
| 82 |
<p><b>These steps are not required to run this example. Pregenerated keystore and |
| 83 |
truststore files are already present. Password used was "asdfgh".</b></p> |
| 84 |
<p>We needed set up few things to get this example working: |
| 85 |
<ul> |
| 86 |
<li>generate client and server keys</li> |
| 87 |
<li>generate client and server certificates</li> |
| 88 |
<li>import certificates to corresponding truststores</li> |
| 89 |
</ul> |
| 90 |
Client certificate is needed too because we're going to use server-side |
| 91 |
certificate authentication as well (yes, after this HTTP Basic authentication |
| 92 |
seems to be kind of redundant but there are some usecases where you might |
| 93 |
want to use them both). |
| 94 |
|
| 95 |
<p> |
| 96 |
Generate client key and store it into keystore:<br /> |
| 97 |
<pre>keytool -genkey -keystore ./keystore_client -alias clientKey -dname "CN=Client, OU=Jersey, O=Oracle, L=Prague, ST=Czech Republic, C=CZ"</pre> |
| 98 |
Generate client certificate (this will generate self-signed certificate; if you have certification authority and want generate certificate request, use keytool -certreq):<br /> |
| 99 |
<pre>keytool -export -alias clientKey -rfc -keystore ./keystore_client > ./client.cert</pre> |
| 100 |
Import client certificate to servers truststore:<br /> |
| 101 |
<pre>keytool -import -alias clientCert -file ./client.cert -keystore ./truststore_server</pre> |
| 102 |
|
| 103 |
These steps are similar for server side: |
| 104 |
<pre>keytool -genkey -keystore ./keystore_server -alias serverKey -dname "CN=localhost, OU=Jersey, O=Oracle, L=Prague, ST=Czech Republic, C=CZ" |
| 105 |
keytool -export -alias serverKey -rfc -keystore ./keystore_server > ./server.cert |
| 106 |
keytool -import -alias serverCert -file ./server.cert -keystore ./truststore_client</pre> |
| 107 |
|
| 108 |
|
| 109 |
<h2>Running the Example</h2> |
| 110 |
<p>Run the example as follows:</p> |
| 111 |
<p> test |
| 112 |
<blockquote><code>mvn test</code></blockquote></p> |
| 113 |
<p> run |
| 114 |
<blockquote><code>mvn compile exec:java</code></blockquote></p> |
| 115 |
<p>From a web browser, visit:<br /> |
| 116 |
<b style="color: red">This won't work! *</b> |
| 117 |
<blockquote><code><a href="https://localhost:4463/">https://localhost:4463/</a></code></blockquote> |
| 118 |
<b style="color: red">[*]</b> Your web browser needs have and use generated |
| 119 |
client keys. Or you have to disable server side client |
| 120 |
authentication - set NeedClientAuth to false: <b>new SSLEngineConfigurator(sslContext).setClientMode(false).setNeedClientAuth(<span style="color:red">false</span>)</b> |
| 121 |
in <i>Server.java</i>. Then ignore any security warning (self-signed certificates |
| 122 |
aren't trusted in general) and login with username "user" and password "password". |
| 123 |
Text "JERSEY HTTPS EXAMPLE" should appear. |
| 124 |
|
| 125 |
<p>Mozila Firefox and Internet Explorer don't allow users to |
| 126 |
display any content provided on behalf of any self-signed certificate so you have to use some other |
| 127 |
browser which allows this (for example Safari or Opera).</p> |
| 128 |
|
| 129 |
|
| 130 |
<h2>References</h2> |
| 131 |
|
| 132 |
<a name="JSSERefGuide"/>[1] <a href="http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html">http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html</a> |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
</body></html> |