Redirecting unauthorized users to a custom page instead of Spring Security's default error is done by supplying a custom AccessDeniedHandler, which intercepts the AccessDeniedException thrown when an authenticated user lacks the required permissions.
A user without the ADMIN role hitting /admin/dashboard triggers AccessDeniedException, and the custom handler redirects them to /access-denied with a friendly explanation instead of Spring's generic 403 whitelabel page.
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.exceptionHandling(ex -> ex
.accessDeniedHandler((request, response, accessDeniedException) ->
response.sendRedirect("/access-denied")));
return http.build();
}A concise interview answer is
I'd implement a custom AccessDeniedHandler and register it through exceptionHandling().accessDeniedHandler() in the security configuration. It intercepts the AccessDeniedException thrown for authenticated-but-unauthorized users and redirects them to a friendly custom page instead of Spring's default error response.
Last updated: 16 July 2026
Previous question: What are AuthenticationManager and ProviderManager in Spring Security?