-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace whitelabel page with custom error controller
- Loading branch information
Showing
3 changed files
with
56 additions
and
1 deletion.
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
49 changes: 49 additions & 0 deletions
49
src/main/java/com/gw2auth/oauth2/server/web/CustomErrorController.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,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); | ||
} | ||
} |
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