1 22 package org.jboss.ejb3.embedded; 23 24 25 import org.jboss.ejb3.naming.BrainlessContext; 26 import org.jboss.logging.Logger; 27 import org.jboss.security.auth.callback.SecurityAssociationHandler; 28 import org.jboss.security.auth.login.XMLLoginConfigImpl; 29 import org.jboss.security.plugins.JBossAuthorizationManager; 30 import org.jboss.security.plugins.JaasSecurityManager; 31 import org.jboss.security.plugins.SecurityDomainContext; 32 33 import javax.naming.*; 34 import javax.naming.spi.ObjectFactory ; 35 import javax.security.auth.login.Configuration ; 36 import java.util.HashMap ; 37 import java.util.Hashtable ; 38 import java.util.Map ; 39 40 43 public class JaasSecurityManagerService 44 { 45 @SuppressWarnings ("unused") 46 private static final Logger log = Logger.getLogger(JaasSecurityManager.class); 47 48 private static final String SECURITY_MGR_PATH = "java:/jaas"; 49 50 private static Map <String , JaasSecurityManager> cache = new HashMap <String , JaasSecurityManager>(); 51 52 private Hashtable initialContextProperties; 53 54 public JaasSecurityManagerService() 55 { 56 57 } 58 59 public void setInitialContextProperties(Hashtable initialContextProperties) 60 { 61 this.initialContextProperties = initialContextProperties; 62 } 63 64 private InitialContext getInitialContext() throws NamingException 65 { 66 if (initialContextProperties != null) return new InitialContext(initialContextProperties); 67 else return new InitialContext(); 68 } 69 70 public void start() throws Exception 71 { 72 XMLLoginConfigImpl configuration = new XMLLoginConfigImpl(); 73 configuration.setConfigResource("login-config.xml"); 74 configuration.loadConfig(); 75 76 Configuration.setConfiguration(configuration); 77 78 Context ctx = getInitialContext(); 79 80 String factoryName = SecurityDomainObjectFactory.class.getName(); 81 Reference ref = new Reference("nl.wolfc.embedded.security.plugins.JaasSecurityManager", factoryName, null); 82 ctx.rebind(SECURITY_MGR_PATH, ref); 83 } 84 85 private static JaasSecurityManager getSecurityManager(String name) 86 { 87 JaasSecurityManager manager = cache.get(name); 88 if (manager != null) 89 { 90 return manager; 92 } 93 synchronized (cache) 94 { 95 if (manager != null) 96 return manager; 97 98 manager = new JaasSecurityManager(name, new SecurityAssociationHandler()); 99 cache.put(name, manager); 100 } 101 return manager; 102 } 103 104 public static class SecurityDomainObjectFactory implements ObjectFactory 105 { 106 @SuppressWarnings ("unused") 107 private static final Logger log = Logger.getLogger(SecurityDomainObjectFactory.class); 108 109 public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable <?, ?> environment) throws Exception 110 { 111 115 String securityDomainName = name.get(1); 116 final SecurityDomainContext ctx = new SecurityDomainContext(getSecurityManager(securityDomainName), null); 117 ctx.setAuthorizationManager(new JBossAuthorizationManager(securityDomainName, new SecurityAssociationHandler())); 118 return new BrainlessContext() 119 { 120 public Object lookup(Name name) throws NamingException 121 { 122 log.debug("lookup " + name); 123 if(name.size() < 2) 124 return lookup(name.get(0)); 125 else 126 return ctx.lookup(name.get(1)); 127 } 128 129 public Object lookup(String name) throws NamingException 130 { 131 log.debug("lookup " + name); 132 return getSecurityManager(name); 133 } 134 }; 135 } 136 } 137 } 138 | Popular Tags |