[jboss-cvs] jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2 ...

Norman Richards norman.richards at jboss.com
Wed Nov 1 13:14:21 EST 2006


  User: nrichards
  Date: 06/11/01 13:14:21

  Added:       jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2        
                        Echo.java EchoBean.java EchoHome.java ExClient.java
                        JndiUserAndPass.java ejb-jar.xml jboss-service.xml
                        jboss.xml
  Log:
  modified for j2ee guide
  
  Revision  Changes    Path
  1.1      date: 2006/11/01 18:14:21;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2/Echo.java
  
  Index: Echo.java
  ===================================================================
  package org.jboss.book.security.ex2;
  
  import java.rmi.RemoteException;
  import javax.ejb.EJBObject;
  
  /**
   *
   * @author  Scott.Stark at jboss.org
   * @version $Revision: 1.1 $
   */
  public interface Echo extends EJBObject
  {
     public String echo(String arg) throws RemoteException;
  }
  
  
  
  1.1      date: 2006/11/01 18:14:21;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2/EchoBean.java
  
  Index: EchoBean.java
  ===================================================================
  package org.jboss.book.security.ex2;
  
  import java.rmi.RemoteException;
  import javax.ejb.SessionBean;
  import javax.ejb.SessionContext;
  import javax.naming.Context;
  
  import org.apache.log4j.Category;
  
  public class EchoBean implements SessionBean
  {
     private static final Category log = Category.getInstance(EchoBean.class);
     private SessionContext sessionCtx;
  
     public void ejbCreate()
     {
        log.debug("ejbCreate: ");
     }
  
     public void ejbLoad()
     {
        log.debug("ejbLoad");
     }
  
     public void ejbRemove()
     {
        log.debug("ejbRemove");
     }
  
     public void ejbStore()
     {
        log.debug("ejbStore");
     }
  
     public void setSessionContext(SessionContext context)
     {
        sessionCtx = context;
        log.debug("setSessionContext");
     }
  
     public void unsetSessionContext()
     {
        sessionCtx = null;
        log.debug("unsetSessionContext");
     }
  
     public void ejbActivate()
     {
        log.debug("ejbActivate");
     }
     public void ejbPassivate()
     {
        log.debug("ejbPassivate");
     }
  
     public String echo(String arg)
     {
        log.debug("echo, arg="+arg);
        return arg;
     }
  }
  
  
  
  1.1      date: 2006/11/01 18:14:21;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2/EchoHome.java
  
  Index: EchoHome.java
  ===================================================================
  package org.jboss.book.security.ex2;
  
  import java.rmi.RemoteException;
  import javax.ejb.CreateException;
  import javax.ejb.EJBHome;
  
  /**
   *
   * @author  Scott.Stark at jboss.org
   * @version $Revision: 1.1 $
   */
  public interface EchoHome extends EJBHome
  {
     public Echo create()
        throws RemoteException, CreateException;
  }
  
  
  
  1.1      date: 2006/11/01 18:14:21;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2/ExClient.java
  
  Index: ExClient.java
  ===================================================================
  package org.jboss.book.security.ex2;
  
  import javax.naming.InitialContext;
  import javax.security.auth.login.LoginContext;
  
  import org.apache.log4j.Logger;
  import org.jboss.security.auth.callback.UsernamePasswordHandler;
  
  /**
   *
   * @author  Scott.Stark at jboss.org
   * @version $Revision: 1.1 $
   */
  public class ExClient
  {
     public static void main(String args[]) throws Exception
     {
        Logger log = Logger.getLogger("ExClient");
        log.info("Login with username="+args[0]+", password="+args[1]);
        UsernamePasswordHandler handler = new UsernamePasswordHandler(args[0], args[1].toCharArray());
        LoginContext lc = new LoginContext("ExClient", handler);
        lc.login();
  
        log.info("Looking up EchoBean2");
        InitialContext iniCtx = new InitialContext();
        Object ref = iniCtx.lookup("EchoBean2");
        EchoHome home = (EchoHome) ref;
        Echo echo = home.create();
        log.info("Created Echo");
        log.info("Echo.echo('Hello') = "+echo.echo("Hello"));
        lc.logout();
     }
  }
  
  
  
  1.1      date: 2006/11/01 18:14:21;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2/JndiUserAndPass.java
  
  Index: JndiUserAndPass.java
  ===================================================================
  package org.jboss.book.security.ex2;
  
  import java.security.acl.Group;
  import java.util.Map;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.security.auth.Subject;
  import javax.security.auth.callback.CallbackHandler;
  import javax.security.auth.login.LoginException;
  
  import org.jboss.security.SimpleGroup;
  import org.jboss.security.SimplePrincipal;
  import org.jboss.security.auth.spi.UsernamePasswordLoginModule;
  
  /** An example custom login module that obtains passwords and roles for a user
  from a JNDI lookup.
   
  @author Scott.Stark at jboss.org
  @version $Revision: 1.1 $
  */
  public class JndiUserAndPass extends UsernamePasswordLoginModule
  {
     /** The JNDI name to the context that handles the password/<username> lookup */
     private String userPathPrefix;
     /** The JNDI name to the context that handles the roles/<username> lookup */
     private String rolesPathPrefix;
  
     /** Override to obtain the userPathPrefix and rolesPathPrefix options.
      */
     public void initialize(Subject subject, CallbackHandler callbackHandler,
        Map sharedState, Map options)
     {
        super.initialize(subject, callbackHandler, sharedState, options);
        userPathPrefix = (String) options.get("userPathPrefix");
        rolesPathPrefix = (String) options.get("rolesPathPrefix");
     }
  
     /** Get the roles the current user belongs to by querying the
      rolesPathPrefix + '/' + super.getUsername() JNDI location.
      */
     protected Group[] getRoleSets() throws LoginException
     {
        try
        {
           InitialContext ctx = new InitialContext();
           String rolesPath = rolesPathPrefix + '/' + super.getUsername();
           String[] roles = (String[]) ctx.lookup(rolesPath);
           Group[] groups = {new SimpleGroup("Roles")};
           log.info("Getting roles for user="+super.getUsername());
           for(int r = 0; r < roles.length; r ++)
           {
              SimplePrincipal role = new SimplePrincipal(roles[r]);
              log.info("Found role="+roles[r]);
              groups[0].addMember(role);
           }
           return groups;
        }
        catch(NamingException e)
        {
           log.error("Failed to obtain groups for user="+super.getUsername(), e);
           throw new LoginException(e.toString(true));
        }
     }
  
     /** Get the password of the current user by querying the
      userPathPrefix + '/' + super.getUsername() JNDI location.
      */
     protected String getUsersPassword() throws LoginException
     {
        try
        {
           InitialContext ctx = new InitialContext();
           String userPath = userPathPrefix + '/' + super.getUsername();
           log.info("Getting password for user="+super.getUsername());
           String passwd = (String) ctx.lookup(userPath);
           log.info("Found password="+passwd);
           return passwd;
        }
        catch(NamingException e)
        {
           log.error("Failed to obtain password for user="+super.getUsername(), e);
           throw new LoginException(e.toString(true));
        }
     }
  
  }
  
  
  
  1.1      date: 2006/11/01 18:14:21;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2/ejb-jar.xml
  
  Index: ejb-jar.xml
  ===================================================================
  <?xml version="1.0"?>
  <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
                           "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
  
  <ejb-jar>
      <enterprise-beans>
          <session>
              <ejb-name>EchoBean2</ejb-name>
              <home>org.jboss.book.security.ex2.EchoHome</home>
              <remote>org.jboss.book.security.ex2.Echo</remote>
              <ejb-class>org.jboss.book.security.ex2.EchoBean</ejb-class>
              <session-type>Stateless</session-type>
              <transaction-type>Container</transaction-type>
          </session>
      </enterprise-beans>
      
      <assembly-descriptor>
          <method-permission>
              <role-name>Echo</role-name>
              <method>
                  <ejb-name>EchoBean2</ejb-name>
                  <method-name>*</method-name>
              </method>
          </method-permission>
      </assembly-descriptor>
  </ejb-jar>
  
  
  
  1.1      date: 2006/11/01 18:14:21;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2/jboss-service.xml
  
  Index: jboss-service.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <server>
      <mbean code="org.jboss.book.security.ex2.service.JndiStore"
             name="jboss.docs.security:service=JndiStore"/>
      
      <!-- The custom JAAS login configuration that installs 
           a Configuration capable of dynamically updating the
           config settings
      -->
      <mbean code="org.jboss.book.security.service.SecurityConfig"
             name="jboss.docs.security:service=LoginConfig-EX2">
          <attribute name="AuthConfig">META-INF/login-config.xml</attribute>
          <attribute name="SecurityConfigName">jboss.security:service=XMLLoginConfig</attribute>
      </mbean>
  </server>
  
  
  
  1.1      date: 2006/11/01 18:14:21;  author: nrichards;  state: Exp;jboss-docs/jbossas/j2ee/examples/src/main/org/jboss/book/security/ex2/jboss.xml
  
  Index: jboss.xml
  ===================================================================
  <?xml version="1.0"?>
  <jboss>
      <security-domain>java:/jaas/security-ex2</security-domain>
  </jboss>
  
  
  



More information about the jboss-cvs-commits mailing list