This exercise will step through the setup of security authentication and authorization for the EJB Tier and access through an RMI client. We will use simple credentials (username and password) and a simple credential and role store (property files) for the duration of the exercise. Once we have the application updated, integrated, and working with the simple approach -- more advance techniques (e.g., PKI certs, RDBMS or LDAP for credential storage) can be added without impacting the core of the application.
We will be using a built-in security domain that comes with the application server by default. The security domain is powered by a set of property files witin the JBOSS_HOME/standalone/configuration directory. Your server should already be setup with the required accounts and roles but look through the following steps for how that was done or if you ever want to create new accounts for your project(s).
$ cat .../standalone/configuration/application-users.properties ... known=3745b3f6973383c9c11810c7b200b1f4 user1=2dc3eacfed8cf95a4a31159167b936fc admin1=2ae76a0e3f0b615a6229c880555273b5 ...
$ /opt/jboss-as-7.1.1.Final/bin/add-user.sh What type of user do you wish to add? a) Management User (mgmt-users.properties) b) Application User (application-users.properties) (a): b Enter the details of the new user to add. Realm (ApplicationRealm) : Username : admin1 Password : Re-enter Password : What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none) : admin,user About to add user 'admin1' for realm 'ApplicationRealm' Is this correct yes/no? yes Added user 'admin1' to file '/opt/jboss-as-7.1.1.Final/standalone/configuration/application-users.properties' Added user 'admin1' to file '/opt/jboss-as-7.1.1.Final/domain/configuration/application-users.properties' Added user 'admin1' with roles admin,user to file '/opt/jboss-as-7.1.1.Final/standalone/configuration/application-roles.properties' Added user 'admin1' with roles admin,user to file '/opt/jboss-as-7.1.1.Final/domain/configuration/application-roles.properties'
$ cd $JBOSS_HOME
$ cat standalone/configuration/standalone.xml
...
<security-domain name="other" cache-type="default">
<authentication>
<login-module code="Remoting" flag="optional">
<module-option name="password-stacking" value="useFirstPass"/>
</login-module>
<login-module code="RealmUsersRoles" flag="required">
<module-option name="usersProperties" value="${jboss.server.config.dir}/application-users.properties"/>
<module-option name="rolesProperties" value="${jboss.server.config.dir}/application-roles.properties"/>
<module-option name="realm" value="ApplicationRealm"/>
<module-option name="password-stacking" value="useFirstPass"/>
</login-module>
</authentication>
</security-domain> <security-domain name="jboss-web-policy" cache-type="default">
<authorization>
<policy-module code="Delegating" flag="required"/>
</authorization>
</security-domain>
<security-domain name="jboss-ejb-policy" cache-type="default">
<authorization>
<policy-module code="Delegating" flag="required"/>
</authorization>
</security-domain> <management>
<security-realms>
<security-realm name="ManagementRealm">
<authentication>
<properties path="mgmt-users.properties" relative-to="jboss.server.config.dir"/>
</authentication>
</security-realm>
<security-realm name="ApplicationRealm">
<authentication>
<properties path="application-users.properties" relative-to="jboss.server.config.dir"/>
</authentication>
</security-realm>
</security-realms>
...
</management>That ApplicationRealm is the assigned security-realm of the remoting-connector which we use for the RMI communications we have used to date.
<subsystem xmlns="urn:jboss:domain:remoting:1.1">
<connector name="remoting-connector" socket-binding="remoting" security-realm="ApplicationRealm"/>
</subsystem>That is why you needed to add a valid username/password to your jndi.properties file. This allowed you to authenticate access to the server prior to advancing to the individual application.
$ cat javaeeextest/src/test/resources/jndi.properties
...
java.naming.security.principal=${jboss.remoting.java.naming.security.principal}
java.naming.security.credentials=${jboss.remoting.java.naming.security.credentials}
...$ cat javaeeextest/target/test-classes/jndi.properties ... java.naming.security.principal=known java.naming.security.credentials=password1! ...
$ cat javaeeextest/src/test/resources/jndi.properties
...
#java.naming.security.principal=${jboss.remoting.java.naming.security.principal}
#java.naming.security.credentials=${jboss.remoting.java.naming.security.credentials}
...$ mvn clean verify -rf :javaeeExTest Tests in error: testPing(myorg.javaeeex.ejbclient.RegistrarIT): Failed to create remoting connection testCreatePerson(myorg.javaeeex.ejbclient.RegistrarIT): Failed to create remoting connection testLazy(myorg.javaeeex.ejbclient.RegistrarIT): Failed to create remoting connection testPOJO(myorg.javaeeex.ejbclient.RegistrarIT): Failed to create remoting connection testDTOs(myorg.javaeeex.ejbclient.RegistrarIT): Failed to create remoting connection testWebUseCase(myorg.javaeeex.ejbclient.RegistrarIT): Failed to create remoting connection Tests run: 6, Failures: 0, Errors: 6, Skipped: 0 ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE
./javaeeExTest/target/failsafe-reports/myorg.javaeeex.ejbclient.RegistrarIT.txt
::::::::::::::
-------------------------------------------------------------------------------
Test set: myorg.javaeeex.ejbclient.RegistrarIT
-------------------------------------------------------------------------------
Tests run: 6, Failures: 0, Errors: 6, Skipped: 0, Time elapsed: 0.934 sec <<< FAILURE!
testPing(myorg.javaeeex.ejbclient.RegistrarIT) Time elapsed: 0.472 sec <<< ERROR!
javax.naming.NamingException: Failed to create remoting connection [Root exception is java.lang.RuntimeException: javax.security.sasl.SaslException: Au
thentication failed: all available authentication mechanisms failed]
at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:36)
at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:121)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
at javax.naming.InitialContext.init(InitialContext.java:240)
at javax.naming.InitialContext.<init>(InitialContext.java:192)
at myorg.javaeeex.ejbclient.RegistrarIT.setUp(RegistrarIT.java:39)$ cat standalone/configuration/standalone.xml
...
<subsystem xmlns="urn:jboss:domain:remoting:1.1">
<connector name="remoting-connector" socket-binding="remoting"/>
</subsystem>
...$ mvn verify -rf :javaeeExTest ... Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 ... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
$ cat ./javaeeExEJB/src/main/java/myorg/javaeeex/ejb/TestUtilEJB.java
...
import javax.annotation.Resource;
import javax.ejb.SessionContext;
...
@Stateless
public class TestUtilEJB implements TestUtilRemote {
private static Log log = LogFactory.getLog(TestUtilEJB.class);
@Resource
private SessionContext ctx;
...
public void resetAll() throws Exception {
try {
log.debug("caller=" + ctx.getCallerPrincipal().getName());
testUtil.resetAll();$ mvn clean install -rf :javaeeExEJB ... Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 ... [INFO] Java EE Exercise EJB .............................. SUCCESS [6.169s] [INFO] Java EE Exercise WAR .............................. SUCCESS [2.553s] [INFO] Java EE Exercise EAR .............................. SUCCESS [1.302s] [INFO] Java EE Exercise Remote Test ...................... SUCCESS [19.988s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
//SERVER log
20:21:25,024 *** TestUtilEJB:init() ***
20:21:25,052 caller=anonymous
20:21:25,061 found 4 statements
20:21:25,062 executing:
alter table JAVAEEEX_ADDRESS
... <logger category="myorg">
<level name="DEBUG"/>
</logger>$ cat .../standalone/configuration/standalone.xml
...
<subsystem xmlns="urn:jboss:domain:remoting:1.1">
<connector name="remoting-connector" socket-binding="remoting" security-realm="ApplicationRealm"/>
</subsystem>
...$ cat javaeeExTest/src/test/resources/jndi.properties
...
java.naming.security.principal=${jboss.remoting.java.naming.security.principal}
java.naming.security.credentials=${jboss.remoting.java.naming.security.credentials}
...$ mvn clean install ... [INFO] Java EE Exercise .................................. SUCCESS [0.669s] [INFO] Java EE Exercise Impl ............................. SUCCESS [15.644s] [INFO] Java EE Exercise EJB .............................. SUCCESS [4.093s] [INFO] Java EE Exercise WAR .............................. SUCCESS [2.876s] [INFO] Java EE Exercise EAR .............................. SUCCESS [2.317s] [INFO] Java EE Exercise Remote Test ...................... SUCCESS [15.629s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
At this point we are a bit more familiar with the security-domains within the application server and how our application has been interacting with these domains in a default way. We know there is an additional security-domain called "other" that can perform authentication based on username and password credentials and we verified that domain has the accounst we require to continue with the exercise. Now we need to do some things on our application and client sides.
Lets do some quick work to begin locking down the application. The resetAll() method can now be called by any, anonymous user by design. Lets change that design to require the caller to authenticate into the admin role. We don't yet have the ability to authenticate users into a role but we will start by defining which roles the EJBs and methods require.
$ cat ./javaeeExEJB/src/main/java/myorg/javaeeex/ejb/TestUtilEJB.java
...
import javax.annotation.security.RolesAllowed;
...
@RolesAllowed({"admin"})
public void resetAll() throws Exception {$ mvn clean verify -rf :javaeeExEJB -Dit.test=myorg.javaeeex.ejbclient.RegistrarIT#testCreatePerson ...
//SERVER log
20:31:55,006 *** TestUtilEJB:init() ***
20:31:55,012 caller=anonymous
20:31:55,014 found 4 statements
20:31:55,014 executing:
alter table JAVAEEEX_ADDRESS
drop constraint FKEB70B40A6E18CE38
...$ cat javaeeExEJB/src/main/resources/META-INF/jboss-ejb3.xml
<?xml version="1.0"?>
<jboss:ejb-jar
xmlns:jboss="urn:jboss:domain:ejb3:1.2"
xmlns:sec="urn:security"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd
urn:security urn:security"
version="3.1"
impl-version="2.0">
<assembly-descriptor>
<sec:security>
<ejb-name>*</ejb-name>
<sec:security-domain>other</sec:security-domain>
</sec:security>
</assembly-descriptor>
</jboss:ejb-jar>$ mvn clean verify -rf :javaeeExEJB -Dit.test=myorg.javaeeex.ejbclient.RegistrarIT#testPing ... Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.088 sec <<< FAILURE! Results : Tests in error: testPing(myorg.javaeeex.ejbclient.RegistrarIT): JBAS014502: Invocation on method: public abstract void myorg.javaeeex.bl.TestUtil.resetAll() throws java.lang.Exception of bean: TestUtilEJB is not allowed Tests run: 1, Failures: 0, Errors: 1, Skipped: 0 ... [INFO] Java EE Exercise EJB .............................. SUCCESS [5.888s] [INFO] Java EE Exercise WAR .............................. SUCCESS [2.293s] [INFO] Java EE Exercise EAR .............................. SUCCESS [1.234s] [INFO] Java EE Exercise Remote Test ...................... FAILURE [10.527s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE
./javaeeExTest/target/failsafe-reports/myorg.javaeeex.ejbclient.RegistrarIT.txt
::::::::::::::
-------------------------------------------------------------------------------
Test set: myorg.javaeeex.ejbclient.RegistrarIT
-------------------------------------------------------------------------------
Tests run: 6, Failures: 0, Errors: 6, Skipped: 0, Time elapsed: 1.803 sec <<< FAILURE!
testPing(myorg.javaeeex.ejbclient.RegistrarIT) Time elapsed: 1.107 sec <<< ERROR!
javax.ejb.EJBAccessException: JBAS014502: Invocation on method: public abstract void myorg.javaeeex.bl.TestUtil.resetAll() throws java.lang.Exception o
f bean: TestUtilEJB is not allowed
at org.jboss.as.ejb3.security.AuthorizationInterceptor.processInvocation(AuthorizationInterceptor.java:101)At this point we can now protect our methods from being accessed by unauthorized callers. Next we will provide a means to authenticate authorized users.
$ cat javaeeExTest/pom.xml
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>${surefire.argLine}</argLine>
<systemPropertyVariables>
<admin.user>admin1</admin.user>
<admin.password>password1!</admin.password>
</systemPropertyVariables>
</configuration>
</plugin>
...$ cat javaeeExTest/src/test/java/myorg/javaeeex/ejbclient/RegistrarIT.java
public class RegistrarIT {
...
private static final String adminUser = System.getProperty("admin.user", "admin1");
private static final String adminPassword = System.getProperty("admin.password", "password1!");
...
@Before
public void setUp() throws Exception {
...
log.debug(String.format("admin= %s/%s", adminUser, adminPassword));
...
}import java.util.Properties;
import javax.naming.NamingException;
...
private Context runAs(String username, String password) throws NamingException {
if (jndi!=null) {
jndi.close();
}
Properties env = new Properties();
if (username != null) {
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
}
log.debug(String.format("%s env=%s", username==null?"anonymous":username, env));
jndi=new InitialContext(env);
return jndi;
}import javax.naming.Context;
...
private Context jndi; log.debug("getting jndi initial context");
jndi = runAs(null, null);
log.debug("jndi=" + jndi.getEnvironment());
jndi.lookup("/"); //do a quick comms check of JNDI /**
* It is important to close the JNDI context in between tests
*/
@After
public void tearDown() throws NamingException {
if (jndi != null) {
jndi.close();
jndi=null;
}
}$ cat javaeeExTest/src/test/java/myorg/javaeeex/ejbclient/RegistrarIT.java
...
protected void cleanup() throws Exception {
log.info("calling testUtil.resetAll()");
((TestUtilRemote)runAs(adminUser, adminPassword).lookup(testUtilJNDI)).resetAll();
log.info("testUtil.resetAll() complete");
}
...$ mvn clean verify -rf :javaeeExEJB -Dit.test=myorg.javaeeex.ejbclient.RegistrarIT#testPing ... Tests in error: testPing(myorg.javaeeex.ejbclient.RegistrarIT): JBAS014502: Invocation on method: public abstract void myorg.javaeeex.bl.TestUtil.resetAll() throws java.lang.Exception of bean: TestUtilEJB is not allowed Tests run: 1, Failures: 0, Errors: 1, Skipped: 0 ... [INFO] Java EE Exercise EJB .............................. SUCCESS [5.868s] [INFO] Java EE Exercise WAR .............................. SUCCESS [2.353s] [INFO] Java EE Exercise EAR .............................. SUCCESS [1.426s] [INFO] Java EE Exercise Remote Test ...................... FAILURE [12.136s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE
$ mvn clean verify -rf :javaeeExEJB -Dit.test=myorg.javaeeex.ejbclient.RegistrarIT#testPing ...
//SERVER log 12:21:44,265 *** TestUtilEJB:init() *** 12:21:44,272 caller=known 12:21:44,274 found 4 statements
...
#java.naming.security.principal=${jboss.remoting.java.naming.security.principal}
#java.naming.security.credentials=${jboss.remoting.java.naming.security.credentials}
...$ cat javaeeExTest/pom.xml
...
<systemPropertyVariables>
<known.user>known</known.user>
<known.password>password1!</known.password>
<admin.user>admin1</admin.user>
<admin.password>password1!</admin.password>
</systemPropertyVariables>
...$ cat javaeeExTest/src/test/java/myorg/javaeeex/ejbclient/RegistrarIT.java
...
private static final String knownUser = System.getProperty("known.user", "known");
private static final String knownPassword = System.getProperty("known.password", "password1!");
...$ cat javaeeExTest/src/test/java/myorg/javaeeex/ejbclient/RegistrarIT.java
...
private Context runAs() throws NamingException {
return runAs(knownUser, knownPassword);
}
...$ cat javaeeExTest/src/test/java/myorg/javaeeex/ejbclient/RegistrarIT.java
...
log.debug("getting jndi initial context");
jndi = runAs();
log.debug("jndi=" + jndi.getEnvironment());
jndi.lookup("/"); //do a quick comms check of JNDI
...
protected void cleanup() throws Exception {
log.info("calling testUtil.resetAll()");
((TestUtilRemote)runAs(adminUser, adminPassword).lookup(testUtilJNDI)).resetAll();
log.info("testUtil.resetAll() complete");
}$ mvn clean verify -rf :javaeeExEJB -Dit.test=myorg.javaeeex.ejbclient.RegistrarIT#testPing ...
//SERVER log 12:44:54,229 *** TestUtilEJB:init() *** 12:44:54,232 caller=admin1 12:44:54,233 found 4 statements
$ cat javaeeExEJB/src/main/java/myorg/javaeeex/ejb/TestUtilEJB.java
...
@RolesAllowed({"admin"})
public void resetAll() throws Exception {$ mvn clean install -rf :javaeeExEJB -Dit.test=myorg.javaeeex.ejbclient.RegistrarIT#testPing ... [INFO] Java EE Exercise EJB .............................. SUCCESS [6.067s] [INFO] Java EE Exercise WAR .............................. SUCCESS [2.389s] [INFO] Java EE Exercise EAR .............................. SUCCESS [1.692s] [INFO] Java EE Exercise Remote Test ...................... SUCCESS [12.944s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
$ cat javaeeExTest/src/test/java/myorg/javaeeex/ejbclient/RegistrarIT.java
...
protected void cleanup() throws Exception {
...
runAs(adminUser, "badpass");
...
}
...$ mvn clean verify -rf :javaeeExEJB -Dit.test=myorg.javaeeex.ejbclient.RegistrarIT#testPing
...
-admin1 env={java.naming.security.principal=admin1, java.naming.security.credentials=badpass}
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.06 sec <<< FAILURE!
Results :
Tests in error:
testPing(myorg.javaeeex.ejbclient.RegistrarIT): Failed to create remoting connection
...
[INFO] BUILD FAILURE./javaeeExTest/target/failsafe-reports/myorg.javaeeex.ejbclient.RegistrarIT.txt
::::::::::::::
-------------------------------------------------------------------------------
Test set: myorg.javaeeex.ejbclient.RegistrarIT
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.06 sec <<< FAILURE!
testPing(myorg.javaeeex.ejbclient.RegistrarIT) Time elapsed: 0.771 sec <<< ERROR!
javax.naming.NamingException: Failed to create remoting connection [Root exception is java.lang.RuntimeException: javax.security.sasl.SaslException: Au
thentication failed: all available authentication mechanisms failed]
at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:36)
at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:121)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
at javax.naming.InitialContext.init(InitialContext.java:240)
at javax.naming.InitialContext.<init>(InitialContext.java:214)
at myorg.javaeeex.ejbclient.RegistrarIT.runAs(RegistrarIT.java:82)
at myorg.javaeeex.ejbclient.RegistrarIT.cleanup(RegistrarIT.java:66) protected void cleanup() throws Exception {
...
runAs(adminUser, adminPassword);
...
}$ mvn clean install ... Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.505 sec Results : Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] [INFO] --- cargo-maven2-plugin:1.2.3:undeploy (cargo-post) @ javaeeExTest --- [INFO] [INFO] --- maven-failsafe-plugin:2.12.2:verify (verify) @ javaeeExTest --- ... [INFO] Java EE Exercise .................................. SUCCESS [0.761s] [INFO] Java EE Exercise Impl ............................. SUCCESS [17.815s] [INFO] Java EE Exercise EJB .............................. SUCCESS [4.466s] [INFO] Java EE Exercise WAR .............................. SUCCESS [3.088s] [INFO] Java EE Exercise EAR .............................. SUCCESS [1.159s] [INFO] Java EE Exercise Remote Test ...................... SUCCESS [16.695s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
Once you completed the previous steps you have all the mechanics down for adding authentication, access control, and authorization to your server and application. In this section we will just add breadth to the overall implementation.
$ cat javaeeExEJB/src/main/java/myorg/javaeeex/ejb/RegistrarEJB.java
...
import javax.annotation.security.RolesAllowed;
...
@Stateless
@RolesAllowed({"user"})
public class RegistrarEJB implements RegistrarLocal, RegistrarRemote {$ mvn install -rf :javaeeExEJB Tests run: 6, Failures: 0, Errors: 6, Skipped: 0, Time elapsed: 2.959 sec <<< FAILURE! Results : Tests in error: testPing(myorg.javaeeex.ejbclient.RegistrarIT): JBAS014502: Invocation on method: public abstract void myorg.javaeeex.ejb.RegistrarRemote.ping() of bean: RegistrarEJB is not allowed ... Tests run: 6, Failures: 0, Errors: 6, Skipped: 0 ... [INFO] Java EE Exercise EJB .............................. SUCCESS [6.078s] [INFO] Java EE Exercise WAR .............................. SUCCESS [2.523s] [INFO] Java EE Exercise EAR .............................. SUCCESS [2.165s] [INFO] Java EE Exercise Remote Test ...................... FAILURE [14.380s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE
$ cat javaeeExEJB/src/main/java/myorg/javaeeex/ejb/RegistrarEJB.java
...
import javax.annotation.security.PermitAll;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
...
@Resource
protected SessionContext ctx;
...
@PermitAll
public void ping() {
log.debug("ping called");
log.debug("caller=" + ctx.getCallerPrincipal().getName());
}$ mvn install -rf :javaeeExEJB ... Tests run: 6, Failures: 0, Errors: 5, Skipped: 0, Time elapsed: 3.216 sec <<< FAILURE! Results : Tests in error: testCreatePerson(myorg.javaeeex.ejbclient.RegistrarIT): JBAS014502: Invocation on method: public abstract myorg.javaeeex.bo.Person myorg.javaeeex.ejb.RegistrarRemote.createPerson(myorg.javaeeex.bo.Person) throws myorg.javaeeex.bl.RegistrarException of bean: RegistrarEJB is not allowed testLazy(myorg.javaeeex.ejbclient.RegistrarIT): JBAS014502: Invocation on method: public abstract myorg.javaeeex.bo.Person myorg.javaeeex.ejb.RegistrarRemote.createPerson(myorg.javaeeex.bo.Person) throws myorg.javaeeex.bl.RegistrarException of bean: RegistrarEJB is not allowed testPOJO(myorg.javaeeex.ejbclient.RegistrarIT): JBAS014502: Invocation on method: public abstract myorg.javaeeex.bo.Person myorg.javaeeex.ejb.RegistrarRemote.createPerson(myorg.javaeeex.bo.Person) throws myorg.javaeeex.bl.RegistrarException of bean: RegistrarEJB is not allowed testDTOs(myorg.javaeeex.ejbclient.RegistrarIT): JBAS014502: Invocation on method: public abstract myorg.javaeeex.bo.Person myorg.javaeeex.ejb.RegistrarRemote.createPerson(myorg.javaeeex.bo.Person) throws myorg.javaeeex.bl.RegistrarException of bean: RegistrarEJB is not allowed testWebUseCase(myorg.javaeeex.ejbclient.RegistrarIT): JBAS014502: Invocation on method: public abstract myorg.javaeeex.bo.Person myorg.javaeeex.ejb.RegistrarRemote.createPerson(myorg.javaeeex.bo.Person) throws myorg.javaeeex.bl.RegistrarException of bean: RegistrarEJB is not allowed Tests run: 6, Failures: 0, Errors: 5, Skipped: 0
//SERVER LOG 19:16:32,007 **** init **** 19:16:32,007 init complete, registrar=myorg.javaeeex.blimpl.RegistrarImpl@15e6726 19:16:32,008 ping called 19:16:32,008 caller=known
$ cat javaeeExTest/pom.xml
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
...
<user.user>user1</user.user>
<user.password>password1!</user.password>
... $ cat javaeeExTest/src/test/java/myorg/javaeeex/ejbclient/RegistrarIT.java
...
private static final String userUser = System.getProperty("user.user","user1");
private static final String userPassword = System.getProperty("user.password", "password1!");
...$ cat javaeeExTest/src/test/java/myorg/javaeeex/ejbclient/RegistrarIT.java
...
@Before
public void setUp() throws Exception {
...
cleanup();
//run the tests as user
Context context=runAs(userUser, userPassword);
registrar=(RegistrarRemote)context.lookup(registrarJNDI);
}
...$ mvn install -rf :javaeeExEJB ... Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 ... [INFO] Java EE Exercise EJB .............................. SUCCESS [6.313s] [INFO] Java EE Exercise WAR .............................. SUCCESS [2.219s] [INFO] Java EE Exercise EAR .............................. SUCCESS [1.766s] [INFO] Java EE Exercise Remote Test ...................... SUCCESS [18.469s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS
At this point you can experiment with the remaining methods and user identities. For example, it may be interesting to use a user with multiple roles to verify they work across roles as well as users with no roles to verify a user with a valid login will be rejected if they do not have the right roles.
In this exercise we secured an EJB according to an application policy and enabled access to authorized users. More specifically we ...
Next steps should be to extend the security implementation to the web tier because at this point the web tier will not longer be able to access the EJB's role-constrained methods.
The following is an overview of the primary modules accessed during this exercise.
|-- javaeeExImpl |-- javaeeExEJB | |-- pom.xml | `-- src | `-- main | |-- java | | `-- myorg | | `-- javaeeex | | |-- cdi | | | `-- ResourceConfig.java | | |-- dto | | | |-- AddressDTO.java | | | `-- PersonDTO.java | | `-- ejb | | |-- RegistrarEJB.java | | |-- RegistrarLocal.java | | |-- RegistrarRemote.java | | |-- TestUtilEJB.java | | `-- TestUtilRemote.java | `-- resources | `-- META-INF | |-- beans.xml | |-- jboss-ejb3.xml | `-- persistence.xml |-- javaeeExWAR |-- javaeeExEAR |-- javaeeExTest | |-- pom.xml | `-- src | `-- test | |-- java | | `-- myorg | | `-- javaeeex | | `-- ejbclient | | `-- RegistrarIT.java | `-- resources | |-- jndi.properties | `-- log4j.xml `-- pom.xml