9 Replies Latest reply on Mar 7, 2007 8:23 PM by LeQuoc Thai

    I need helps from anyone about JAAS with DatabaseServerLogin

    LeQuoc Thai Novice

      Hi everyone!

      I have a problem that I can't solve. I have two machines with hostname is "A" and "B". On A, I installed mySQL and create the database is "mydatabase" and two tables are "Roles" and "Users"
      Also on A, I installed JBoss Server (jboss-4.0.5.GA), and I select database is mysql. File "hsqldb.xml" has contents:

      ...
      <jndi-name>DefaultDS</jndi-name>
      <connection-url>jdbc:mysql://localhost:3306/mydatabase</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>root</user-name>
      <password>abc123</password>
      ...

      When I restart server on machine A, I see some messages:
      10:43:02,109 INFO [WrapperDataSourceService] Bound ConnectionMana
      :service=DataSourceBinding,name=DefaultDS' to JNDI name 'java:DefaultDS'

      On the B machine, I use Eclipse to create a EJB 3.0 project called "ActionBean". Then, I write three file ejb-jar.xml, jboss.xml and login-config.xml. They have some info important :
      ejb-jar.xml
      ...
      <enterprise-beans>
       <session>
       <ejb-name>ActionBean</ejb-name>
       <resource-ref>
       <res-ref-name>jdbc/DefaultDS</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       </resource-ref>
       </session>
      </enterprise-beans>
      <assembly-descriptor>
       <Security-role>
       <role-name>doctor</role-name>
       </Security-role>
       <Security-role>
       <role-name>nurse</role-name>
       </Security-role>
       <method-permission>
       <role-name>doctor</role-name>
       <method>
       <ejb-name>ActionBean</ejb-name>
       <method-name>getCallInfo</method-name>
       </method>
       </method-permission>
       <method-permission>
       <role-name>nurse</role-name>
       <method>
       <ejb-name>ActionBean</ejb-name>
       <method-name>add</method-name>
       </method>
       </method-permission>
       </assembly-descriptor>
      ...

      jboss.xml
      <security-domain>DB-Domain</security-domain>
      <enterprise-beans>
       <session>
       <ejb-name>ActionBean</ejb-name>
       <resource-ref>
       <res-ref-name>jdbc/DefaultDS</res-ref-name>
       <jndi-name>java:/DefaultDS</jndi-name>
       </resource-ref>
       </session>
      </enterprise-beans>

      login-config.xml
      <application-policy name="DB-Domain">
       <authentication>
       <login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag="required">
       <module-option name="dsJndiName">java:/DefaultDS</module-option>
       <module-option name="principalsQuery">
       select passwd from Users where username=?</module-option>
       <module-option name="rolesQuery">
       select Role from Roles where username=?</module-option>
       </login-module>
       </authentication>
      </application-policy>

      Then, I pakage ActionBean and put three files (ejb-jar.xml,jboss.xml and login-config.xml) inside META-INF folder. JBoss server on the machine A inform all things ok!
      Also, on the machine B. I write the main class (client):
      LoginContext lc = new LoginContext("userTest", new ClientCallbackHandler());;
      lc.login();
      InitialContext ctx = new InitialContext();
      ClientAction obj = (ClientAction)ctx.lookup("ActionBean/remote");
      try{
       System.out.println(obj.getCallInfo());
       //System.out.println(obj.add());
      
       }catch(Exception e){
       System.out.println("You don't allow to do this method !");
       }
      ...

      My auth.config:
      userTest {
       org.jboss.security.ClientLoginModule required;
      };

      I imported package "jbossall-client.jar" and "jbosssx.jar". On the server(machine A), I also copied dirver "mysql-connector-java-3.1.14-bin.jar" inside \server\default\lib.
      Then, I run my main class and from console I enter username and passwd. But any informations that I entered can't call method on ActionBean (ex:obj.getCallInfo()). This mean is correct info can't call any method on ActionBean. Which wrongs ? mydatabase has two tables:
      CREATE TABLE Users(username VARCHAR(64) PRIMARY KEY, passwd VARCHAR(64))
      CREATE TABLE Roles(username VARCHAR(64), Role VARCHAR(32))

      (I were writed a simple bean to access informations from mydatabase, all things ok). Please help me to solve my problem. Thanks.