|
| 1 | +package org.minbox.framework.api.boot.plugin.oauth.translator; |
| 2 | + |
| 3 | +import org.minbox.framework.api.boot.plugin.oauth.exception.ApiBootOAuth2Exception; |
| 4 | +import org.minbox.framework.api.boot.plugin.oauth.response.AuthorizationDeniedResponse; |
| 5 | +import org.springframework.http.HttpHeaders; |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.security.access.AccessDeniedException; |
| 9 | +import org.springframework.security.core.AuthenticationException; |
| 10 | +import org.springframework.security.oauth2.common.DefaultThrowableAnalyzer; |
| 11 | +import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException; |
| 12 | +import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; |
| 13 | +import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator; |
| 14 | +import org.springframework.security.web.util.ThrowableAnalyzer; |
| 15 | +import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | + |
| 19 | +/** |
| 20 | + * Override the default WebResponseExceptionTranslator |
| 21 | + * Customize the error message format returned by the authentication server |
| 22 | + * |
| 23 | + * @author 恒宇少年 |
| 24 | + * @see org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator |
| 25 | + */ |
| 26 | +public class ApiBootWebResponseExceptionTranslator implements WebResponseExceptionTranslator { |
| 27 | + /** |
| 28 | + * Response in case of authentication error |
| 29 | + */ |
| 30 | + private AuthorizationDeniedResponse authorizationDeniedResponse; |
| 31 | + |
| 32 | + public ApiBootWebResponseExceptionTranslator(AuthorizationDeniedResponse authorizationDeniedResponse) { |
| 33 | + this.authorizationDeniedResponse = authorizationDeniedResponse; |
| 34 | + } |
| 35 | + |
| 36 | + private ThrowableAnalyzer throwableAnalyzer = new DefaultThrowableAnalyzer(); |
| 37 | + |
| 38 | + @Override |
| 39 | + public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception { |
| 40 | + Throwable[] causeChain = this.throwableAnalyzer.determineCauseChain(e); |
| 41 | + |
| 42 | + Exception ase = (OAuth2Exception) this.throwableAnalyzer.getFirstThrowableOfType(OAuth2Exception.class, causeChain); |
| 43 | + if (ase != null) { |
| 44 | + return this.handleOAuth2Exception((OAuth2Exception) ase); |
| 45 | + } |
| 46 | + |
| 47 | + ase = (AuthenticationException) this.throwableAnalyzer.getFirstThrowableOfType(AuthenticationException.class, causeChain); |
| 48 | + if (ase != null) { |
| 49 | + return this.handleOAuth2Exception(new UnauthorizedException(e.getMessage(), e)); |
| 50 | + } |
| 51 | + |
| 52 | + ase = (AccessDeniedException) this.throwableAnalyzer.getFirstThrowableOfType(AccessDeniedException.class, causeChain); |
| 53 | + if (ase instanceof AccessDeniedException) { |
| 54 | + return this.handleOAuth2Exception(new ForbiddenException(ase.getMessage(), ase)); |
| 55 | + } |
| 56 | + |
| 57 | + ase = (HttpRequestMethodNotSupportedException) throwableAnalyzer.getFirstThrowableOfType( |
| 58 | + HttpRequestMethodNotSupportedException.class, causeChain); |
| 59 | + if (ase instanceof HttpRequestMethodNotSupportedException) { |
| 60 | + return handleOAuth2Exception(new MethodNotAllowed(ase.getMessage(), ase)); |
| 61 | + } |
| 62 | + |
| 63 | + return this.handleOAuth2Exception(new ServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), e)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Handling Formatted OAuth2Exception Response |
| 68 | + * |
| 69 | + * @param e {@link OAuth2Exception} |
| 70 | + * @return {@link ResponseEntity} |
| 71 | + * @throws IOException |
| 72 | + */ |
| 73 | + private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) throws IOException { |
| 74 | + int status = e.getHttpErrorCode(); |
| 75 | + HttpHeaders headers = new HttpHeaders(); |
| 76 | + headers.set("Cache-Control", "no-store"); |
| 77 | + headers.set("Pragma", "no-cache"); |
| 78 | + if (status == HttpStatus.UNAUTHORIZED.value() || e instanceof InsufficientScopeException) { |
| 79 | + headers.set("WWW-Authenticate", String.format("%s %s", "Bearer", e.getSummary())); |
| 80 | + } |
| 81 | + |
| 82 | + // use ApiBootOAuth2Exception as the returned exception type |
| 83 | + ApiBootOAuth2Exception apiBootOAuth2Exception = new ApiBootOAuth2Exception(e.getMessage(), e, authorizationDeniedResponse); |
| 84 | + // get custom authorization definition response HttpStatus |
| 85 | + HttpStatus httpStatus = authorizationDeniedResponse.getHttpStatus(); |
| 86 | + ResponseEntity<OAuth2Exception> response = new ResponseEntity(apiBootOAuth2Exception, headers, httpStatus); |
| 87 | + return response; |
| 88 | + } |
| 89 | + |
| 90 | + private static class MethodNotAllowed extends OAuth2Exception { |
| 91 | + public MethodNotAllowed(String msg, Throwable t) { |
| 92 | + super(msg, t); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String getOAuth2ErrorCode() { |
| 97 | + return HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public int getHttpErrorCode() { |
| 102 | + return HttpStatus.METHOD_NOT_ALLOWED.value(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static class UnauthorizedException extends OAuth2Exception { |
| 107 | + public UnauthorizedException(String msg, Throwable t) { |
| 108 | + super(msg, t); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String getOAuth2ErrorCode() { |
| 113 | + return HttpStatus.UNAUTHORIZED.getReasonPhrase(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public int getHttpErrorCode() { |
| 118 | + return HttpStatus.UNAUTHORIZED.value(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static class ServerErrorException extends OAuth2Exception { |
| 123 | + public ServerErrorException(String msg, Throwable t) { |
| 124 | + super(msg, t); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String getOAuth2ErrorCode() { |
| 129 | + return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public int getHttpErrorCode() { |
| 134 | + return HttpStatus.INTERNAL_SERVER_ERROR.value(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static class ForbiddenException extends OAuth2Exception { |
| 139 | + public ForbiddenException(String msg, Throwable t) { |
| 140 | + super(msg, t); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String getOAuth2ErrorCode() { |
| 145 | + return HttpStatus.FORBIDDEN.getReasonPhrase(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public int getHttpErrorCode() { |
| 150 | + return HttpStatus.FORBIDDEN.value(); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments