This example demonstrates how to develop secure RESTful hello world web service with a Servlet 2.5 container.
The example consists of just one Java class:
com.sun.jersey.samples.helloworldwebapp.resources.HelloWorldResourceThe mapping of the URI path space is presented in the following table:
| URI path | Resource class | HTTP methods |
|---|---|---|
| /helloworld | HelloWorldResource | GET |
Run the example as follows:
create war package
mvn clean package
setup GlassFish 3.1 (installed at AS_HOME=<install_dir>/glassfish):
myRealm"$AS_HOME/bin/asadmin start-domain Configurations -> server-config -> Security -> Realms, click on "New..."| Name | myRealm |
|---|---|
| Class Name | com.sun.enterprise.security.auth.realm.file.FileRealm |
| JAAS context | fileRealm |
| Key File | ${com.sun.aas.instanceRoot}/config/myRealm-keyfile |
myUser (click on "myRealm", "Manage Users", "New...");
User ID:" set to "myUser" and "Group List:" set to "Users"myPass in "New Password:" and "Confirm New Password:" text fieldsjoeUser (click on "myRealm", "Manage Users", "New...");
User ID:" set to "joeUser" and "Group List:" set to "Users"joePass in "New Password:" and "Confirm New Password:" text fieldsweb.xml and sun-web.xml to understand how role mappings are configured. This blog is also helpful to clarify role mappings http://blogs.oracle.com/bobby/entry/simplified_security_role_mapping httpsBasicAuth-webapp.war as a Web ApplicationApplications, click on "Deploy...". Select file ./target/httpsBasicAuth-webapp.war$AS_HOME/bin/asadmin deploy ./target/httpsBasicAuth-webapp.warFrom a web browser, visit:
https://localhost:8181/httpsBasicAuth-webapp/helloworld
(you have to add security exception for GlassFish server certificate / ignore all warnings about security)
myUser and Password myPass or, Username
joeUser and Password joePassmyUser and Password myPass, verify that expected
output: Sending "Hello World" to user "myUser" was displayed
joeUser and Password joePass, verify that expected
output: Sending "Hello World" to user "joeUser" was displayed
Run the example as follows:
create war package
mvn clean package
setup GlassFish 3.1 (installed at AS_HOME=<install_dir>/glassfish):
joeUser" to the "Group List:" of "Users" set
"Group List:" for this user to "OtherUsers"
From a web browser, visit:
https://localhost:8181/httpsBasicAuth-webapp/helloworld
(you have to add security exception for GlassFish server certificate / ignore all warnings about security)
joeUser and Password joePass, verify that expected
output looks like: HTTP Status 403 - Access to the requested resource has been denied