10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Enterprise Bean Example Applications<br />

818<br />

Example: Securing an Enterprise Bean<br />

This section discusses how to configure an enterprise bean for username-password<br />

authentication. When a bean that is constrained in this way is requested, the server requests a<br />

user name and password from the client and verifies that the user name and password are valid<br />

by comparing them against a database of authorized users on the Application Server.<br />

If the topic of authentication is new to you, please refer to the section titled “Specifying an<br />

Authentication Mechanism” on page 854.<br />

For this tutorial, you will add the security elements to an enterprise bean; add security elements<br />

to the deployment descriptors; build, package, and deploy the application; and then build and<br />

run the client application.<br />

<strong>The</strong> completed version of this example can be found at<br />

tut-install/javaeetutorial5/examples/ejb/cart-secure/. This example was developed by<br />

starting with the unsecured enterprise bean application, cart, which is found in the directory<br />

tut-install/javaeetutorial5/examples/ejb/cart/ and is discussed in “<strong>The</strong> cart Example”<br />

on page 655. You build on this example by adding the necessary elements to secure the<br />

application using username-password authentication.<br />

In general, the following steps are necessary to add username-password authentication to an<br />

enterprise bean. In the example application included with this tutorial, many of these steps have<br />

been completed for you and are listed here simply to show what needs to be done should you<br />

wish to create a similar application.<br />

1. Create an application like the one in “<strong>The</strong> cart Example” on page 655. <strong>The</strong> example in this<br />

tutorial starts with this example and demonstrates adding basic authentication of the client<br />

to this application. <strong>The</strong> example application discussed in this section can be found at<br />

tut-install/javaeetutorial5/examples/ejb/cart-secure/.<br />

2. If you have not already done so, complete the steps in “Building the Examples” on page 69 to<br />

configure your system properly for running the tutorial applications.<br />

3. If you have not already done so, add a user to the file realm and specify user for the group<br />

of this new user. Write down the user name and password so that you can use them for<br />

testing this application in a later step. Refer to the section “Managing Users and Groups on<br />

the Application Server” on page 779 for instructions on completing this step.<br />

4. Modify the source code for the enterprise bean, CartBean.java, to specify which roles are<br />

authorized to access which protected methods. This step is discussed in “Annotating the<br />

Bean” on page 819.<br />

5. Modify the runtime deployment descriptor, sun-ejb-jar.xml, to map the role used in this<br />

application (CartUser) to a group defined on the Application Server (user) and to add<br />

security elements that specify that username-password authentication is to be performed.<br />

This step is discussed in “Setting Runtime Properties” on page 820.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!