-->

apache tomcat 7.0.30 datasourcerealm javax.naming.

2019-02-17 01:42发布

问题:

let me describe the environment first:

environment: - mac os x (java build 1.6.0_35-b10-428-11M3811) - apache tomcat 7.0.30 - mysql 5.5.27 - tomcat/lib --> mysql-connector-java-5.1.22-bin.jar

i've implemented successfully a JDBCRealm and want to switch to a DataSourceRealm because it is recommended for production environments. i use form-based authentication.

I did everything described here: http://tomcat.apache.org/tomcat-7.0-doc/realm-howto.html#DataSourceRealm and here: http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#MySQL_DBCP_Example

server.xml

<Realm className="org.apache.catalina.realm.LockOutRealm">
     <Realm className="org.apache.catalina.realm.DataSourceRealm" 
            dataSourceName="jdbc/proto" digest="SHA-256" roleNameCol="role" 
            userCredCol="password" userNameCol="name" userRoleTable="roles" 
            userTable="users"/>
</Realm>

context.xml

<Resource auth="Container" 
          driverClassName="com.mysql.jdbc.Driver" 
          maxActive="100" maxIdle="30" maxWait="10000" 
          name="jdbc/proto" password="proto" type="javax.sql.DataSource" 
          url="jdbc:mysql://localhost:3306/proto" username="proto"/>

WEB-INF/web.xml

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/proto</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

when i submit the username and password i get the following exception:

Sep 18, 2012 9:38:32 PM org.apache.catalina.realm.DataSourceRealm open
SEVERE: Exception performing authentication
javax.naming.NameNotFoundException: Name [jdbc/proto] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
at org.apache.catalina.realm.DataSourceRealm.open(DataSourceRealm.java:394)
at org.apache.catalina.realm.DataSourceRealm.authenticate(DataSourceRealm.java:285)
at org.apache.catalina.realm.CombinedRealm.authenticate(CombinedRealm.java:146)
at org.apache.catalina.realm.LockOutRealm.authenticate(LockOutRealm.java:180)
at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:295)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:450)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

i haven't tried to setup the environment on a different os to test if it's a mac specific issue.

any suggestions what i missed? i appreciate your help! thank you.

回答1:

To avoid declaring the ressource in GlobalNamingResources, put localDataSource="true" in the Realm



回答2:

In this case I believe it's because your realm (In this case it is declared at the server level) is trying to use a naming resource, jdbc/proto (Which in this case is declared at the web application level)

Here is what I understand is the java server context hierarchy

  1. Server
  2. Host
  3. WebApplication Context (can be in server.xml, or in META-INF Context.xml)


回答3:

Actually this [jdbc/proto] is mentioned in ProjectName.xml as a property like

<ResourceLink global="jdbc/proto" name="jdbc/proto"/>

So make sure that ProjectName.xml file exist at tomcatInstances/ServerName/conf/Catalina/localhost/ProjectName.xml and having this resource link and your exception is gone.

I also faced the same issue and got that local context file of application was got deleted for some reason from location

tomcatInstances/ServerName/conf/Catalina/localhost/ProjectName.xml 

Restored the files from back and done... its working fine now, Hope this will help someone.



回答4:

In my case the problem was that I had a conflicting dependency in my pom.xml. In my case it was the java-naming dependency. Make sure you don't have any naming dependencies / jars that conflict with Tomcat's naming libs.

After I removed it, the issue is gone (same environment as you btw).