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