【问题标题】:Tomcat JDBC vs. DataSource RealmTomcat JDBC 与 DataSource 领域
【发布时间】:2012-11-23 16:33:07
【问题描述】:

对于 webapp testapp,它的 web.xml 中包含以下内容(除其他外)

<security-constraint>
    <web-resource-collection>
        <web-resource-name>My JSP</web-resource-name>
        <url-pattern>*.secured</url-pattern>
        <url-pattern>/login</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>

    <auth-constraint>
        <role-name>mobileusers</role-name>
    </auth-constraint>
    <!--
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
    -->
</security-constraint>

<login-config>
    <auth-method>DIGEST</auth-method>
    <realm-name>Identity</realm-name>
</login-config>

<security-role>
    <description>
        No Description
    </description>
    <role-name>mobileusers</role-name>
</security-role>

考虑以下两种 Tomcat Realm 配置:

配置 1 - JDBC 领域:

在.../webapps/testapp/META-INF/context.xml

<Realm  className="org.apache.catalina.realm.JDBCRealm" 
        driverName="com.mysql.jdbc.Driver"
        connectionName="mysqluser"
        connectionPassword="redacted"
        connectionURL="jdbc:mysql://192.168.1.5/testdb?autoReconnectForPools=true&amp;characterEncoding=UTF-8"
        digest="MD5"
        userTable="Users" 
        userNameCol="name" 
        userCredCol="password"
        userRoleTable="Users" 
        roleNameCol="roleName"
/>

配置 2 - 数据源领域:

在.../webapps/testapp/META-INF/context.xml:

<Realm  className="org.apache.catalina.realm.DataSourceRealm" 
        digest="MD5"
        userTable="Users" 
        userNameCol="name" 
        userCredCol="password"
        userRoleTable="Users" 
        roleNameCol="roleName"
        dataSourceName="jdbc/testDB"
/>

在.../conf/context.xml:

<Resource 
    name="jdbc/testDB" 
    auth="Container" 
    type="javax.sql.DataSource" 
    removeAbandoned="true" 
    removeAbandonedTimeout="15" 
    maxActive="5" 
    maxIdle="5" 
    maxWait="7000" 
    username="mysqluser"
    password="redacted"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://192.168.1.5/testdb?autoReconnectForPools=true&amp;characterEncoding=UTF-8"
    factory="com.mycompany.util.configuration.customfactory"
    validationQuery="SELECT '1';"
    testOnBorrow="true"/>

由于我不清楚的原因,配置 1 对我们有效,但配置 2 不适用。请注意,我们使用配置 2 中的 Context.xml 资源在我们的代码中到处连接到 MySQL,它工作得很好。然而,当一个 tomcat Realm 尝试使用它时,身份验证总是失败,即使它似乎在做与配置 1 相同的事情。

有人知道为什么会这样吗?

【问题讨论】:

  • 我刚刚意识到......对于 serverfault,这是一个更好的问题吗?我是 Java 开发者,所以习惯在这里提问,但是...
  • 我正在尝试复制配置 1,但不幸的是我没有任何 META-INF 目录。我使用 maven jersey-quickstart-archtype 为我生成项目。我该怎么做才能生成目录和 context.xml。或者手动在哪里可以放置这个目录或自己创建文件?
  • 我不确定。通常,当您使用 Maven 构建 WAR 文件并将其解压缩时,您将拥有一个 META-INF 目录。如果您想将内容添加到项目中以包含在 META-INF 中,通常(根据我的经验),您创建目录 src/main/webapp/META-INF/,然后将文件放在那里。
  • 我正在尝试为我的 webapp 设置领域。请帮忙。 stackoverflow.com/questions/39715453/…

标签: tomcat digest-authentication jdbcrealm


【解决方案1】:

假设您的 DataSource 在其他地方工作(例如,在 Servlets 中),您所要做的就是将 localDataSource="true" 添加到 Realm 声明中,这样 Realm 是:

<Realm  className="org.apache.catalina.realm.DataSourceRealm"
    localDataSource="true"
    digest="MD5"
    userTable="Users" 
    userNameCol="name" 
    userCredCol="password"
    userRoleTable="Users" 
    roleNameCol="roleName"
    dataSourceName="jdbc/testDB"
/>

至少,这对我有用。

为了完美,100% 清楚,尽管有此参数的名称,但如果您不想将 DataSource 放在 Webapp 的 context.xml 中,则不需要;服务器的上下文 XML 可以正常工作。

【讨论】:

  • 请注意,这也是使用 DataSourceRealm 执行 JAAS 身份验证时出现无用的 JNDI 错误 javax.naming.NameNotFoundException: Name jdbc is not bound in this Context 的解决方案。
  • 对于使用 tomcat 8 及更高版本的摘要属性不起作用,我们需要包含 CredentialHandler 标记。
猜你喜欢
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 2010-09-14
  • 1970-01-01
  • 2013-03-21
相关资源
最近更新 更多