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.

Securing Enterprise Beans<br />

800<br />

You declare the security roles referenced in the code using the @DeclareRoles annotation.<br />

When declaring the name of a role used as a parameter to the isCallerInRole(String<br />

roleName) method, the declared name must be the same as the parameter value. You can<br />

optionally provide a description of the named security roles in the description element of the<br />

@DeclareRoles annotation.<br />

<strong>The</strong> following code snippet demonstrates the use of the @DeclareRoles annotation. In this<br />

example, the @DeclareRoles annotation indicates that the enterprise bean AardvarkPayroll<br />

makes the security check using isCallerInRole("payroll") to verify that the caller is<br />

authorized to change salary data. <strong>The</strong> security role reference is scoped to the session or entity<br />

bean whose declaration contains the @DeclareRoles annotation.<br />

@DeclareRoles("payroll")<br />

@Stateless public class PayrollBean implements Payroll {<br />

@Resource SessionContext ctx;<br />

}<br />

public void updateEmployeeInfo(EmplInfo info) {<br />

}<br />

...<br />

oldInfo = ... read from database;<br />

// <strong>The</strong> salary field can be changed only by callers<br />

// who have the security role "payroll"<br />

if (info.salary != oldInfo.salary &&<br />

!ctx.isCallerInRole("payroll")) {<br />

throw new SecurityException(...);<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 />

Declaring Security Roles Using Deployment Descriptor Elements<br />

Note – Any values explicitly specified in the deployment descriptor override any values specified<br />

in annotations. If a value for a method has not been specified in the deployment descriptor, and<br />

a value has been specified for that method by means of the use of annotations, the value<br />

specified in annotations will apply. <strong>The</strong> granularity of overriding is on the per-method basis.<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!