How to configure JDBCRealm in Apache Tomcat 7?

I would like users (who are allowed to register on my site) to boot from the MySQL database. For this, I want to configure JDBCRealm for my Apache Tomcat 7 application server.

I read the documentation and created a database connection using the JNDI resource (jdbc / foo4). This resource works (I use it already in my application to extract data). This does not seem to work, this is binding the area to this resource.

My configuration file looks like this:

CSI \ main \ WebApp \ META-INF \ context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/my-webapp">
  <!-- works! -->
  <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"
          />
  <!-- Does not seem to work?! -->
  <Realm className="org.apache.catalina.realm.DataSourceRealm"
         dataSourceName="jdbc/foo4"
         userTable="users" 
         userNameCol="user_name" 
         userCredCol="user_pass"
         userRoleTable="user_roles" 
         roleNameCol="role_name"/>
</Context>

In my standard deployment descriptor, I entered the following:

CSI \ main \ WebApp \ WEB-INF \ web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <security-constraint>
    <display-name>General Restriction</display-name>
    <web-resource-collection>
      <web-resource-name>Entire Application</web-resource-name>
      <description>All resources in this application are protected.</description>
      <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>admin</role-name>
    </auth-constraint>
  </security-constraint>
  <login-config>
    <auth-method>FORM</auth-method>
    <realm-name>Administration Area</realm-name>
    <form-login-config>
      <form-login-page>/login.jsp</form-login-page>
      <form-error-page>/error.jsp</form-error-page>
    </form-login-config>
  </login-config>
  <security-role>
    <role-name>admin</role-name>
  </security-role>
  <resource-ref>
    <res-ref-name>jdbc/foo4</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

And I used this SQL script to create a user sample:

security.sql

(_ varchar (15) null key, user_pass varchar (15) null);

user_roles (_ varchar (15) ,
role_name varchar (15) null, (_, role_name));

INSERT INTO users (user_name, user_pass) VALUES ('benny', 'test'); INTO user_roles (_, _) VALUES ('benny', 'admin');

"benny" "test". "tomcat" "tomcat" ( % CATALINA_HOME%\conf\tomcat-users.xml), MySQL.

, Tomcat- MySQL ( "mysql-connector-java-5.1.9.jar" % CATALINA_HOME%\lib).

:

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].

- ?

+5
1

( Tomcat /conf/server.xml), ( webapp /META-INF/context.xml), . localDataSource true.

<Realm ... localDataSource="true" />

. Tomcat 7.0 Realm:

localDataSource

Context, , , DataSource. , false: .

+11

All Articles