如何在Apache Tomcat 7中设置JDBCRealm?

Ben*_*uer 5 mysql tomcat jdbc j-security-check jdbcrealm

我希望用户(允许登录我的网站)从MySQL数据库加载.为此,我想为我的Apache Tomcat 7应用程序服务器设置JDBCRealm.

我已经阅读了文档并使用JNDI资源(jdbc/foo4)创建了数据库连接.此资源有效(我已在我的应用程序中使用它来检索数据).这似乎不起作用,是一个领域与这个资源的链接.

我的配置文件如下所示:

SRC \主\ web应用程序\ 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>
Run Code Online (Sandbox Code Playgroud)

在我的标准部署描述符中,我输入了以下内容:

SRC \主\ web应用\ 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>
Run Code Online (Sandbox Code Playgroud)

我使用这个SQL脚本来创建一个示例用户:

security.sql

create table users(user_name varchar(15)not null primary key,user_pass varchar(15)not null);

create table user_roles(user_name varchar(15)not null,
role_name varchar(15)not null,primary key(user_name,role_name));

INSERT INTO用户(user_name,user_pass)VALUES('benny','test'); INSERT INTO user_roles(user_name,role_name)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].
Run Code Online (Sandbox Code Playgroud)

有人看到错误吗?

Bal*_*usC 11

默认情况下,域将始终查找全局数据源(在Tomcat端配置/conf/server.xml),但是您已经定义了本地数据源(在webapp端配置完全相同/META-INF/context.xml),因此域无法找到数据源.您需要将localDataSource属性设置为true.

<Realm ... localDataSource="true" />
Run Code Online (Sandbox Code Playgroud)

另请参阅Tomcat 7.0 Realm文档:

localDataSource

当领域嵌套在Context元素中时,这允许领域使用为Context定义的DataSource而不是全局DataSource.如果未指定,则默认为false:使用全局DataSource.