2012-12-05 53 views
4

對於具有在其web.xml中有以下(除其他事項外)的web應用testappTomcat的JDBC與數據源境界

<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的境界配置:

配置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不。請注意,我們使用Configuration 2中的Context.xml資源在我們的代碼中連接到MySQL,並且它工作得很好。然而,當一個tomcat Realm嘗試使用它時,即使它看起來與配置1做同樣的事情,身份驗證總是失敗。

任何人都有任何的洞察力,爲什麼這可能是?

+0

我剛剛意識到...這是一個更好的問題serverfault?我是一個Java開發人員,所以我習慣在這裏提問,但是... –

+0

我想複製配置1,但不幸的是我沒有任何META-INF目錄。我使用maven jersey-quickstart-archtype爲我生成項目。我能做些什麼來生成目錄和context.xml。或者手動在哪裏放置這個目錄或自己創建文件? –

+0

我不確定。通常,當您使用Maven構建WAR文件並將其解包時,您將擁有一個META-INF目錄。如果你想添加東西到你的項目中以包含在META-INF中,通常(以我的經驗),你創建目錄'src/main/webapp/META-INF /',然後把文件放在那裏。 –

回答

9

假設你有數據源別處工作(在,比方說,Servlet的),所有你需要做的就是添加localDataSource="true"到領域decleration使得境界是:

<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將工作得很好。

+2

請注意,這也是無用的JNDI錯誤'javax.naming .NameNotFoundException:使用DataSourceRealm執行JAAS身份驗證時,名稱jdbc未在此Context中綁定。 – bzuillsmith