Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

In Spring Security, what is the difference between an AuthenticationEntryPoint and an AccessDeniedHandler, and which filter calls them?

Both are called by ExceptionTranslationFilter, which catches AuthenticationException and AccessDeniedException thrown further down the chain. The AuthenticationEntryPoint handles 'you are not authenticated' and starts authentication, for example by redirecting to a login page or sending 401 with a WWW-Authenticate header. The AccessDeniedHandler handles 'you are authenticated but not allowed' and usually sends 403. An anonymous user who hits an AccessDeniedException is sent to the entry point, not the handler, because logging in might fix the problem.

Why this question is asked

Every secured application answers two different questions when a request is refused: should the client log in, or is logging in pointless because this user may never see the page? Getting them mixed up produces bugs we all have met: an API that redirects a mobile app to an HTML login page, or a 403 where the client should have refreshed its token. Interviewers ask this because the two interfaces, and the filter that chooses between them, explain most of those bugs. In this answer, we will recap 401 versus 403, look at ExceptionTranslationFilter, see each interface and its default implementations, explain the anonymous rule, and configure both.

1A Quick Recap of 401 and 403

HTTP has two status codes for refusals. 401 Unauthorized, despite its name, means unauthenticated: the server does not know who you are, and the response should say how to authenticate, in a WWW-Authenticate header such as Basic or Bearer. 403 Forbidden means the server knows who you are and the answer is still no; logging in again will not help.

Spring Security maps these two situations to two exception types and two handler interfaces. AuthenticationException means authentication is missing or failed. AccessDeniedException means authorization failed: the current user lacks permission.

2ExceptionTranslationFilter Decides Who Answers

ExceptionTranslationFilter sits near the end of the security filter chain, just before AuthorizationFilter, the filter that checks the URL rules. It calls the rest of the chain inside a try block. Anything thrown further down, by AuthorizationFilter, or by method security around a controller if the exception propagates that far, comes back to it. It then translates the exception into a response.

Notice the word 'further down'. Exceptions thrown by filters before it, such as an authentication filter rejecting a bad password, are not seen by ExceptionTranslationFilter. Those filters handle their own failures, typically by calling their own entry point or failure handler directly.

Step throughWhat ExceptionTranslationFilter does
  1. Run the rest of the chainAuthorizationFilter, then the servlet
  2. AuthenticationException thrown?
    YesClear context, save request, call entry pointNoContinue
  3. AccessDeniedException thrown?
    YesCheck who the user isNoRethrow anything else
  4. Is the user anonymous or remember-me?
    YesCall entry point: logging in may helpNoCall AccessDeniedHandler: 403

4 steps in order. Play them, or step through them one at a time.

Anonymous users never reach the AccessDeniedHandler.

3The Two Interfaces and Their Defaults

AuthenticationEntryPoint has one method, commence(request, response, authException). Its job is to start authentication, and what that means depends on the login method. LoginUrlAuthenticationEntryPoint redirects to /login for form login. BasicAuthenticationEntryPoint sends 401 with WWW-Authenticate: Basic realm="...". BearerTokenAuthenticationEntryPoint sends 401 with WWW-Authenticate: Bearer, adding error="invalid_token" when a token was rejected. HttpStatusEntryPoint simply sends a status. Before calling it, ExceptionTranslationFilter clears the security context and saves the request in the RequestCache, so that after a form login the user returns to the page they wanted.

AccessDeniedHandler has one method, handle(request, response, accessDeniedException). The default, AccessDeniedHandlerImpl, sends 403, or forwards to an error page if one is configured. For resource servers, BearerTokenAccessDeniedHandler sends 403 with WWW-Authenticate: Bearer error="insufficient_scope".

Which component answers
SituationCalledTypical response
No credentials, page needs loginAuthenticationEntryPoint302 to /login, or 401 with WWW-Authenticate
Expired or invalid tokenAuthenticationEntryPoint401 Bearer error="invalid_token"
Anonymous user, rule denies accessAuthenticationEntryPoint302 or 401: logging in may help
Logged-in user without the roleAccessDeniedHandler403
Missing or wrong CSRF tokenAccessDeniedHandler (via CsrfFilter)403

4Why the Anonymous User Goes to the Entry Point

Spring Security fills the context with an AnonymousAuthenticationToken when nobody is logged in, so rules like permitAll() and hasRole() always have an object to look at. When AuthorizationFilter denies such a request, it throws AccessDeniedException, the same exception it throws for a logged-in user without a role.

