I can't take full credit for all of the code because I found the concept and most of 
the code via google.  But, I can't locate the original source it seems.  I've tried 
some searches but no luck so far.  If I can find the URL for where I found the 
original code, I will post it to give credit to the author.

Here are the files that make up my service...

MBean interface:
----------------------------------------------------------------
import org.jboss.system.ServiceMBean;
  |                                                                                    
                                         
  | /**
  |  * An mbean interface for a config service that pushes an xml based
  |  * javax.security.auth.login.Configuration onto the config stack managed by
  |  * the mbean whose name is given by the SecurityConfigName attribute.
  |  *
  |  **/
  | public interface SecurityConfigMBean extends ServiceMBean
  | {
  |    /**
  |     * Get the classpath resource name of the security configuration file
  |     **/
  |    public String getAuthConfig();
  |                                                                                    
                                         
  |    /**
  |     * Set the classpath resource name of the security configuration file
  |     **/
  |    public void setAuthConfig(String configURL);
  |                                                                                    
                                         
  |    /**
  |     * Get the name of the SecurityConfig mbean whose pushLoginConfig and
  |     * popLoginConfig ops will be used to install and remove the xml login
  |     * config
  |     **/
  |    public String getSecurityConfigName();
  |                                                                                    
                                         
  |    /**
  |     * Set the name of the SecurityConfig mbean whose pushLoginConfig and
  |     * popLoginConfig ops will be used to install and remove the xml login
  |     * config
  |     **/
  |    public void setSecurityConfigName(String objectName);
  | ----------------------------------------------------------------


MBean implementation:
----------------------------------------------------------------
import java.net.URL;
  | import java.util.Hashtable;
  | import javax.management.MBeanServer;
  | import javax.management.ObjectName;
  | import org.jboss.security.auth.login.XMLLoginConfig;
  | import org.jboss.system.ServiceMBeanSupport;
  | import org.apache.log4j.Logger;
  |                                                                                    
                                         
  | /**
  |  * A security config mbean that loads an xml login configuration and
  |  * pushes a XMLLoginConfig instance onto the the config stack managed by
  |  * the SecurityConfigName mbean(default=jboss.security:name=SecurityConfig).
  |  *
  |  **/
  | public class SecurityConfig extends ServiceMBeanSupport
  |    implements SecurityConfigMBean
  | {
  |    // Constants -----------------------------------------------------
  |                                                                                    
                                         
  |    // Attributes ----------------------------------------------------
  |    private String authConf = "login-config.xml";
  |    private XMLLoginConfig config = null;
  |    private ObjectName mainSecurityConfig;
  |    protected Logger log = Logger.getLogger(SecurityConfig.class);
  |                                                                                    
                                         
  |    // Static --------------------------------------------------------
  |                                                                                    
                                         
  |    // Constructors --------------------------------------------------
  |    public SecurityConfig()
  |    {
  |       setSecurityConfigName("jboss.security:service=SecurityConfig");
  |    }
  |                                                                                    
                                         
  |    // Public --------------------------------------------------------
  |    /**
  |     * Get the name
  |     **/
  |    public String getName()
  |    {
  |       return "JAAS Login Config";
  |    }
  |                                                                                    
                                         
  |    /**
  |     * Get securityConfigName
  |     **/
  |    public String getSecurityConfigName()
  |    {
  |       return mainSecurityConfig.toString();
  |    }
  |                                                                                    
                                         
  |    /**
  |     * Set securityConfigName
  |     **/
  |    public void setSecurityConfigName(String objectName)
  |    {
  |       try
  |       {
  |          mainSecurityConfig = new ObjectName(objectName);
  |       }
  |       catch(Exception e)
  |       {
  |          log.fatal("Failed to create ObjectName", e);
  |       }
  |    }
  |                                                                                    
                                         
  |    /**
  |     * Get the resource path to the JAAS login configuration file to use.
  |     **/
  |    public String getAuthConfig()
  |    {
  |       return authConf;
  |    }
  |                                                                                    
                                         
  |    /**
  |     * Set the resource path to the JAAS login configuration file to use.
  |     * The default is "login-config.xml".
  |     **/
  |    public void setAuthConfig(String authConf)
  |    {
  |       this.authConf = authConf;
  |    }
  |                                                                                    
                                         
  |    // Protected --------------------------------------------------------
  |    /**
  |     * Start the service.
  |     **/
  |    protected void startService() throws Exception
  |    {
  |       // Look for the authConf as resource
  |       ClassLoader loader = Thread.currentThread().getContextClassLoader();
  |       URL loginConfig = loader.getResource(authConf);
  |       if( loginConfig != null )
  |       {
  |          String securityConfigName = "MySecurityConfig";
  |          log.info("Using securityConfigName: '"+securityConfigName+"'");
  |          log.info("Using JAAS AuthConfig: "+loginConfig.toExternalForm());
  |          config = new XMLLoginConfig();
  |          config.setConfigURL(loginConfig);
  |          config.start();
  |          MBeanServer server = super.getServer();
  |          ObjectName name = super.getServiceName();
  |          Hashtable props = name.getKeyPropertyList();
  |          props.put(securityConfigName, "XMLLoginConfig");
  |          name = new ObjectName(name.getDomain(), props);
  |          server.registerMBean(config, name);
  |          Object[] args = {name.toString()};
  |          String[] sig = {String.class.getName()};
  |          server.invoke(mainSecurityConfig, "pushLoginConfig", args, sig);
  |       }
  |       else
  |       {
  |          log.warn("No AuthConfig resource found");
  |       }
  |    }
  |                                                                                    
                                         
  |    /**
  |     * Stop the service.
  |     **/
  |    protected void stopService() throws Exception
  |    {
  |       String securityConfigName = "PanelSecurityConfig";
  |       log.info("Using securityConfigName: '"+securityConfigName+"'");
  |       MBeanServer server = super.getServer();
  |       ObjectName name = super.getServiceName();
  |       Hashtable props = name.getKeyPropertyList();
  |       props.put(securityConfigName, "XMLLoginConfig");
  |       name = new ObjectName(name.getDomain(), props);
  |       Object[] args = {};
  |       String[] sig = {};
  |       server.invoke(mainSecurityConfig, "popLoginConfig", args, sig);
  |       server.unregisterMBean(name);
  |    }
  | }
  | ----------------------------------------------------------------

jboss-service.xml
----------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
  | <server>
  |    <mbean code="com.innovationsw.panel.security.jmx.SecurityConfig"
  |       name="ISG.panel:service=PanelSecurityLoginConfig">
  |       <attribute name="AuthConfig">META-INF/login-config.xml</attribute>
  |       <!-- The service which supports dynamic processing of login-config.xml
  |          configurations.
  |       -->
  |       <depends>jboss.security:service=XMLLoginConfig</depends>
  |       <!-- Optionally specify the security mgr service to use when
  |          this service is stopped to flush the auth caches of the domains
  |          registered by this service.
  |       -->
  |       <!-- only in jboss 4.0 series
  |       <depends optional-attribute-name="SecurityManagerService">
  |          jboss.security:service=JaasSecurityManager
  |       </depends>
  |       -->
  |    </mbean>
  | </server>
----------------------------------------------------------------

login-config.xml
----------------------------------------------------------------
<policy>
  |         <application-policy name = "MyRealm">
  |                 <authentication>
  |                         <login-module code = 
"org.jboss.security.auth.spi.DatabaseServerLoginModule" flag = "required">
  |                                 <module-option name = 
"dsJndiName">java:/MyDS</module-option>
  |                                 <module-option name = "principalsQuery">SELECT 
password FROM users WHERE username=?</module-option>
  |                                 <module-option name = "rolesQuery">SELECT 
role,rolegroup FROM users_role WHERE username=?</module-option>
  |                                 <module-option name = 
"unauthenticatedIdentity">nobody</module-option>
  |                         </login-module>
  |                 </authentication>
  |         </application-policy>
  | </policy>
  | ----------------------------------------------------------------


View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3844693#3844693

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3844693


-------------------------------------------------------
SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to