This simple tutorial takes the solution from EJB3_Tutorial_5_Message_Driven_Beans and augments the beans with the configuration information necessary to limit access declaratively.
Project Setup
We recommend you create a copy of your project (or if you are using revision control software, make sure to check-in and tag your work).
We need to create a few basic files: users.properties and roles.properties.
users.properties
This file defines user accounts. Note that while the use of this information is defined in the specification, exactly how it is configured is vendor specific.
This file should reside anywhere in the root of a classpath entry. Place this in the conf directory, which is configured as a source entry.
By the way, notice that it is users and not user. You can use another name, but this is the default name JBoss uses.
roles.properties
The comments from users.properties apply here.
Update Session Bean
Next we need to configure the bean with security information. As usual, we can use either XML or annotations. Here is an updated version of AccountInventoryBean.java:
AccountInventoryBean.java
and the updated interface:
AccountInventory.java
Updated JBossUtil
We now need to update JBossUtil once more to read our security properties. The only method that has changed is startDeployer:
This method now reads in MDB configuration information in the first two calls to deployXmlResources and configures the security settings in the third line.
The Test
This test attempts one successful and four failed attempts. The names of the methods describe whether we expect success or failure:
AccountInventoryBeanTest.java
Notice that in all cases where the method is expected to generate an exception, we first catch EJBAccessException. EJBAccessException is a wrapper exception. We verify the contents by getting the wrapped exception and throwing it. We then let JUnit tell us if we got the exception we expected.
Questions
If you do not provide a RolesAllowed, what is the default?
How do you set a different default value for all the methods in a class?
Comments