Reputation: 23
I have Tomcat authentication (form authentication) in my application. After successful authentication, I am able to get the username from the request.getRemoteUser()
method in my servlet.
Where I can find request.getRemoteUser()
code? How does this work? Where has the RemoteUser has been set and stored? Since HTTP requests are stateless and execute independently, how this giving the username in all subsequent requests?
Upvotes: 0
Views: 2012
Reputation: 16055
In order to deal with authentication, every context in Tomcat has a Valve
that extends AuthenticatorBase
, that:
HttpServletRequest
,401
response with a WWW-Authenticate
header for most authentication methods or a 302
redirect to the login page for form authentication.403
response.Request#setUserPrincipal
and proceeds with the next valve.For the details check the AuthenticatorBase#invoke
method.
Most authentication methods are based on the Authorization
header sent by the browser (the form authenticator uses request parameters).
If a session is present (e.g. you called HttpServletRequest#getSession
), the authenticated user will be cached in the session and subsequent requests will not need to authenticate any more. You can force session creation using the alwaysUseSession
attribute on the authenticator valves (cf. documentation). The server can recognize the presence of a previously established session through many methods:
JSESSIONID
Cookie
header in the request,jsessionid
path parameter in the URL,Upvotes: 1