1 22 23 29 30 package org.jboss.test.security.clientlogin; 31 32 import java.security.Principal ; 33 import java.rmi.RemoteException ; 34 import javax.ejb.SessionContext ; 35 import javax.ejb.SessionBean ; 36 import javax.security.auth.login.LoginContext ; 37 import javax.naming.InitialContext ; 38 39 import org.jboss.security.auth.callback.UsernamePasswordHandler; 40 41 48 public class BeanA implements SessionBean 49 { 50 private SessionContext context; 51 52 public void ejbCreate() 53 { 54 } 55 56 public void ejbActivate() 57 { 58 } 59 60 public void ejbPassivate() 61 { 62 } 63 64 public void ejbRemove() 65 { 66 } 67 68 public void setSessionContext(SessionContext context) 69 { 70 this.context = context; 71 } 72 73 public Principal callBeanAsClientLoginUser() throws RemoteException 74 { 75 Principal caller = context.getCallerPrincipal(); 76 String inputName = caller.getName(); 77 try 78 { 79 UsernamePasswordHandler handler = new UsernamePasswordHandler("clientLoginA1", "A1"); 80 LoginContext lc = new LoginContext ("client-login", handler); 81 lc.login(); 82 InitialContext ctx = new InitialContext (); 83 IClientLoginHome home = (IClientLoginHome) ctx.lookup("java:comp/env/TargetBean"); 84 IClientLogin bean = home.create(); 85 Principal callerB1 = bean.callBeanAsClientLoginUser(); 86 if( callerB1.getName().equals("clientLoginA1") == false ) 87 throw new RemoteException ("callBeanAsClientLoginUser#1 != clientLoginA1"); 88 lc.logout(); 89 90 handler = new UsernamePasswordHandler("clientLoginA2", "A2"); 91 lc = new LoginContext ("client-login", handler); 92 lc.login(); 93 Principal callerB2 = bean.callBeanAsClientLoginUser(); 94 if( callerB2.getName().equals("clientLoginA2") == false ) 95 throw new RemoteException ("callBeanAsClientLoginUser#2 != clientLoginA2"); 96 lc.logout(); 97 98 String inputName2 = context.getCallerPrincipal().getName(); 100 if( inputName.equals(inputName2) == false ) 101 throw new RemoteException ("CallerPrincipal changed after logout"); 102 } 103 catch(Exception e) 104 { 105 if(e instanceof RemoteException ) 106 throw (RemoteException ) e; 107 throw new RemoteException ("callBeanAsClientLoginUser", e); 108 } 109 return caller; 110 } 111 public Principal callTarget() throws RemoteException 112 { 113 return null; 114 } 115 116 } 117 | Popular Tags |