Java Code Examples for org.springframework.security.oauth2.common.exceptions.OAuth2Exception#getMessage()

The following examples show how to use org.springframework.security.oauth2.common.exceptions.OAuth2Exception#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CloudResponseExceptionTranslator.java    From smaker with GNU Lesser General Public License v3.0 6 votes vote down vote up
private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) {

		int status = e.getHttpErrorCode();
		HttpHeaders headers = new HttpHeaders();
		headers.set("Cache-Control", "no-store");
		headers.set("Pragma", "no-cache");
		if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {
			headers.set("WWW-Authenticate", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, e.getSummary()));
		}

		// 客户端异常直接返回客户端,不然无法解析
		if (e instanceof ClientAuthenticationException) {
			return new ResponseEntity<>(e, headers,
				HttpStatus.valueOf(status));
		}
		return new ResponseEntity<>(new CloudAuth2Exception(e.getMessage(), e.getOAuth2ErrorCode()), headers,
			HttpStatus.valueOf(status));

	}
 
Example 2
Source File: SophiaWebResponseExceptionTranslator.java    From sophia_scaffolding with Apache License 2.0 6 votes vote down vote up
private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) {

        int status = e.getHttpErrorCode();
        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.CACHE_CONTROL, "no-store");
        headers.set(HttpHeaders.PRAGMA, "no-cache");
        if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {
            headers.set(HttpHeaders.WWW_AUTHENTICATE, String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, e.getSummary()));
        }

        // 客户端异常直接返回客户端,不然无法解析
        if (e instanceof ClientAuthenticationException) {
            return new ResponseEntity<>(e, headers,
                    HttpStatus.valueOf(status));
        }
        return new ResponseEntity<>(new SophiaAuth2Exception(e.getMessage(), e.getOAuth2ErrorCode()), headers,
                HttpStatus.valueOf(status));

    }
 
Example 3
Source File: SophiaWebResponseExceptionTranslator.java    From sophia_scaffolding with Apache License 2.0 6 votes vote down vote up
private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) {

        int status = e.getHttpErrorCode();
        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.CACHE_CONTROL, "no-store");
        headers.set(HttpHeaders.PRAGMA, "no-cache");
        if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {
            headers.set(HttpHeaders.WWW_AUTHENTICATE, String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, e.getSummary()));
        }

        // 客户端异常直接返回客户端,不然无法解析
        if (e instanceof ClientAuthenticationException) {
            return new ResponseEntity<>(e, headers,
                    HttpStatus.valueOf(status));
        }
        return new ResponseEntity<>(new SophiaAuth2Exception(e.getMessage(), e.getOAuth2ErrorCode()), headers,
                HttpStatus.valueOf(status));

    }
 
Example 4
Source File: SophiaWebResponseExceptionTranslator.java    From sophia_scaffolding with Apache License 2.0 6 votes vote down vote up
private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) {

        int status = e.getHttpErrorCode();
        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.CACHE_CONTROL, "no-store");
        headers.set(HttpHeaders.PRAGMA, "no-cache");
        if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {
            headers.set(HttpHeaders.WWW_AUTHENTICATE, String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, e.getSummary()));
        }

        // 客户端异常直接返回客户端,不然无法解析
        if (e instanceof ClientAuthenticationException) {
            return new ResponseEntity<>(e, headers,
                    HttpStatus.valueOf(status));
        }
        return new ResponseEntity<>(new SophiaAuth2Exception(e.getMessage(), e.getOAuth2ErrorCode()), headers,
                HttpStatus.valueOf(status));

    }
 
Example 5
Source File: ApiBootWebResponseExceptionTranslator.java    From api-boot with Apache License 2.0 6 votes vote down vote up
/**
 * Handling Formatted OAuth2Exception Response
 *
 * @param e {@link OAuth2Exception}
 * @return {@link ResponseEntity}
 * @throws IOException
 */
private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) throws IOException {
    int status = e.getHttpErrorCode();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Cache-Control", "no-store");
    headers.set("Pragma", "no-cache");
    if (status == HttpStatus.UNAUTHORIZED.value() || e instanceof InsufficientScopeException) {
        headers.set("WWW-Authenticate", String.format("%s %s", "Bearer", e.getSummary()));
    }

    // use ApiBootOAuth2Exception as the returned exception type
    ApiBootOAuth2Exception apiBootOAuth2Exception = new ApiBootOAuth2Exception(e.getMessage(), e, authorizationDeniedResponse);
    // get custom authorization definition response HttpStatus
    HttpStatus httpStatus = authorizationDeniedResponse.getHttpStatus();
    ResponseEntity<OAuth2Exception> response = new ResponseEntity(apiBootOAuth2Exception, headers, httpStatus);
    return response;
}
 
Example 6
Source File: CustomWebResponseExceptionTranslator.java    From Taroco with Apache License 2.0 6 votes vote down vote up
private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) throws IOException {

        int status = e.getHttpErrorCode();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Cache-Control", "no-store");
        headers.set("Pragma", "no-cache");
        if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {
            headers.set("WWW-Authenticate", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, e.getSummary()));
        }

        final CustomOauth2Exception exception = new CustomOauth2Exception(e.getMessage(), e);
        exception.setOauth2ErrorCode(e.getOAuth2ErrorCode());
        return new ResponseEntity<>(exception, headers,
                HttpStatus.valueOf(status));

    }
 
Example 7
Source File: BootOAuth2WebResponseExceptionTranslator.java    From oauth-boot with MIT License 6 votes vote down vote up
private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) throws IOException {

        int status = e.getHttpErrorCode();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Cache-Control", "no-store");
        headers.set("Pragma", "no-cache");
        if (status == HttpStatus.UNAUTHORIZED.value() || (e instanceof InsufficientScopeException)) {
            headers.set("WWW-Authenticate", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, e.getSummary()));
        }

        BootOAuth2Exception exception = new BootOAuth2Exception(e.getMessage(), e);

        ResponseEntity<OAuth2Exception> response = new ResponseEntity<OAuth2Exception>(exception, headers,
                HttpStatus.valueOf(status));

        return response;

    }
 
Example 8
Source File: SecurityConfiguration.java    From nakadi with MIT License 6 votes vote down vote up
protected Object toJsonResponse(final Object object) throws UnknownStatusCodeException {
    if (object instanceof OAuth2Exception) {
        final OAuth2Exception oae = (OAuth2Exception) object;
        if (oae.getCause() != null) {
            if (oae.getCause() instanceof AuthenticationException) {
                return new ProblemResponse(UNAUTHORIZED, oae.getCause().getMessage());
            }
            return new ProblemResponse(INTERNAL_SERVER_ERROR, oae.getMessage());
        }

        return new ProblemResponse(fromStatusCode(oae.getHttpErrorCode()), oae.getMessage());
    }

    return new ProblemResponse(INTERNAL_SERVER_ERROR,
            "Unrecognized error happened in authentication path");
}
 
Example 9
Source File: AuthorizationController.java    From Taroco with Apache License 2.0 5 votes vote down vote up
/**
 * 自定义错误处理 重写{@link WhitelabelErrorEndpoint}
 *
 * @param request
 * @return
 */
@RequestMapping("/oauth/error")
@ResponseBody
public ResponseEntity<Response> handleError(HttpServletRequest request) {
    Object error = request.getAttribute("error");
    String errorSummary;
    if (error instanceof OAuth2Exception) {
        OAuth2Exception oauthError = (OAuth2Exception) error;
        errorSummary = oauthError.getMessage();
    } else {
        errorSummary = "Unknown error";
    }
    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Response.failure(errorSummary));
}