When a user tries to access a resource without the necessary permissions, you want to redirect them to a custom "access denied" page instead of displaying the default Spring Security error message. How would you achieve this in your Spring Security configuration?

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.

Key Points

Example

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();
}

Interview Tip

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?

More Spring Security - Level I interview questions

All Spring Security - Level I questions