1 22 package org.jboss.ejb3.test.security; 23 24 import java.security.Principal ; 25 import javax.ejb.EJB ; 26 import javax.ejb.EJBs ; 27 import javax.annotation.Resource; 28 import javax.annotation.security.RolesAllowed; 29 import javax.annotation.security.RunAs; 30 import javax.ejb.EJBException ; 31 import javax.ejb.Remote ; 32 import javax.ejb.SessionContext ; 33 import javax.ejb.Stateless ; 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import org.jboss.logging.Logger; 37 import org.jboss.annotation.ejb.RemoteBinding; 38 import org.jboss.annotation.security.SecurityDomain; 39 import org.jboss.ejb3.Container; 40 41 46 @Stateless (name="Level1CallerBean") 47 @Remote (CalledSession.class) 48 @RemoteBinding(jndiBinding = "spec.Level1CallerBean") 49 @RunAs("InternalRole") 50 @RolesAllowed({"Echo"}) 51 @SecurityDomain("spec-test") 52 @EJBs ({@EJB (name="Caller2", beanInterface=org.jboss.ejb3.test.security.CalledSession.class, beanName="CallerBean2"), 53 @EJB (name="Callee", beanInterface=org.jboss.ejb3.test.security.StatelessSessionLocal.class, beanName="CalleeBean")}) 54 public class Level1CallerBean implements CalledSession 55 { 56 private static Logger log = Logger.getLogger(Level1CallerBean.class); 57 @Resource SessionContext sessionContext; 58 59 63 public String invokeEcho(String arg) 64 { 65 log.info("echo, arg=" + arg); 66 Principal p = sessionContext.getCallerPrincipal(); 67 log.info("echo, callerPrincipal=" + p); 68 boolean isEchoCaller = sessionContext.isCallerInRole("Echo"); 69 log.info("echo, isCallerInRole('Echo')=" + isEchoCaller); 70 boolean isInternalRole = sessionContext.isCallerInRole("InternalRole"); 71 log.info("echo, isCallerInRole('InternalRole')=" + isInternalRole); 72 73 if (isEchoCaller == false && isInternalRole == false) 74 throw new SecurityException ("isEchoCaller == false && isInternalRole == false"); 75 76 try 77 { 78 InitialContext ic = new InitialContext (); 79 StatelessSessionLocal localBean = (StatelessSessionLocal)ic.lookup(Container.ENC_CTX_NAME + "/env/Callee"); 80 String echo2 = localBean.echo(arg); 81 log.info("echo#1, callee.echo=" + echo2); 82 echo2 = localBean.echo(arg); 83 log.info("echo#2, callee.echo=" + echo2); 84 } 85 catch (Exception e) 86 { 87 log.error("Failed to invoke Callee.echo", e); 88 throw new EJBException ("Failed to invoke Callee.echo", e); 89 } 90 91 isEchoCaller = sessionContext.isCallerInRole("Echo"); 92 log.info("echo, isCallerInRole#2('Echo')=" + isEchoCaller); 93 isInternalRole = sessionContext.isCallerInRole("InternalRole"); 94 log.info("echo, isCallerInRole#2('InternalRole')=" + isInternalRole); 95 96 if (isEchoCaller == false && isInternalRole == false) 97 throw new SecurityException ("isEchoCaller == false && isInternalRole == false post calls"); 98 99 return arg; 100 } 101 102 105 public String callEcho() 106 { 107 try 108 { 109 InitialContext ic = new InitialContext (); 110 111 CalledSession bean = (CalledSession)ic.lookup(Container.ENC_CTX_NAME + "/env/Caller2"); 112 String echo = bean.invokeEcho("Level1"); 113 log.info("echo, callee.invokeEcho=" + echo); 114 115 String principal = sessionContext.getCallerPrincipal().getName(); 116 return principal; 117 } 118 catch (Exception e) 119 { 120 log.error("Failed to invoke Callee.invokeEcho", e); 121 throw new EJBException ("Failed to invoke Callee.invokeEcho", e); 122 } 123 124 } 125 126 129 public String callLocalEcho(String arg) 130 { 131 try 132 { 133 InitialContext ic = new InitialContext (); 134 Context enc = (Context ) ic.lookup("java:comp/env"); 135 CalledSessionLocal bean = (CalledSessionLocal)enc.lookup("ejb/Caller"); 136 String echo2 = bean.invokeEcho(arg + "Level1"); 137 log.info("echo, callee.invokeEcho=" + echo2); 138 return echo2; 139 } 140 catch (Exception e) 141 { 142 log.error("Failed to invoke Callee.invokeEcho", e); 143 throw new EJBException ("Failed to invoke Callee.invokeEcho", e); 144 } 145 } 146 147 public void noop() 148 { 149 log.info("noop"); 150 } 151 152 } 153 | Popular Tags |