Tomcat implements authentication through the Authenticator Valve mechanism. Each authentication method (Documentation Index
Fetch the complete documentation index at: https://mintlify.com/apache/tomcat/llms.txt
Use this file to discover all available pages before exploring further.
BASIC, DIGEST, FORM, SPNEGO, CLIENT-CERT) maps to a concrete AuthenticatorBase subclass in the org.apache.catalina.authenticator package. The method is declared in the web application’s WEB-INF/web.xml inside a <login-config> element, and Tomcat automatically installs the corresponding Authenticator valve when the context starts — no manual valve configuration is needed for standard methods.
Declaring Authentication in web.xml
Every protected web application needs two declarations inWEB-INF/web.xml: a <login-config> that specifies the authentication method, and one or more <security-constraint> elements that define which URL patterns require authentication and which roles are permitted.
<login-config> skeleton:
<security-constraint> protecting a URL pattern:
<http-method> restricts all HTTP methods. Using <http-method-omission> instead protects every method except the listed ones.
BASIC Authentication
BASIC authentication is implemented byBasicAuthenticator (RFC 7617). When a request arrives for a protected resource without credentials, Tomcat returns a 401 Unauthorized response with a WWW-Authenticate: Basic realm="..." header. The browser displays a native login dialog and retransmits the request with an Authorization: Basic <base64(user:password)> header.
BasicAuthenticator source shows it decodes the Base64 header using either UTF-8 (default) or ISO-8859-1 depending on the configurable charset property.
DIGEST Authentication
DIGEST authentication is implemented byDigestAuthenticator (RFC 7616). Instead of transmitting the password, it uses a challenge-response mechanism: the server sends a nonce, and the client computes an MD5 (or SHA-256 with RFC 7616) hash of the credentials combined with the nonce.
DigestAuthenticator implementation supports MD5 as the fallback algorithm and SHA-256/SHA-512-256 per RFC 7616. The nonce itself is hashed with SHA-256 internally.
DIGEST authentication is more secure than BASIC over plain HTTP because passwords are never sent in the clear. However, it is deprecated in HTTP/2 (RFC 7540) and offers weaker protection than FORM over HTTPS with a strong cipher. For new applications prefer FORM over HTTPS.
FORM-Based Authentication
FORM authentication is implemented byFormAuthenticator. Instead of the browser’s native login dialog it uses a custom HTML login page and error page specified in the <login-config>. On successful authentication the user is redirected to the originally requested URL.
web.xml configuration:
action must be j_security_check, and the username and password fields must use the reserved names j_username and j_password:
FormAuthenticator creates a temporary session (default timeout: 120 seconds, configurable via authenticationSessionTimeout) to hold the saved request while the user completes the login form.
FORM authentication stores the original request in the HTTP session before redirecting to the login page. If the session times out before the user submits the form, the
FormAuthenticator either redirects to the landingPage (if configured) or returns an error response.SPNEGO / Kerberos Authentication
SPNEGO authentication is implemented bySpnegoAuthenticator. It enables transparent single sign-on in Windows Active Directory environments — users already logged in to the domain are authenticated without entering credentials again.
The authenticator sends WWW-Authenticate: Negotiate to the browser, receives a Kerberos service ticket (wrapped in a SPNEGO token), and validates it using a JAAS LoginContext. The JAAS configuration name defaults to com.sun.security.jgss.krb5.accept and is configurable via the loginConfigName attribute.
web.xml configuration:
Create a Kerberos service account
In Active Directory, create a service account (e.g.
HTTP/tomcat.example.com@EXAMPLE.COM) and export a keytab file.Configure krb5.conf
Place a Point the JVM at it:
krb5.conf (Linux) or krb5.ini (Windows) that points to your KDC:-Djava.security.krb5.conf=/etc/krb5.confClient Certificate (Mutual TLS) Authentication
CLIENT-CERT authentication is implemented bySSLAuthenticator. It validates the client’s X.509 certificate against the connector’s trust store and then maps the certificate’s Subject DN to a Tomcat user principal via the configured Realm.
certificateVerification set to required (or optional). See the SSL/TLS configuration guide for full connector setup instructions.
Single Sign-On (SSO) Valve
TheSingleSignOn valve (org.apache.catalina.authenticator.SingleSignOn) propagates an authenticated identity across multiple web applications deployed on the same <Host>. Once a user authenticates to any application on the host, subsequent requests to other applications on that host automatically receive the cached Principal without prompting for credentials again.
As documented in the source Javadoc, requirements for SSO to work are:
- The valve must be placed on the
<Host>element (not<Engine>or<Context>). - The shared
<Realm>must be configured at the<Host>level or higher and not overridden per-application. - All web applications must use one of the standard Tomcat
Authenticatorimplementations.
server.xml):
| Attribute | Default | Description |
|---|---|---|
requireReauthentication | false | When true, forces each app’s authenticator to re-validate credentials (useful when the Realm performs additional setup per-request such as setting EJB security context). |
cookieDomain | — | Domain to set on the SSO cookie, enabling SSO across subdomains. |
cookieSecure | — | Set the Secure flag on the SSO cookie. Recommended when using HTTPS. |
cookieHttpOnly | true | Set the HttpOnly flag on the SSO cookie. |
JASPIC / Jakarta Authentication
Tomcat implements the Jakarta Authentication specification (formerly JASPIC — Java Authentication Service Provider Interface for Containers). This allows third-partyServerAuthModule implementations to handle authentication, enabling integration with OAuth 2.0, SAML, JWT, and other modern identity protocols.
Registering a provider via conf/jaspic-providers.xml:
ServletContainerInitializer):
When a JASPIC provider is registered for an application context, it takes precedence over the
<login-config> declaration in web.xml. The standard Authenticator valve is bypassed entirely.Security Constraints Reference
Security constraints are declared entirely inWEB-INF/web.xml. A complete example showing all constraint elements:
<transport-guarantee> values:
| Value | Meaning |
|---|---|
NONE | No transport requirement (default when element is omitted) |
INTEGRAL | Data must not be modified in transit (in practice, requires HTTPS) |
CONFIDENTIAL | Data must be encrypted in transit — Tomcat redirects HTTP to the redirectPort |
