3

I have been experimenting with setting up authentication with Tomcat config by only setting a context file.

Given the following webapp context file conf/Catalina/localhost/myapp.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <Resource
      name="MyUserDatabase"
      auth="Container"
      type="org.apache.catalina.UserDatabase"
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
      pathname="conf/myapp-users.xml"
      />
  <Realm
      className="org.apache.catalina.realm.UserDatabaseRealm"
      resourceName="MyUserDatabase"
      />
</Context>

When starting Tomcat, it gives the following error:

javax.naming.NameNotFoundException: Name [MyUserDatabase] is not bound in this Context. Unable to find [MyUserDatabase].

However, if I define <Resource name="MyUserDatabase"... in server.xml under <GlobalNamingResources> it works as expected. The documentation for contexts clearly shows the <Resource> element is valid there.

Is there a special way to reference resources defined in a context file? Or is it just not possible?

In-case it matters, I am dealing with Tomcat 9.

1 Answer 1

4

You need to add the property localJndiResource="true" to your realm:

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
       resourceName="MyUserDatabase"
       localJndiResource="true"/>

Without this property Tomcat looks for a resource in <GlobalNamingResources> (cf. Tomcat documentation).

Sign up to request clarification or add additional context in comments.

1 Comment

I confirm it works! - Thanks, I spent ages reading the docs, but alas not the right bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.