1 22 package org.jboss.ejb3.test.security; 23 24 import java.rmi.RemoteException ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import javax.ejb.Remote ; 28 import javax.ejb.SessionContext ; 29 import javax.ejb.Stateless ; 30 import javax.naming.InitialContext ; 31 32 import javax.annotation.Resource; 33 import javax.annotation.security.PermitAll; 34 35 import org.jboss.annotation.ejb.RemoteBinding; 36 import org.jboss.annotation.security.SecurityDomain; 37 import org.jboss.ejb3.Container; 38 import org.jboss.logging.Logger; 39 40 44 @Stateless (name="UserInRoleContextSessionTarget") 45 @Remote (org.jboss.ejb3.test.security.SecurityContext.class) 46 @RemoteBinding(jndiBinding = "spec.UserInRoleContextSessionTarget") 47 @SecurityDomain("spec-test-domain2") 48 @PermitAll 49 public class SecurityContextTargetBean 50 { 51 static Logger log = Logger.getLogger(SecurityContextTargetBean.class); 52 53 @Resource SessionContext sessionContext; 54 55 public void testDomainInteraction(Set expectedRoles) 56 { 57 validateRoles(expectedRoles, true); 59 try 61 { 62 InitialContext ctx = new InitialContext (); 63 SecurityContext bean = (SecurityContext)ctx.lookup(Container.ENC_CTX_NAME + "/env/ejb/CalledBean"); 64 bean.nestedInteraction(expectedRoles); 65 } 66 catch(Exception e) 67 { 68 SecurityException se = new SecurityException ("DataSource connection failed"); 69 se.initCause(e); 70 throw se; 71 } 72 validateRoles(expectedRoles, true); 74 } 75 76 public void nestedInteraction(Set expectedRoles) 77 throws SecurityException 78 { 79 validateRoles(expectedRoles, false); 80 } 81 82 91 private void validateRoles(Set expectedRoles, boolean isCallerInRoleFlag) 92 throws SecurityException 93 { 94 Iterator names = expectedRoles.iterator(); 95 while( names.hasNext() ) 96 { 97 String name = (String ) names.next(); 98 boolean hasRole = sessionContext.isCallerInRole(name); 99 if( hasRole != isCallerInRoleFlag ) 100 { 101 throw new SecurityException ("Caller does not have role: "+name); 102 } 103 } 104 } 105 } 106 | Popular Tags |