Example usage for org.springframework.security.oauth2.common.exceptions OAuth2Exception getHttpErrorCode

List of usage examples for org.springframework.security.oauth2.common.exceptions OAuth2Exception getHttpErrorCode

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.common.exceptions OAuth2Exception getHttpErrorCode.

Prototype

public int getHttpErrorCode() 

Source Link

Document

The HTTP error code associated with this error.

Usage

From source file:org.joyrest.oauth2.handler.OAuthExceptionConfiguration.java

private void process(Request<?> req, Response<OAuth2Exception> resp, OAuth2Exception ex) {
    resp.entity(ex);/* ww w . j a va2 s  .c o m*/

    int status = ex.getHttpErrorCode();
    resp.status(HttpStatus.of(status));

    resp.header(CACHE_CONTROL, "no-store");
    resp.header(PRAGMA, "no-cache");
    if (status == HttpStatus.UNAUTHORIZED.code() || (ex instanceof InsufficientScopeException)) {
        resp.header(WWW_AUTHENTICATE, String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, ex.getSummary()));
    }
}

From source file:com.ge.predix.acceptance.test.zone.admin.ZoneEnforcementStepsDefinitions.java

@When("^client_two does a GET on (.*?) with (.*?) in zone (.*?)$")
public void client_two_does_a_GET_on_subject_with_subject_id__in_zone(final String api, final String identifier,
        final String subdomainSuffix) throws Throwable {

    OAuth2RestTemplate acsTemplate = this.acsZone2Template;
    String encodedIdentifier = URLEncoder.encode(identifier, "UTF-8");
    HttpHeaders zoneHeaders = new HttpHeaders();
    // differentiate between zone 1 and zone 2, which will have slightly different uris
    zoneHeaders.set(PolicyHelper.PREDIX_ZONE_ID, getZoneName(subdomainSuffix));

    URI uri = URI.create(this.acsUrl + ACS_VERSION + "/" + api + "/" + encodedIdentifier);
    try {//from  www .  j av  a 2  s.c om
        switch (api) {
        case "subject":
            this.responseEntity = acsTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(zoneHeaders),
                    BaseSubject.class);
            this.status = this.responseEntity.getStatusCode().value();
            break;
        case "resource":
            this.responseEntityForResource = acsTemplate.exchange(uri, HttpMethod.GET,
                    new HttpEntity<>(zoneHeaders), BaseResource.class);
            this.status = this.responseEntityForResource.getStatusCode().value();
            break;
        case "policy-set":
            this.policyset = acsTemplate.exchange(
                    this.acsUrl + PolicyHelper.ACS_POLICY_SET_API_PATH + this.testPolicyName, HttpMethod.GET,
                    new HttpEntity<>(zoneHeaders), PolicySet.class);
            this.status = this.policyset.getStatusCode().value();
            break;
        default:
            Assert.fail("Api " + api + " does not match/is not yet implemented for this test code.");
        }
    } catch (OAuth2Exception e) {
        this.status = e.getHttpErrorCode();
    }
}

From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java

/**
 * This test is designed to test having a bad secret.
 *///from w w  w  . ja  v a 2 s . co  m
@Test
public void testBadClientSecret() {
    OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentialsWithBadSecret();
    try {
        restTemplate.getAccessToken();
        fail("Expected OAuth2AccessDeniedException, but none was thrown");
    } catch (OAuth2AccessDeniedException ex) {
        if (ex.getCause() instanceof OAuth2Exception) {
            OAuth2Exception clientException = (OAuth2Exception) ex.getCause();
            assertEquals(HttpStatus.BAD_REQUEST.value(), clientException.getHttpErrorCode());
        } else if (ex.getCause() instanceof ResourceAccessException) {
            fail("It appears that the server may not be running. Please start it before running tests");
        } else {
            fail(String.format("Expected HttpClientErrorException. Got %s",
                    ex.getCause().getClass().getName()));
        }
    } catch (Exception ex) {
        fail(ex.getMessage());
    }
}

From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaController.java

@ExceptionHandler(OAuth2Exception.class)
public ModelAndView handleOAuth2Exception(OAuth2Exception e, ServletWebRequest webRequest) throws Exception {
    logger.info(e.getSummary());//  ww  w  .j  a va 2  s. c o m
    int errorCode = e.getHttpErrorCode();
    if (errorCode != 401 && "Bad credentials".equals(e.getMessage())) {
        //https://github.com/spring-projects/spring-security-oauth/issues/191
        errorCode = 401;
    }
    webRequest.getResponse().setStatus(errorCode);
    return new ModelAndView("forward:/home", Collections.singletonMap("error", e.getSummary()));
}

From source file:org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator.java

private ResponseEntity<OAuth2Exception> handleOAuth2Exception(OAuth2Exception e) throws IOException {

    if (logger.isDebugEnabled()) {
        logger.debug("OAuth error.", e);
    }//  ww w . ja  v a 2s . c  o m

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

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

    return response;

}