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.

applications for annotations that specify deployment information. When the<br />

metadata-complete attribute is set to true, the deployment descriptor ignores any servlet<br />

annotations present in the class files of the application. Thus, deployers can use deployment<br />

descriptors to customize or override the values specified in annotations.<br />

Many elements for security in a web application deployment descriptor cannot, as yet, be<br />

specified as annotations, therefore, for securing web applications, deployment descriptors are a<br />

necessity. However, where possible, annotations are the recommended method for securing<br />

web components.<br />

<strong>The</strong> following sections discuss the use of annotations and deployment descriptor elements to<br />

secure web applications:<br />

■ “Declaring Security Requirements Using Annotations” on page 845<br />

■ “Declaring Security Requirements in a Deployment Descriptor” on page 847<br />

Declaring Security Requirements Using Annotations<br />

<strong>The</strong> <strong>Java</strong> Metadata Specification (JSR-175), which is part of J2SE 5.0 and greater, provides a<br />

means of specifying configuration data in <strong>Java</strong> code. Metadata in <strong>Java</strong> code is more commonly<br />

referred to in this document as annotations. In <strong>Java</strong> <strong>EE</strong>, annotations are used to declare<br />

dependencies on external resources and configuration data in <strong>Java</strong> code without the need to<br />

define that data in a configuration file. Several common annotations are specific to specifying<br />

security in any <strong>Java</strong> application. <strong>The</strong>se common annotations are specified in JSR-175, A<br />

Metadata Facility for the <strong>Java</strong> Programming Language (http://www.jcp.org/en/jsr/<br />

detail?id=175), and JSR-250, Common Annotations for the <strong>Java</strong> Platform<br />

(http://www.jcp.org/en/jsr/detail?id=250). Annotations specific to web components are<br />

specified in the <strong>Java</strong> Servlet 2.5 Specification (http://www.jcp.org/en/jsr/detail?id=154).<br />

In servlets, you can use the annotations discussed in the following sections to secure a web<br />

application:<br />

■ “Using the @DeclareRoles Annotation” on page 845<br />

■ “Using the @RunAs Annotation” on page 846<br />

Using the @DeclareRoles Annotation<br />

Defining Security Requirements forWeb Applications<br />

This annotation is used to define the security roles that comprise the security model of the<br />

application. This annotation is specified on a class, and it typically would be used to define roles<br />

that could be tested (for example, by calling isUserInRole) from within the methods of the<br />

annotated class.<br />

Following is an example of how this annotation would be used. In this example, BusinessAdmin<br />

is the only security role specified, but the value of this parameter can include a list of security<br />

roles specified by the application.<br />

Chapter 30 • SecuringWeb Applications 845

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

Saved successfully!

Ooh no, something went wrong!