Question

This might be very basic, but I stuck in it for couple of hours now, so please hear me out.

I'm trying to do a JDBCRealm-based of authentication in a Java 7 web app on Glassfish 4.0 in Eclipse. This blog post was the reference for cofiguring JDBCRealm.

web.xml is as follow

<login-config>
  <auth-method>FORM</auth-method>
  <realm-name>MyJDBCRealm</realm-name>
  <form-login-config>
    <form-login-page>/login.html</form-login-page>
    <form-error-page>/tologin.html</form-error-page>
  </form-login-config>
</login-config>

When I start the Glassfish server it's clearly written in console that:

SEC1115: Realm [MyJDBCRealm] of classtype [com.sun.enterprise.security.ee.auth.realm.jdbc.JDBCRealm] successfully created.

But when I request any app's resource, I'm getting this exception

javax.naming.NamingException: Lookup failed for 'MyJDBCRealm' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: MyJDBCRealm not found]

with root cause:

javax.naming.NameNotFoundException: MyJDBCRealm not found
  at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:237)
  at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:204)
  ...

which happens here

public DataSource getDataSource() {
  DataSource datasource = null;

  try {
    datasource = (DataSource) initialContext.lookup("MyJDBCRealm");
  } catch (NamingException ex) {
    ex.printStackTrace();
  }

  return datasource;
}

I should note that the Hibernate successfully connects to backend database and maps the tables and in the glassfish console, I can successfully ping the JDBC connection pool.

Était-ce utile?

La solution

I guess you are confusing something.

A realm is not the same thing as a datasource. To get an instance of a realm in Java you can do the following:

JDBCRealm realm = (JDBCRealm) Realm.getInstance("realmName");

If you really want to get a datasource you have to use JNDI name which often starts with jdbc/ like this:

DataSource datasource = (DataSource) initialContext.lookup("jdbc/MyJDBCRealm");

But anyway I think for normal JDBC based authentication you don't need something like this.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top