aspose file tools*
The moose likes Tomcat and the fly likes Help with Tomcat 7 DataSourceRealm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Help with Tomcat 7 DataSourceRealm" Watch "Help with Tomcat 7 DataSourceRealm" New topic
Author

Help with Tomcat 7 DataSourceRealm

Bell Katapa
Ranch Hand

Joined: Jan 06, 2012
Posts: 45

I have a working web app deployed on Tomcat 7 and it's already using successfully connection pooling from a configured datasource. Trouble is when i enforce authentication i simply can't login. I keep being forwarded to the login-error.html. Nothing being dumped to the logs. Please help! Below are the details:

1. web.xml

<security-role>
<role-name>cashier</role-name>
</security-role>

<security-role>
<role-name>supervisor</role-name>
</security-role>

<security-role>
<role-name>administrator</role-name>
</security-role>

<security-constraint>
<web-resource-collection>
<web-resource-name>Secure Zone</web-resource-name>
<url-pattern>/reports/*</url-pattern>
<url-pattern>/register/*</url-pattern>
<url-pattern>/index.html</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>administrator</role-name>
<role-name>supervisor</role-name>
</auth-constraint>
</security-constraint>

<security-constraint>
<web-resource-collection>
<web-resource-name>Secure Zone</web-resource-name>
<url-pattern>/index/*</url-pattern>
<url-pattern>/register/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>cashier</role-name>
</auth-constraint>
</security-constraint>

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

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

2. context.xml

<Resource name="jdbc/myDS"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="use"
password="pass"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://IPAddress:3306/homeDB?autoReconnect=true"
removeAbandonedTimeout="60" />

3. server.xml

<Realm className="org.apache.catalina.realm.DataSourceRealm" debug="99"
dataSourceName="jdbc/myDS"
userTable="userT" userNameCol="username" userCredCol="password"
userRoleTable="userRT" roleNameCol="rolename" />

4. Authentication & Authorization Tables

CREATE TABLE IF NOT EXISTS userT (
username VARCHAR(20) NOT NULL PRIMARY KEY
,password VARCHAR(20) NOT NULL
);

CREATE TABLE IF NOT EXISTS userRT (
username VARCHAR(20) NOT NULL
,rolename VARCHAR(20) NOT NULL
,PRIMARY KEY (username, rolename)
);

INSERT userT VALUES ('user1', 'user1');
INSERT userRT VALUES ('user1','administrator');


When enter 'user1' for username and 'user1' for password, i keep being sent to the error page.
Here is the login page:

5. Login Page

<table class="login" width="400pt" height="290pt">
<form action="j_security_check" method="post">
<tr>
<th>Enter Username:</th>
<td><input type="text" name="j_username"/></td>
</tr>
<tr>
<th>Enter Password:</th>
<td><input type="password" name="j_password"/></td>
</tr>
<tr>
<td/>
<td><b><input type="submit" value="Submit"/></b></td>
</tr>
</form>
</table>
Bell Katapa
Ranch Hand

Joined: Jan 06, 2012
Posts: 45

Ok. Now am getting something from the log. Any help to fix this?

Dec 23, 2012 1:56:03 PM org.apache.catalina.realm.DataSourceRealm open
SEVERE: Exception performing authentication
javax.naming.NameNotFoundException: Name [jdbc/myDS] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
at org.apache.catalina.realm.DataSourceRealm.open(DataSourceRealm.java:394)
at org.apache.catalina.realm.DataSourceRealm.authenticate(DataSourceRealm.java:285)
at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:295)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:450)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1770)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Bell Katapa
Ranch Hand

Joined: Jan 06, 2012
Posts: 45

Just an update. I finally got it working. I put the <Realm> and <Resource> tag contents into the same context.xml file. At this point i still could not login. Then i added one more attribute to the <Realm> tag i.e. localDataSource="true". It worked after that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Tomcat 7 DataSourceRealm