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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Defining Security Requirements forWeb Applications<br />

846<br />

@DeclareRoles("BusinessAdmin")<br />

public class CalculatorServlet {<br />

//...<br />

}<br />

Specifying @DeclareRoles("BusinessAdmin") is equivalent to defining the following in<br />

web.xml:<br />

<br />

<br />

BusinessAdmin<br />

<br />

<br />

<strong>The</strong> syntax for declaring more than one role is as shown in the following example:<br />

@DeclareRoles({"Administrator", "Manager", "Employee"})<br />

This annotation is not used to link application roles to other roles. When such linking is<br />

necessary, it is accomplished by defining an appropriate security-role-ref in the associated<br />

deployment descriptor, as described in “Declaring and Linking Role References” on page 842.<br />

When a call is made to isUserInRole from the annotated class, the caller identity associated<br />

with the invocation of the class is tested for membership in the role with the same name as the<br />

argument to isUserInRole.Ifasecurity-role-ref has been defined for the argument<br />

role-name, the caller is tested for membership in the role mapped to the role-name.<br />

For further details on the @DeclareRoles annotation, refer to JSR–250, Common Annotations<br />

for the <strong>Java</strong> Platform (http://www.jcp.org/en/jsr/detail?id=250), and “Using Enterprise<br />

Bean Security Annotations” on page 812 in this tutorial.<br />

Using the @RunAs Annotation<br />

<strong>The</strong> @RunAs annotation defines the role of the application during execution in a <strong>Java</strong> <strong>EE</strong><br />

container. It can be specified on a class, allowing developers to execute an application under a<br />

particular role. <strong>The</strong> role must map to the user/group information in the container’s security<br />

realm. <strong>The</strong> value element in the annotation is the name of a security role of the application<br />

during execution in a <strong>Java</strong> <strong>EE</strong> container. <strong>The</strong> use of the @RunAs annotation is discussed in more<br />

detail in “Propagating Security Identity” on page 810.<br />

<strong>The</strong> following is an example that uses the @RunAs annotation:<br />

@RunAs("Admin")<br />

public class CalculatorServlet {<br />

@EJB private ShoppingCart myCart;<br />

public void doGet(HttpServletRequest, req, HttpServletResponse res) {<br />

//....<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!