If ExceptionTranslationFilter passed that to the AccessDeniedHandler, the anonymous visitor would get a 403 and never be offered a login. So the filter asks its AuthenticationTrustResolver whether the user is anonymous (or only remembered by a remember-me cookie). If so, it treats the denial as missing authentication and calls the entry point with an InsufficientAuthenticationException. The visitor is redirected to the login page, or an API client receives 401, which is exactly what it should do next.

5Configuring Both

Both components are configured in the exceptionHandling section of a SecurityFilterChain. Login configurers such as formLogin(), httpBasic() and oauth2ResourceServer() register their own entry points automatically; we set ours explicitly when we want a specific response.

Java
@Bean
SecurityFilterChain api(HttpSecurity http) throws Exception {
http.securityMatcher("/api/**")
.authorizeHttpRequests(a -> a
.requestMatchers("/api/admin/**").hasRole("ADMIN")
.anyRequest().authenticated())
.oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults()))
.exceptionHandling(e -> e
.authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint())
.accessDeniedHandler(new BearerTokenAccessDeniedHandler()));
return http.build();
}
// GET /api/orders, no token -> 401 WWW-Authenticate: Bearer
// GET /api/orders, expired token -> 401 WWW-Authenticate: Bearer error="invalid_token", ...
// GET /api/admin/users, user token -> 403 WWW-Authenticate: Bearer error="insufficient_scope", ...

Notice that the expired token is answered by the bearer token filter itself, through its failure handler, which uses the same kind of entry point; the request never reaches ExceptionTranslationFilter. The result is consistent because both paths end in a BearerTokenAuthenticationEntryPoint.

6Follow-up questions

What happens if a chain has no login method at all?

There is no configurer to register an entry point, so Spring Security falls back to Http403ForbiddenEntryPoint, which answers 403 even for anonymous users. That is a common cause of APIs returning 403 where 401 was expected, especially with hand-written token filters. Setting an explicit entry point fixes it.

Does an AccessDeniedException thrown by @PreAuthorize reach the AccessDeniedHandler?

Only if it propagates out of Spring MVC. If a @ControllerAdvice has a broad @ExceptionHandler(Exception.class), it catches the exception first and usually turns it into a 500. Either rethrow AccessDeniedException from the advice, or handle it there deliberately with a 403.

Can we use different entry points for different paths in one chain?

Yes, with exceptionHandling(e -> e.defaultAuthenticationEntryPointFor(entryPoint, matcher)) we register an entry point per request matcher, for example 401 for /api/** and a login redirect elsewhere. Separate SecurityFilterChain beans per area are often clearer.

7Conclusion

In this answer, we saw that 401 means 'authenticate first' and 403 means 'you may not'. ExceptionTranslationFilter catches the exceptions thrown further down the chain and chooses: AuthenticationException goes to the AuthenticationEntryPoint, which starts authentication with a redirect or a 401 and a WWW-Authenticate header, and AccessDeniedException goes to the AccessDeniedHandler, usually 403, unless the user is anonymous, in which case the entry point is called because logging in might help. We configure both in exceptionHandling().

What to say in the interviewAbout 90 seconds aloud

Open with

Both are hooks used by ExceptionTranslationFilter, which sits just before AuthorizationFilter and wraps the rest of the chain in a try block, catching AuthenticationException and AccessDeniedException thrown further down.

Then, if they want more

The AuthenticationEntryPoint answers 'you're not authenticated, here's how to authenticate'. Depending on the login method, it redirects to the login page, or sends 401 with a WWW-Authenticate header, like Basic or Bearer with error invalid_token. Before calling it, the filter clears the context and saves the request so a form login can return the user to the page they wanted.

The AccessDeniedHandler answers 'I know who you are, and the answer is no', which is usually a 403; CsrfFilter also uses it for bad CSRF tokens. The subtle rule is the anonymous user. When nobody is logged in, the context holds an anonymous token, and an authorization rule that denies it throws AccessDeniedException just like for a real user.

ExceptionTranslationFilter checks whether the user is anonymous or remember-me, and if so it calls the entry point instead, because logging in might fix the problem. That's why an anonymous API call gets 401, not 403. I configure them with http.exceptionHandling, setting authenticationEntryPoint and accessDeniedHandler.

One thing I watch for: a chain with no login method gets Http403ForbiddenEntryPoint by default, so anonymous calls come back 403, which confuses clients.

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 "In Spring Security, what is the difference between an AuthenticationEntryPoint and an...": https://scrivio-interview-library.netlify.app/spring-boot/security-architecture/a764350a23/