1 22 package org.jboss.ejb3.test.security; 23 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import javax.ejb.EJB ; 27 import javax.ejb.EJBs ; 28 import javax.annotation.Resource; 29 import javax.annotation.security.RolesAllowed; 30 import javax.ejb.Remote ; 31 import javax.ejb.SessionContext ; 32 import javax.ejb.Stateless ; 33 import javax.naming.InitialContext ; 34 import org.jboss.annotation.ejb.RemoteBinding; 35 import org.jboss.annotation.security.SecurityDomain; 36 import org.jboss.ejb3.Container; 37 import org.jboss.logging.Logger; 38 39 43 @Stateless (name="UserInRoleContextSession") 44 @Remote (org.jboss.ejb3.test.security.SecurityContext.class) 45 @RemoteBinding(jndiBinding = "spec.UserInRoleContextSession") 46 @SecurityDomain("spec-test") 47 @RolesAllowed({"Role1", "Role2"}) 48 @EJBs ({@EJB (name="CalledBean", beanInterface=org.jboss.ejb3.test.security.SecurityContext.class, beanName="UserInRoleContextSessionTarget")}) 49 public class SecurityContextBean implements SecurityContext 50 { 51 static Logger log = Logger.getLogger(SecurityContextBean.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/CalledBean"); 64 bean.nestedInteraction(expectedRoles); 65 } 66 catch(Exception e) 67 { 68 e.printStackTrace(); 69 SecurityException se = new SecurityException ("DataSource connection failed"); 70 se.initCause(e); 71 throw se; 72 } 73 validateRoles(expectedRoles, true); 75 } 76 77 public void nestedInteraction(Set expectedRoles) 78 throws SecurityException 79 { 80 validateRoles(expectedRoles, false); 81 } 82 83 92 private void validateRoles(Set expectedRoles, boolean isCallerInRoleFlag) 93 throws SecurityException 94 { 95 Iterator names = expectedRoles.iterator(); 96 while( names.hasNext() ) 97 { 98 String name = (String ) names.next(); 99 boolean hasRole = sessionContext.isCallerInRole(name); 100 if( hasRole != isCallerInRoleFlag ) 101 { 102 throw new SecurityException ("Caller does not have role: "+name); 103 } 104 } 105 } 106 } 107 | Popular Tags |