Skip to content

Commit

Permalink
feat: replace whitelabel page with custom error controller
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Jul 14, 2024
1 parent 56d835b commit d75b1d6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.gw2auth</groupId>
<artifactId>oauth2-server</artifactId>
<version>1.73.1</version>
<version>1.74.0</version>
<packaging>jar</packaging>

<parent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.gw2auth.oauth2.server.web;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;

@RestController
public class CustomErrorController extends AbstractErrorController {

@Autowired
public CustomErrorController(ErrorAttributes errorAttributes) {
super(errorAttributes);
}

@GetMapping("/error")
public ResponseEntity<?> error(HttpServletRequest request) {
final Map<String, Object> attributes = getErrorAttributes(request, getErrorAttributeOptions());

return ResponseEntity.status(HttpStatus.FOUND)
.location(
UriComponentsBuilder
.fromUriString(request.getRequestURI())
.replacePath("/error")
.queryParam("status", attributes.get("status"))
.queryParam("error", attributes.get("error"))
.queryParam("message", attributes.get("message"))
.queryParam("path", attributes.get("path"))
.build()
.toUri()
)
.build();
}

protected ErrorAttributeOptions getErrorAttributeOptions() {
return ErrorAttributeOptions.defaults()
.including(ErrorAttributeOptions.Include.EXCEPTION)
.including(ErrorAttributeOptions.Include.MESSAGE)
.including(ErrorAttributeOptions.Include.PATH);
}
}
6 changes: 6 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ com.gw2auth:
interval: ${GW2AUTH_TOKEN_VALID_CHECK_INTERVAL}
ignore-after: ${GW2AUTH_TOKEN_VALID_CHECK_IGNORE_AFTER}

server:
error:
path: '/error'
whitelabel:
enabled: false

spring:
datasource:
url: "jdbc:postgresql://${SERVER_POSTGRES_HOST}:${SERVER_POSTGRES_PORT}/${SERVER_POSTGRES_DB}${SERVER_POSTGRES_OPTIONS}"
Expand Down

0 comments on commit d75b1d6

Please sign in to comment.