-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[maven-release-plugin] copy for tag 4.3.0
- Loading branch information
Showing
14 changed files
with
445 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
</parent> | ||
<groupId>io.github.wimdeblauwe</groupId> | ||
<artifactId>error-handling-spring-boot-starter</artifactId> | ||
<version>4.2.0</version> | ||
<version>4.3.0</version> | ||
<name>Error Handling Spring Boot Starter</name> | ||
<description>Spring Boot starter that configures error handling</description> | ||
|
||
|
@@ -50,7 +50,7 @@ | |
</developerConnection> | ||
<connection>scm:git:[email protected]:wimdeblauwe/error-handling-spring-boot-starter.git</connection> | ||
<url>[email protected]:wimdeblauwe/error-handling-spring-boot-starter.git</url> | ||
<tag>4.2.0</tag> | ||
<tag>4.3.0</tag> | ||
</scm> | ||
<issueManagement> | ||
<system>github</system> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...ithub/wimdeblauwe/errorhandlingspringbootstarter/ApiErrorResponseAccessDeniedHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.github.wimdeblauwe.errorhandlingspringbootstarter; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.ErrorCodeMapper; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.ErrorMessageMapper; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.mapper.HttpStatusMapper; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Use this {@link AccessDeniedHandler} implementation if you want to have a consistent response | ||
* with how this library works when the user is not allowed to access a resource. | ||
* <p> | ||
* It is impossible for the library to provide auto-configuration for this. So you need to manually add | ||
* this to your security configuration. For example: | ||
* | ||
* <pre> | ||
* public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {* | ||
* @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(); | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* @see UnauthorizedEntryPoint | ||
*/ | ||
public class ApiErrorResponseAccessDeniedHandler implements AccessDeniedHandler { | ||
private final ObjectMapper objectMapper; | ||
private final HttpStatusMapper httpStatusMapper; | ||
private final ErrorCodeMapper errorCodeMapper; | ||
private final ErrorMessageMapper errorMessageMapper; | ||
|
||
public ApiErrorResponseAccessDeniedHandler(ObjectMapper objectMapper, HttpStatusMapper httpStatusMapper, ErrorCodeMapper errorCodeMapper, | ||
ErrorMessageMapper errorMessageMapper) { | ||
this.objectMapper = objectMapper; | ||
this.httpStatusMapper = httpStatusMapper; | ||
this.errorCodeMapper = errorCodeMapper; | ||
this.errorMessageMapper = errorMessageMapper; | ||
} | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) | ||
throws IOException, ServletException { | ||
ApiErrorResponse errorResponse = createResponse(accessDeniedException); | ||
|
||
response.setStatus(errorResponse.getHttpStatus().value()); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setCharacterEncoding(StandardCharsets.UTF_8.toString()); | ||
response.getWriter().write(objectMapper.writeValueAsString(errorResponse)); | ||
} | ||
|
||
public ApiErrorResponse createResponse(AccessDeniedException exception) { | ||
HttpStatusCode httpStatus = httpStatusMapper.getHttpStatus(exception, HttpStatus.FORBIDDEN); | ||
String code = errorCodeMapper.getErrorCode(exception); | ||
String message = errorMessageMapper.getErrorMessage(exception); | ||
|
||
return new ApiErrorResponse(httpStatus, code, message); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/io/github/wimdeblauwe/errorhandlingspringbootstarter/ErrorHandlingFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.github.wimdeblauwe.errorhandlingspringbootstarter; | ||
|
||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ErrorHandlingFacade { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ErrorHandlingFacade.class); | ||
|
||
private final List<ApiExceptionHandler> handlers; | ||
private final FallbackApiExceptionHandler fallbackHandler; | ||
private final LoggingService loggingService; | ||
private final List<ApiErrorResponseCustomizer> responseCustomizers; | ||
|
||
public ErrorHandlingFacade(List<ApiExceptionHandler> handlers, FallbackApiExceptionHandler fallbackHandler, LoggingService loggingService, | ||
List<ApiErrorResponseCustomizer> responseCustomizers) { | ||
this.handlers = handlers; | ||
this.fallbackHandler = fallbackHandler; | ||
this.loggingService = loggingService; | ||
this.responseCustomizers = responseCustomizers; | ||
} | ||
|
||
public ApiErrorResponse handle(Throwable exception) { | ||
ApiErrorResponse errorResponse = null; | ||
for (ApiExceptionHandler handler : handlers) { | ||
if (handler.canHandle(exception)) { | ||
errorResponse = handler.handle(exception); | ||
break; | ||
} | ||
} | ||
|
||
if (errorResponse == null) { | ||
errorResponse = fallbackHandler.handle(exception); | ||
} | ||
|
||
for (ApiErrorResponseCustomizer responseCustomizer : responseCustomizers) { | ||
responseCustomizer.customize(errorResponse); | ||
} | ||
|
||
loggingService.logException(errorResponse, exception); | ||
|
||
return errorResponse; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.