Friday Feb 15, 2008

Using JDBCRealm with self-registration

My last blog talked about a pattern to implement self-registration. As a follow up, in this blog I talk about how to use a JDBCRealm in this context.

First we need to create a data realm in glassfish. Here is how you can do it using an ant task. (You need to populate the variables appropriately, ofcourse).

    <exec executable="${ASADMIN_SCRIPT}">
      <arg line="create-auth-realm" />
      <arg line="--user ${AS_ADMIN_USER}" />
      <arg line="--passwordfile ${PASSFILE}" />
      <arg line="--host ${AS_SERVER_NAME}" />
      <arg line="--port ${AS_ADMIN_PORT}" />
      <arg line="--classname com.sun.enterprise.security.auth.realm.jdbc.JDBCRealm" />
      <arg line='--property 
digest-algorithm=SHA:encoding=Hex:user-name-column=USERNAME
:password-column=PASSPHRASE:group-name-column=ROLENAME
:jaas-context=jdbcRealm:datasource-jndi="jdbc/CommonDB"
:group-table=MYUSERROLE_VIEW:user-table=MYUSER' />
      <arg line="JDBC_UserRealm" />
    </exec>

Then, your persistence.xml would have an entry for the persistence unit that maps the PU name to the data source:

  <persistence-unit name="UserManagementPU" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
    <non-jta-data-source>java:comp/env/jdbc/CommonDB</non-jta-data-source>
    <class>com.x.y.User</class>
    <class>com.x.y.UserRole</class>
  </persistence-unit>

The User management implementation should talk to this realm. So the EntityManagerFactory should be created looking up this JNDI.

                
String jndiName = "java:comp/env/" + "CommonDB";
EntityManagerFactoy emf = 
(EntityManagerFactory) new InitialContext().lookup(emfJndiName);
...

The user table is now exposed through this realm, so that you can use it in your login page as follows.

Add this to your applications's web.xml to use this realm during form login.

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>JDBC_UserRealm</realm-name>
        <form-login-config>
            <form-login-page>/login.jsf</form-login-page>
            <form-error-page>/loginError.jsf</form-error-page>
        </form-login-config>
    </login-config>
About

manveen

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today