### Configure Spring Security Exception Handlers via YAML Properties Source: https://github.com/clutcher/spring-security-exception-handler/blob/main/README.md Example YAML configuration showing how to enable and customize different exception handlers (default, GraphQL, custom) based on URL patterns and processing order using Spring Boot application properties. ```yaml dev: clutcher: security: handlers: default: enabled: true urls: ["/**"] order: 100 graphql: enabled: true urls: ["/graphql"] order: 0 custom: enabled: true urls: ["/api/v2/**"] order: 50 ``` -------------------------------- ### Manually Configure Spring Security Exception Filter in Java Source: https://github.com/clutcher/spring-security-exception-handler/blob/main/README.md Example Java configuration class demonstrating how to manually create and register the `SpringSecurityExceptionFilter` bean and integrate it into the Spring Security filter chain using `HttpSecurity` for explicit control over filter placement. ```java @Configuration @EnableWebSecurity public class SecurityConfig { @Autowired private List exceptionHandlers; @Bean public SpringSecurityExceptionFilter springSecurityExceptionFilter() { return new SpringSecurityExceptionFilter(exceptionHandlers); } @Bean public SecurityFilterChain filterChain(HttpSecurity http, SpringSecurityExceptionFilter exceptionFilter) throws Exception { return http .authorizeHttpRequests(authorize -> authorize .requestMatchers("/public/**").permitAll() .anyRequest().authenticated() ) .addFilterBefore(exceptionFilter, ExceptionTranslationFilter.class) .formLogin(withDefaults()) .build(); } } ``` -------------------------------- ### Add Gradle Dependency for Spring Security Exception Handler Source: https://github.com/clutcher/spring-security-exception-handler/blob/main/README.md Instructions to add the `spring-security-exception-handler-starter` dependency to a Spring Boot project using Gradle's Kotlin DSL for build configuration. ```kotlin implementation("dev.clutcher.spring-security:spring-security-exception-handler-starter:1.0.0") ``` -------------------------------- ### Add Maven Dependency for Spring Security Exception Handler Source: https://github.com/clutcher/spring-security-exception-handler/blob/main/README.md Instructions to add the `spring-security-exception-handler-starter` dependency to a Spring Boot project using Maven's XML configuration in the `pom.xml` file. ```xml dev.clutcher.spring-security spring-security-exception-handler-starter 1.0.0 ``` -------------------------------- ### Configure Spring Security Exception Handlers via Application Properties (YAML) Source: https://github.com/clutcher/spring-security-exception-handler/blob/main/README.md This YAML configuration demonstrates how to enable and configure multiple Spring Security exception handlers using application properties. It allows defining URL patterns (`urls`), enabling or disabling specific handlers (`enabled`), and setting their processing priority (`order`) for different scenarios, such as a default handler and a GraphQL-specific handler. ```yaml dev: clutcher: security: handlers: default: enabled: true # Enable/disable the handler urls: ["/**"] # URL patterns to match order: 100 # Handler priority (lower = higher priority) graphql: enabled: true urls: ["/graphql", "/graphiql"] order: 0 ``` -------------------------------- ### Spring Security Exception Handler Library Core Components Source: https://github.com/clutcher/spring-security-exception-handler/blob/main/README.md This section outlines the main interfaces and classes that form the foundation of the Spring Security Exception Handler library. It describes the purpose and role of each component, including the filter responsible for interception, the main handler interface, the fluent builder API, URL matching predicates, response writers, and utility classes for exception mapping. ```APIDOC SpringSecurityExceptionFilter: Servlet filter that intercepts Spring Security exceptions and delegates to registered handlers. SpringSecurityExceptionHandler: Main interface for handling security exceptions. Implements `Ordered` to support priority-based handler selection. SpringSecurityExceptionHandlerBuilder: Fluent builder API for creating custom exception handlers with predicates and response writers. UrlMatchingPredicate: Predicate implementation for URL pattern matching using Spring's `AntPathMatcher`. ErrorResponseWritingConsumer: Consumer for writing structured error responses with customizable status codes and content. ExceptionMappingFunctions: Utility class providing pre-built exception mapping functions for common use cases. ``` -------------------------------- ### Configure Custom API V2 Exception Handler in Java Source: https://github.com/clutcher/spring-security-exception-handler/blob/main/README.md This Java configuration class defines a custom Spring Security exception handler specifically for API v2 endpoints. It utilizes the `SpringSecurityExceptionHandlerBuilder` to match URLs, handle `AuthenticationException` and `AccessDeniedException` by returning specific HTTP status codes and JSON error responses, and sets a processing order for the handler. ```java @Configuration public class CustomExceptionHandlerConfig { @Bean public SpringSecurityExceptionHandler apiV2ExceptionHandler() { return SpringSecurityExceptionHandlerBuilder.builder() .canHandle(new UrlMatchingPredicate(List.of("/api/v2/**"))) .handle(new ErrorResponseWritingConsumer(exception -> { if (exception instanceof AuthenticationException) { return new ErrorResponseWritingConsumer.ErrorResponse( HttpServletResponse.SC_UNAUTHORIZED, "{\"error\":\"Authentication required\",\"code\":\"AUTH_REQUIRED\"}" ); } if (exception instanceof AccessDeniedException) { return new ErrorResponseWritingConsumer.ErrorResponse( HttpServletResponse.SC_FORBIDDEN, "{\"error\":\"Access denied\",\"code\":\"ACCESS_DENIED\"}" ); } return new ErrorResponseWritingConsumer.ErrorResponse( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "{\"error\":\"Internal server error\"}" ); })) .order(50) .build(); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.