Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set up a JDBCRealm in Apache Tomcat 7?
    primarykey
    data
    text
    <p>I would like that the users (which are allowed to log on to my site) are loaded from a MySQL database. To do that I want to set up a JDBCRealm for my Apache Tomcat 7 application server.</p> <p>I've read through the <a href="http://tomcat.apache.org/tomcat-7.0-doc/realm-howto.html#JDBCRealm" rel="noreferrer">documentation</a> and created a database connection using a JNDI resource (jdbc/foo4). This resource works (I use it already in my application to retrieve data). Which does not seem to work, is the linking of a realm with this resource.</p> <p>My configuration file looks like this:</p> <p><strong>src\main\webapp\META-INF\context.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Context antiJARLocking="true" path="/my-webapp"&gt; &lt;!-- works! --&gt; &lt;Resource name="jdbc/foo4" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/foo4" username="root" password="root" maxActive="8" maxIdle="4" maxWait="10000" auth="Container" /&gt; &lt;!-- Does not seem to work?! --&gt; &lt;Realm className="org.apache.catalina.realm.DataSourceRealm" dataSourceName="jdbc/foo4" userTable="users" userNameCol="user_name" userCredCol="user_pass" userRoleTable="user_roles" roleNameCol="role_name"/&gt; &lt;/Context&gt; </code></pre> <p>In my Standard Deployment Descriptor I entered the following:</p> <p><strong>src\main\webapp\WEB-INF\web.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt; &lt;security-constraint&gt; &lt;display-name&gt;General Restriction&lt;/display-name&gt; &lt;web-resource-collection&gt; &lt;web-resource-name&gt;Entire Application&lt;/web-resource-name&gt; &lt;description&gt;All resources in this application are protected.&lt;/description&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/web-resource-collection&gt; &lt;auth-constraint&gt; &lt;role-name&gt;admin&lt;/role-name&gt; &lt;/auth-constraint&gt; &lt;/security-constraint&gt; &lt;login-config&gt; &lt;auth-method&gt;FORM&lt;/auth-method&gt; &lt;realm-name&gt;Administration Area&lt;/realm-name&gt; &lt;form-login-config&gt; &lt;form-login-page&gt;/login.jsp&lt;/form-login-page&gt; &lt;form-error-page&gt;/error.jsp&lt;/form-error-page&gt; &lt;/form-login-config&gt; &lt;/login-config&gt; &lt;security-role&gt; &lt;role-name&gt;admin&lt;/role-name&gt; &lt;/security-role&gt; &lt;resource-ref&gt; &lt;res-ref-name&gt;jdbc/foo4&lt;/res-ref-name&gt; &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt; &lt;res-auth&gt;Container&lt;/res-auth&gt; &lt;/resource-ref&gt; &lt;/web-app&gt; </code></pre> <p>And I used this SQL script to create a sample user:</p> <p><strong>security.sql</strong></p> <blockquote> <p>create table users ( user_name varchar(15) not null primary key, user_pass varchar(15) not null );</p> <p>create table user_roles ( user_name varchar(15) not null,<br> role_name varchar(15) not null, primary key (user_name, role_name) );</p> <p>INSERT INTO users(user_name,user_pass)VALUES('benny','test'); INSERT INTO user_roles(user_name,role_name)VALUES('benny','admin');</p> </blockquote> <p>But I'm not able to login with user "benny" and password "test". I can login with user "tomcat" and password "tomcat" (because it is defined in %CATALINA_HOME%\conf\tomcat-users.xml) but I can not login with my own user from my MySQL database.</p> <p>Of course, my Tomcat server is using a MySQL driver (I put "mysql-connector-java-5.1.9.jar" to %CATALINA_HOME%\lib).</p> <p>I get the following error message when I try to log in:</p> <pre><code>Sep 08, 2012 7:38:55 PM org.apache.catalina.realm.DataSourceRealm open Exception performing authentication javax.naming.NameNotFoundException: Name [jdbc/foo4] is not bound in this Context. Unable to find [jdbc]. </code></pre> <p>Does someone sees the error?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload