16.10.2015 Views

Getting Started with WebSphere Application Server

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

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

150 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>WebSphere</strong> <strong>Application</strong> <strong>Server</strong> Community Edition<br />

Declarative security can restrict access to EJBs through annotations in the EJB class or through<br />

deployment descriptors based on roles in the EJB application. Listing 8.4 provides an example where<br />

annotation is added before the EJB implementation class definition to declare the roles that can access<br />

the class.<br />

@DeclareRoles( { "general_user ","super_user" })<br />

public class HelloBean implements HelloWorld {<br />

…<br />

Listing 8.4 - Declare the roles before EJB class definition<br />

Listing 8.5 below shows the lines you need to insert before the methods you want to restrict.<br />

@RolesAllowed( { "general_user ","super_user" })<br />

public string sayHello(string targetName){<br />

…<br />

@RolesAllowed( { "super_user" })<br />

public string sayByebye(string targetName) {<br />

…<br />

Listing 8.5 - Restrict roles before methods<br />

This means that both, the general user and the super user can say “Hello”, but only the super user has<br />

the rights to say “Bye bye”.<br />

The above annotation statements could be replaced by adding the following lines in EJB’s deployment<br />

descriptor (ejb-jar.xml), as shown in Listing 8.6.<br />

<br />

<br />

super_user<br />

<br />

<br />

general_user<br />

<br />

<br />

super_user<br />

general_user<br />

<br />

HelloBean<br />

sayHello<br />

<br />

<br />

<br />

super_user<br />

<br />

HelloBean<br />

sayByebye<br />

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

Saved successfully!

Ooh no, something went wrong!