Enterprise Java Development@TOPIC@
JBoss Remoting and JNDI InitialContext
EJBClient and JNDI InitialContext
Changing Users
Access Violations
Access Granted
protected String[] userLogin = new String[] { userUser, userPassword };
protected String[] adminLogin = new String[] { adminUser, adminPassword };
jndi=runAs(userLogin);
ejb=(SecurePing)jndi.lookup(jndiName);
assertFalse("user in admin role", ejb.isCallerInRole("admin"));
assertTrue("user not in user role", ejb.isCallerInRole("user"));
jndi.close();
jndi=runAs(adminLogin);
ejb=(SecurePing)jndi.lookup(jndiName);
assertTrue("admin not in admin role", ejb.isCallerInRole("admin"));
assertTrue("admin not in user role", ejb.isCallerInRole("user"));
jndi.close();
protected static String[] currentLogin;
private void runAs(String[] login) throws NamingException, IOException {
if (!Arrays.equals(login, currentLogin) || securePing==null) {
Properties props = new Properties(); //initialize with values from jndi.properties
if (login!=null) {
props.put(Context.SECURITY_PRINCIPAL, login[0]);
props.put(Context.SECURITY_CREDENTIALS, login[1]);
}
InitialContext jndi = null;
try {
jndi = new InitialContext(props);
securePing = (SecurePingRemote)jndi.lookup(jndiName);
currentLogin = login;
} finally {
if (jndi!=null) { jndi.close(); }
}
}
}
protected String[] userLogin = new String[] { userUser, userPassword };
protected String[] adminLogin = new String[] { adminUser, adminPassword };
runAs(userLogin);
assertFalse("user in admin role", securePing.isCallerInRole("admin"));
assertTrue("user not in user role", securePing.isCallerInRole("user"));
runAs(adminLogin);
assertTrue("admin not in admin role", securePing.isCallerInRole("admin"));
assertTrue("admin not in user role", securePing.isCallerInRole("user"));
@Stateless
public class SecurePingEJB implements SecurePingRemote, SecurePingLocal {
@Resource
SessionContext ctx;
Context jndi = new InitialContext();
logger.debug("looking up jndi.name={}", jndiName);
securePing = (SecurePingRemote)jndi.lookup(jndiName);
try {
runAs(userLogin);
logger.info(securePing.pingAdmin());
fail("didn't detect non-admin user");
}
catch (EJBAccessException ex) {
logger.info("expected exception thrown:" + ex);
}
-looking up jndi.name=ejb:securePingEAR/securePingEJB/SecurePingEJB !info.ejava.examples.secureping.ejb.SecurePingRemote as user1 -found=Proxy for remote EJB StatelessEJBLocator for "securePingEAR/securePingEJB/SecurePingEJB", view is interface info.ejava.examples.secureping.ejb.SecurePingRemote, affinity is None -login=[user1, password1!], whoAmI=user1 -expected exception thrown:javax.ejb.EJBAccessException: WFLYEJB0364: Invocation on method: public abstract java.lang.String info.ejava.examples.secureping.ejb.SecurePing.pingAdmin() of bean: SecurePingEJB is not allowed
runAs(adminLogin);
logger.info(securePing.pingAdmin());
-looking up jndi.name=ejb:securePingEAR/securePingEJB/SecurePingEJB !info.ejava.examples.secureping.ejb.SecurePingRemote as admin1 -found=Proxy for remote EJB StatelessEJBLocator for "securePingEAR/securePingEJB/SecurePingEJB", view is interface info.ejava.examples.secureping.ejb.SecurePingRemote, affinity is None -login=[admin1, password1!], whoAmI=admin1 -called pingAdmin, principal=admin1, isUser=true, isAdmin=true, isInternalRole=true