Skip to content

Commit

Permalink
Add documentation on ApiErrorResponseAccessDeniedHandler
Browse files Browse the repository at this point in the history
Fixes #88
  • Loading branch information
wimdeblauwe committed Apr 27, 2024
1 parent 146ff78 commit f9aa52a
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,9 @@ With this configuration, 400 Bad Request will be printed on DEBUG level.
401 Unauthorized will be printed on INFO.
Finally, all status code in the 5xx range will be printed on ERROR.

=== Spring Security AuthenticationEntryPoint
=== Spring Security

==== AuthenticationEntryPoint

By default, the library will not provide a response when there is an unauthorized exception.
It is impossible for this library to provide auto-configuration for this.
Expand Down Expand Up @@ -1239,6 +1241,75 @@ public class WebSecurityConfiguration {
<.> Define the UnauthorizedEntryPoint as a bean.
<.> Use the bean in the security configuration.

==== AccessDeniedHandler

Similar to the <<AuthenticationEntryPoint>>, there is also an `AccessDeniedHandler` implementation available at `io.github.wimdeblauwe.errorhandlingspringbootstarter.ApiErrorResponseAccessDeniedHandler`.

Example configuration:

[source,java]
----
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.wimdeblauwe.errorhandlingspringbootstarter.UnauthorizedEntryPoint;
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.ErrorCodeMapper;
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.ErrorMessageMapper;
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.HttpStatusMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
public class WebSecurityConfiguration {
@Bean
public AccessDeniedHandler accessDeniedHandler(HttpStatusMapper httpStatusMapper,
ErrorCodeMapper errorCodeMapper,
ErrorMessageMapper errorMessageMapper,
ObjectMapper objectMapper) { //<.>
return new ApiErrorResponseAccessDeniedHandler(objectMapper, httpStatusMapper, errorCodeMapper, errorMessageMapper);
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http,
AccessDeniedHandler accessDeniedHandler) throws Exception {
http.httpBasic().disable();
http.authorizeHttpRequests().anyRequest().authenticated();
http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);//<.>
return http.build();
}
}
----

<.> Define the AccessDeniedHandler as a bean.
<.> Use the bean in the security configuration.

[NOTE]
====
You can perfectly combine the `AccessDeniedHandler` with the `UnauthorizedEntryPoint`:
[source,java]
----
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http,
UnauthorizedEntryPoint unauthorizedEntryPoint,
AccessDeniedHandler accessDeniedHandler) throws Exception {
http.httpBasic().disable();
http.authorizeHttpRequests().anyRequest().authenticated();
http.exceptionHandling()
.authenticationEntryPoint(unauthorizedEntryPoint)
.accessDeniedHandler(accessDeniedHandler);
return http.build();
}
----
====

=== Handle non-rest controller exceptions

The library is setup in such a way that only exceptions coming from `@RestController` classes are handled.
Expand Down

0 comments on commit f9aa52a

Please sign in to comment.