| 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><title>HTTPS Server GlassFish Example</title></head> |
| 44 |
|
| 45 |
<body> |
| 46 |
<h1>HTTPS Server Example - GlassFish</h1> |
| 47 |
|
| 48 |
<p>This example demonstrates how to develop secure RESTful hello world web service with a Servlet 2.5 container.</p> |
| 49 |
|
| 50 |
<h2>Contents</h2> |
| 51 |
<p>The example consists of just one Java class:</p> |
| 52 |
<dl> |
| 53 |
<dt><code>com.sun.jersey.samples.helloworldwebapp.resources.HelloWorldResource</code></dt> |
| 54 |
<dd>A resource class that produces a textual response to an HTTP GET.</dd> |
| 55 |
</dl> |
| 56 |
<p>The mapping of the URI path space is presented in the following table:</p> |
| 57 |
<table border="1"> |
| 58 |
<tr> |
| 59 |
<th>URI path</th> |
| 60 |
<th>Resource class</th> |
| 61 |
<th>HTTP methods</th> |
| 62 |
</tr> |
| 63 |
<tr> |
| 64 |
<td>/helloworld</td> |
| 65 |
<td>HelloWorldResource</td> |
| 66 |
<td>GET</td> |
| 67 |
</tr> |
| 68 |
</table> |
| 69 |
|
| 70 |
<h2>Running the Example</h2> |
| 71 |
<p>Run the example as follows:</p> |
| 72 |
<p> create war package</p> |
| 73 |
<blockquote><code>mvn clean package</code><p></blockquote> |
| 74 |
|
| 75 |
<p> setup GlassFish 3.1 (installed at <code>AS_HOME=<install_dir>/glassfish</code>): |
| 76 |
<ul> |
| 77 |
<li>Add new security realm called "<code>myRealm</code>"</li> |
| 78 |
<ul> |
| 79 |
<li>Start GlassFish using asadmin <code>$AS_HOME/bin/asadmin start-domain</code> </li> |
| 80 |
<li>Access Admin console, assuming the use of default admin port, here <a href="http://localhost:4848">http://localhost:4848</a></li> |
| 81 |
<li>Navigate in Admin console to: <code>Configurations -> server-config -> Security -> Realms</code>, click on "<code>New...</code>"</li> |
| 82 |
<table border="1px"> |
| 83 |
<tr><th>Name</th><td><code>myRealm</code></td></tr> |
| 84 |
<tr><th>Class Name</th><td><code>com.sun.enterprise.security.auth.realm.file.FileRealm</code></td></tr> |
| 85 |
<tr><th>JAAS context</th><td><code>fileRealm</code></td></tr> |
| 86 |
<tr><th>Key File</th><td><code>${com.sun.aas.instanceRoot}/config/myRealm-keyfile</code></td></tr> |
| 87 |
</table> |
| 88 |
<li>Create user and set password for user <code>myUser</code> (click on "<code>myRealm</code>", "<code>Manage Users</code>", "<code>New...</code>"); |
| 89 |
<ul> |
| 90 |
<li><strong>Ensure</strong> "<code>User ID:</code>" set to "<code>myUser</code>" <strong>and</strong> "<code>Group List:</code>" set to "<code>Users</code>"</li> |
| 91 |
<li>Enter password <strong><code>myPass</code></strong> in "<code>New Password:</code>" and "<code>Confirm New Password:</code>" text fields</li> |
| 92 |
</ul> |
| 93 |
</li> |
| 94 |
<li>Add one other user name <code>joeUser</code> (click on "<code>myRealm</code>", "<code>Manage Users</code>", "<code>New...</code>"); |
| 95 |
<ul> |
| 96 |
<li><strong>Ensure</strong> "<code>User ID:</code>" set to "<code>joeUser</code>" <strong>and</strong> "<code>Group List:</code>" set to "<code>Users</code>"</li> |
| 97 |
<li>Enter password <strong><code>joePass</code></strong> in "<code>New Password:</code>" and "<code>Confirm New Password:</code>" text fields</li> |
| 98 |
</ul> |
| 99 |
</ul> |
| 100 |
<li>Check <code>web.xml</code> and <code>sun-web.xml</code> to understand how role mappings are configured. This blog is also helpful to clarify role mappings <a href="http://blogs.oracle.com/bobby/entry/simplified_security_role_mapping">http://blogs.oracle.com/bobby/entry/simplified_security_role_mapping</a> </li> |
| 101 |
<li>Deploy <code>httpsBasicAuth-webapp.war</code> as a Web Application</li> |
| 102 |
<ul> |
| 103 |
<li>Admin console: select <code>Applications</code>, click on "<code>Deploy...</code>". Select file <code>./target/httpsBasicAuth-webapp.war</code></li> |
| 104 |
<li>or you can use command line: <code>$AS_HOME/bin/asadmin deploy ./target/httpsBasicAuth-webapp.war</code></li> |
| 105 |
</ul> |
| 106 |
|
| 107 |
</ul> |
| 108 |
|
| 109 |
<p>From a web browser, visit:</p> |
| 110 |
<blockquote><code><a href="https://localhost:8181/httpsBasicAuth-webapp/helloworld">https://localhost:8181/httpsBasicAuth-webapp/helloworld</a></code> |
| 111 |
(you have to add security exception for GlassFish server certificate / ignore all warnings about security) |
| 112 |
</blockquote> |
| 113 |
<ul> |
| 114 |
<li>Enter either Username <code>myUser</code> and Password <code>myPass</code> or, Username |
| 115 |
<code>joeUser</code> and Password <code>joePass</code></li> |
| 116 |
<li> If entered Username <code>myUser</code> and Password <code>myPass</code>, verify that <b>expected |
| 117 |
output</b>: <code>Sending "Hello World" to user "myUser"</code> was displayed |
| 118 |
</li> |
| 119 |
<li> If entered Username <code>joeUser</code> and Password <code>joePass</code>, verify that <b>expected |
| 120 |
output</b>: <code>Sending "Hello World" to user "joeUser"</code> was displayed |
| 121 |
</li> |
| 122 |
</ul> |
| 123 |
|
| 124 |
<h2>Running the Negative Example</h2> |
| 125 |
|
| 126 |
<p>Run the example as follows:</p> |
| 127 |
<p> create war package</p> |
| 128 |
<blockquote><code>mvn clean package</code><p></blockquote> |
| 129 |
|
| 130 |
<p>setup GlassFish 3.1 (installed at <code>AS_HOME=<install_dir>/glassfish</code>):</p> |
| 131 |
<ul> |
| 132 |
<li> |
| 133 |
Follow the steps described in the "<strong>setup GlassFish 3.1</strong>" section from the previous chapter "<strong>Running the Example</strong>" |
| 134 |
but instead of adding the user "<code>joeUser</code>" to the "<code>Group List:</code>" of "<code>Users</code>" set |
| 135 |
"<code>Group List:</code>" for this user to "<code>OtherUsers</code>" |
| 136 |
</li> |
| 137 |
</ul> |
| 138 |
|
| 139 |
<p>From a web browser, visit:</p> |
| 140 |
<blockquote><code><a href="https://localhost:8181/httpsBasicAuth-webapp/helloworld">https://localhost:8181/httpsBasicAuth-webapp/helloworld</a></code> |
| 141 |
(you have to add security exception for GlassFish server certificate / ignore all warnings about security) |
| 142 |
</blockquote> |
| 143 |
<ul> |
| 144 |
<li>Enter Username <code>joeUser</code> and Password <code>joePass</code>, verify that <b>expected |
| 145 |
output</b> looks like: <code>HTTP Status 403 - Access to the requested resource has been denied</code> |
| 146 |
</li> |
| 147 |
</ul> |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
</body></html> |
| 152 |
|
| 153 |
|
| 154 |
<!-- |
| 155 |
|
| 156 |
Resources: |
| 157 |
http://java.sun.com/developer/technicalArticles/J2EE/security_annotation/ |
| 158 |
http://www.ryandelaplante.com/rdelaplante/entry/ssl_and_http_basic_authentication |
| 159 |
|
| 160 |
--> |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|