1 22 package org.jboss.test.security.ejb; 23 24 import java.security.Principal ; 25 import java.util.Set ; 26 import java.util.Iterator ; 27 import javax.ejb.SessionBean ; 28 import javax.ejb.SessionContext ; 29 import javax.naming.InitialContext ; 30 import javax.security.auth.Subject ; 31 import org.jboss.logging.Logger; 32 33 38 public class CustomPrincipalBean implements SessionBean 39 { 40 private static Logger log = Logger.getLogger(CustomPrincipalBean.class); 41 42 private SessionContext ctx; 43 44 public void ejbCreate() 45 { 46 } 47 48 public void ejbActivate() 49 { 50 } 51 52 public void ejbPassivate() 53 { 54 } 55 56 public void ejbRemove() 57 { 58 } 59 60 public void setSessionContext(SessionContext ctx) 61 { 62 this.ctx = ctx; 63 } 64 65 public boolean validateCallerPrincipal(Class type) 66 { 67 ClassLoader typeLoader = type.getClassLoader(); 68 log.info("validateCallerPrincipal, type="+type+", loader="+typeLoader); 69 Principal caller = ctx.getCallerPrincipal(); 70 log.info("caller="+caller+", class="+caller.getClass()); 71 boolean isType = true; 72 if( caller.getClass().isAssignableFrom(type) == false ) 73 { 74 log.error("type of caller is not: "+type); 75 isType = false; 76 } 77 78 try 79 { 80 InitialContext ctx = new InitialContext (); 81 Subject s = (Subject ) ctx.lookup("java:comp/env/security/subject"); 82 Set principals = s.getPrincipals(); 83 Iterator iter = principals.iterator(); 84 while( iter.hasNext() ) 85 { 86 Object p = iter.next(); 87 ClassLoader pLoader = p.getClass().getClassLoader(); 88 log.info("type="+p.getClass()+", loader="+pLoader); 89 } 90 Set customPrincipals = s.getPrincipals(type); 91 caller = (Principal ) customPrincipals.iterator().next(); 92 log.info("Subject caller="+caller+", class="+caller.getClass()); 93 if( caller.getClass().isAssignableFrom(type) == true ) 94 { 95 log.info("type of caller is: "+type); 96 isType = true; 97 } 98 } 99 catch(Exception e) 100 { 101 log.error("Failed to lookup security mgr", e); 102 } 103 return isType; 104 } 105 106 } 107 | Popular Tags |