Quick revision
The short answer
Pre-authentication means another system has already authenticated the user, and Spring Security only extracts the identity from the request and loads the user's authorities. Behind a reverse proxy that sends the username in a header, we use RequestHeaderAuthenticationFilter with that header name and a PreAuthenticatedAuthenticationProvider backed by a user details service for roles. This is only safe if the application is reachable solely through the proxy and the proxy strips any copy of the header sent by clients; X.509 client certificate login is another form of pre-authentication.
What to remember
In this answer, we saw that pre-authentication means another system authenticated the user and Spring Security only extracts the identity and loads authorities. We configured RequestHeaderAuthenticationFilter with the header name and a PreAuthenticatedAuthenticationProvider with a user details service for roles, made the chain stateless, and saw that the setup is only safe when the application is reachable solely through the proxy and the proxy strips client copies of the header. X.509 client certificate login with http.x509() is the other common form.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Pre-authentication means another system has already authenticated the user, and Spring Security only extracts the identity from the request and loads the user's authorities. Behind a reverse proxy that sends the username in a header, we use RequestHeaderAuthenticationFilter with that header name and a PreAuthenticatedAuthenticationProvider backed by a user details service for roles. This is only safe if the application is reachable solely through the proxy and the proxy strips any copy of the header sent by clients; X.509 client certificate login is another form of pre-authentication.
Why this question is asked
Many companies put a single sign-on proxy in front of internal applications, so the applications themselves never see a password. Spring Security supports this with its pre-authentication classes, but the configuration is the easy part. Interviewers ask this question because the security of the setup depends almost entirely on what happens outside the application. In this answer, we will recap what pre-authentication means, configure the header-based filter and provider, see why trusting a header is dangerous without network controls, make sure the proxy strips the header, and look at X.509 certificates as the other common case.
1A Quick Recap of Pre-Authentication
In a normal Spring Security login, our application checks the credentials: a password against a hash, or a token signature. In pre-authentication, that check happened somewhere else before the request reached us. A reverse proxy, a server that sits in front of our application and forwards requests to it, logs the user in through the company's identity provider and adds the result to each request, for example a header X-Forwarded-User: anna.
Spring Security's job is then smaller. It reads the identity from the request, trusts it, and loads the user's authorities, the roles that decide what they may do. The classes for this live in the org.springframework.security.web.authentication.preauth package.
- Browsernever sends the header itself, or the proxy removes it
- SSO reverse proxyauthenticates the user, removes any incoming X-Forwarded-User, sets its own
- Network boundaryonly the proxy can reach the application
- RequestHeaderAuthenticationFilterreads X-Forwarded-User
- PreAuthenticatedAuthenticationProviderloads roles through a user details service
- Controllerssee an authenticated user with authorities
6 layers, from the top down. Step through them one at a time.
2Configuring the Header Filter and Provider
Two pieces do the work. RequestHeaderAuthenticationFilter reads the username from a named header and creates a PreAuthenticatedAuthenticationToken. PreAuthenticatedAuthenticationProvider accepts that token and asks a user details service for the user's roles, through a small wrapper that adapts a UserDetailsService.
Notice that the UserDetailsService here only provides roles; it never checks a password, because there is none. It can read a local table, a directory, or a groups header that the proxy also sets. Notice also the stateless policy: the proxy sends the header on every request, so there is no need to keep a session, and a session would keep a user logged in even after the proxy stopped vouching for them.
3Why Trusting a Header Is Dangerous
A header is just text that any HTTP client can send. If anyone can reach the application directly, without going through the proxy, they can send X-Forwarded-User: admin and Spring Security will believe them. The filter has no way to know who set the header; all of its security comes from the assumption that only the proxy can talk to the application.
So we enforce that assumption in the network. The application listens only on a private network, firewall rules or Kubernetes NetworkPolicies allow traffic only from the proxy, and there is no public load balancer, NodePort or debugging port that bypasses it. Stronger still is mutual TLS between proxy and application: the application accepts connections only from a client presenting the proxy's certificate, so even someone inside the network cannot impersonate the proxy.
We should run exactly that test, from a pod or host that is not the proxy, and expect the connection to be refused. Making it an automated check means a future network change cannot quietly open the hole.
4The Proxy Must Strip the Header
Even with the network locked down, the proxy itself must be careful. If a client sends X-Forwarded-User: admin to the proxy, and the proxy simply adds its own header without removing the client's, the application may see two values, or only the client's, depending on how headers are merged. The proxy must remove any incoming copy of the header from client requests and then set its own value after authentication. Every common proxy can do this, but it must be configured, and it should be tested the same way: send the header through the proxy as an anonymous user and confirm it never reaches the application.
5X.509 Client Certificates: Another Form of Pre-Authentication
Pre-authentication is not only about headers. With X.509 client certificate authentication, the TLS layer, in the servlet container or in a proxy, checks that the client presented a certificate signed by a trusted authority. Spring Security then extracts the username from the certificate's subject and loads roles, exactly like the header case. In Spring Security this is http.x509(...).
Here the trust comes from the TLS handshake rather than from network rules, which is why certificates are popular for service-to-service calls. If TLS ends at a proxy instead of the application, we are back to a header carrying the certificate details, with the same need to restrict who can reach the application.
6Follow-up questions
What happens if the header is missing?
With setExceptionIfHeaderMissing(true), the default, the filter throws an exception and the request fails. Setting it to false lets the request continue as anonymous, which suits applications that have some public pages and rely on authorisation rules to protect the rest.
How do we test locally without the proxy?
In tests we can set the header directly with MockMvc, which is fine because the test controls the request. For local running, a profile can add a fixed test user, but that profile must never be active in deployed environments; a check at startup that refuses to start with it in production is a good safeguard.
Is there a better alternative to a plain username header?
Yes: the proxy can forward a signed token, such as a JWT from the identity provider, and the application validates it with Spring Security's resource server support. Then a forged header without a valid signature is useless, even if someone reaches the application directly.
7Conclusion
In this answer, we saw that pre-authentication means another system authenticated the user and Spring Security only extracts the identity and loads authorities. We configured RequestHeaderAuthenticationFilter with the header name and a PreAuthenticatedAuthenticationProvider with a user details service for roles, made the chain stateless, and saw that the setup is only safe when the application is reachable solely through the proxy and the proxy strips client copies of the header. X.509 client certificate login with http.x509() is the other common form.
Open with
Pre-authentication means someone else already authenticated the user, for example an SSO reverse proxy, and Spring Security only takes the identity from the request and loads the user's authorities; there is no password check in my application.
Then, if they want more
For a proxy that passes the username in a header, I configure a RequestHeaderAuthenticationFilter with the header name, say X-Forwarded-User, and give it an authentication manager with a PreAuthenticatedAuthenticationProvider. That provider uses a user details service, wrapped with UserDetailsByNameServiceWrapper, just to load roles. I usually make the chain stateless since the header comes on every request.
The important part is outside the code: a header is just text anyone can send, so the application must only be reachable through the proxy, enforced with network rules or Kubernetes NetworkPolicies, and ideally mutual TLS so only the proxy's certificate is accepted.
And the proxy must remove any copy of the header that a client sends before setting its own. I would test both: a direct request with a forged header must be refused, and a forged header sent through the proxy must never reach the app.
X.509 client certificate authentication, http.x509 in Spring Security, is the other common form of pre-authentication: TLS validates the certificate and Spring extracts the username from the subject.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is pre-authentication in Spring Security, and how would you configure an...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/faed1e1407/