List of usage examples for org.springframework.security.oauth2.client.resource OAuth2AccessDeniedException getCause
public synchronized Throwable getCause()
From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java
/** * Test secure endpoint with authentication *//* ww w . j av a2s . com*/ @Test public void testSecureEndpoint() { // Test while logged in. OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentials(); ResponseEntity<String> response = null; try { response = restTemplate.getForEntity(SECURE_ENDPOINT, String.class); assertEquals("You are a VIP since you have secure access.", response.getBody()); assertEquals(HttpStatus.OK, response.getStatusCode()); } catch (OAuth2AccessDeniedException ex) { if (ex.getCause() instanceof ResourceAccessException) { fail("It appears that the server may not be running. Please start it before running tests"); } else { fail(ex.getMessage()); } } catch (Exception ex) { fail(ex.getMessage()); } }
From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java
/** * This test is designed to test having a bad client Id. *//*from w w w . j a v a2s. c o m*/ @Test public void testBadClientId() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentialsWithBadClientId(); try { restTemplate.getAccessToken(); fail("Expected OAuth2AccessDeniedException, but none was thrown"); } catch (OAuth2AccessDeniedException ex) { if (ex.getCause() instanceof HttpClientErrorException) { HttpClientErrorException clientException = (HttpClientErrorException) ex.getCause(); assertEquals(HttpStatus.UNAUTHORIZED, clientException.getStatusCode()); } 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:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java
/** * This test is designed to test having a bad secret. *//*from w w w . ja va 2 s . c om*/ @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:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java
/** * This test is designed to test having a bad scope. *//* w w w . j a v a 2s. c o m*/ @Test public void testBadScope() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentialsWithBadScope(); try { restTemplate.getAccessToken(); fail("Expected OAuth2AccessDeniedException, but none was thrown"); } catch (OAuth2AccessDeniedException ex) { if (ex.getCause() instanceof InvalidScopeException) { InvalidScopeException clientException = (InvalidScopeException) 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.springframework.security.oauth2.client.test.OAuth2ContextSetup.java
/** * Get the current access token. Should be available inside a test method as long as a resource has been setup with * {@link OAuth2ContextConfiguration @OAuth2ContextConfiguration}. * //from ww w .jav a 2 s .c o m * @return the current access token initializing it if necessary */ public OAuth2AccessToken getAccessToken() { if (resource == null || client == null) { return null; } if (accessToken != null) { return accessToken; } try { if (accessTokenProvider != null) { client.setAccessTokenProvider(accessTokenProvider); } return client.getAccessToken(); } catch (OAuth2AccessDeniedException e) { Throwable cause = e.getCause(); if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } if (cause instanceof Error) { throw (Error) cause; } throw e; } }