How to define a custom AuthenticationEntryPoint without XML configuration
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Spring Security, an AuthenticationEntryPoint decides what happens when an unauthenticated user tries to access a protected resource. The default behavior is often a redirect to a login page, which is fine for browser-based apps but wrong for JSON APIs that should return a 401 response body.
You do not need XML configuration to customize this. In modern Spring Security, the standard approach is to implement AuthenticationEntryPoint and register it in a SecurityFilterChain bean.
Understand when AuthenticationEntryPoint is used
This hook runs when the request is not authenticated at all. It is not the same as access-denied handling.
- unauthenticated request to a protected endpoint:
AuthenticationEntryPoint - authenticated user without enough authority:
AccessDeniedHandler
That distinction matters because many "why is my custom entry point not called?" bugs are actually authorization failures, not authentication failures.
Implement a custom entry point
For an API, a common custom behavior is to send JSON instead of redirecting:
This gives clients a clear 401 response that is appropriate for API consumers.
Register it with Java configuration
In current Spring Security style, configure security with a SecurityFilterChain bean:
That is the central hook: exceptionHandling(...).authenticationEntryPoint(...).
Use it differently for browser apps and APIs
For a browser application, a redirecting entry point may still be the right answer. For example, you might send unauthenticated users to a custom login page.
For APIs, though, redirecting is usually the wrong contract. API clients expect a status code and structured body, not HTML navigation.
If your application has both browser and API endpoints, you may need different security chains or request matchers so each kind of endpoint gets the correct behavior.
Test the behavior explicitly
A small integration test helps verify that the custom entry point is actually wired:
This kind of test catches configuration drift early, especially when security rules evolve.
Common Pitfalls
The most common mistake is expecting AuthenticationEntryPoint to handle authorization failures for already authenticated users. That is the job of an AccessDeniedHandler.
Another issue is returning redirects for API endpoints. That works technically, but it is a poor contract for API clients that expect JSON and status codes.
Developers also sometimes configure a custom entry point but forget that another security chain or default config matches the request first. If the wrong chain wins, your custom entry point never runs.
Finally, avoid copying outdated examples based on XML or older configuration styles unless you are maintaining an older codebase. Modern Spring Security uses bean-based Java configuration as the normal approach.
Summary
- Implement
AuthenticationEntryPointwhen you want custom behavior for unauthenticated access. - Register it through
exceptionHandling(...).authenticationEntryPoint(...)in aSecurityFilterChain. - Use JSON
401responses for APIs instead of login-page redirects. - Do not confuse unauthenticated access with authorization failures for authenticated users.
- Test the response path so you know the intended security chain is actually handling the request.

