List of usage examples for org.springframework.security.oauth2.client.resource OAuth2AccessDeniedException getCause
public synchronized Throwable getCause()
From source file:com.companyname.plat.commons.client.Oauth2Operation.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}. * * @return the current access token initializing it if necessary *//*from www. ja v a 2s . co m*/ public static OAuth2AccessToken getAccessToken(OAuth2ProtectedResourceDetails resource, OAuth2RestTemplate client) { if (resource == null || client == null) { return null; } try { 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; } }
From source file:com.bcknds.demo.oauth2.security.PasswordAuthenticationTests.java
/** * Verify that authentication is successful. *///from w w w. j a v a2 s .c o m @Test public void testSuccessfulAuthentication() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials(USERNAME, PASSWORD); OAuth2AccessToken token = null; try { token = restTemplate.getAccessToken(); } 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()); } assertNotNull(token.getValue()); }
From source file:com.bcknds.demo.oauth2.security.PasswordAuthenticationTests.java
/** * Test successful authentication to method secure endpoint that requires only authentication * using password authentication/*from ww w . j av a 2 s. c o m*/ */ @Test public void testPasswordAuthenticationMethodEndpoint() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials(USERNAME, PASSWORD); ResponseEntity<String> response = null; try { response = restTemplate.getForEntity(METHOD_SECURE_ENDPOINT, String.class); assertEquals("This is secured by annotation", 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.PasswordAuthenticationTests.java
/** * Test successful authentication to method secure endpoint that requires the ROLE_PASSWORD * role.//from w w w .jav a2 s . c om */ @Test public void testPasswordAuthenticationRoleEndpoint() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials(USERNAME, PASSWORD); ResponseEntity<String> response = null; try { response = restTemplate.getForEntity(ROLE_SECURE_ENDPOINT, String.class); assertEquals("This is secured by annotation and role.", 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.PasswordAuthenticationTests.java
/** * Test using a bad username/*from w w w. ja v a2 s . com*/ */ @Test public void testBadUsername() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials("badUsername", PASSWORD); try { restTemplate.getAccessToken(); fail("Expected OAuth2AccessDeniedException, but none was thrown"); } catch (OAuth2AccessDeniedException ex) { if (ex.getCause() instanceof InvalidGrantException) { InvalidGrantException clientException = (InvalidGrantException) 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 InvalidGrantException. Got %s", ex.getCause().getClass().getName())); } } catch (Exception ex) { fail(ex.getMessage()); } }
From source file:com.bcknds.demo.oauth2.security.PasswordAuthenticationTests.java
/** * Test using a bad username//from ww w . jav a 2 s . c om */ @Test public void testBadPassword() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials(USERNAME, "badPassword"); try { restTemplate.getAccessToken(); fail("Expected OAuth2AccessDeniedException, but none was thrown"); } catch (OAuth2AccessDeniedException ex) { if (ex.getCause() instanceof InvalidGrantException) { InvalidGrantException clientException = (InvalidGrantException) 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 InvalidGrantException. Got %s", ex.getCause().getClass().getName())); } } catch (Exception ex) { fail(ex.getMessage()); } }
From source file:com.bcknds.demo.oauth2.security.PasswordAuthenticationTests.java
/** * Test successful authentication to method secure endpoint that requires the ROLE_PASSWORD * role./*from w w w . j av a2 s . co m*/ */ @Test public void testPasswordAuthenticationMultipleRole() { Role adminRole = new Role("ROLE_ADMIN"); userService.remove(validUser); validUser.addRole(adminRole); userService.save(validUser); OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials(USERNAME, PASSWORD); ResponseEntity<String> response = null; try { response = restTemplate.getForEntity(ROLE_SECURE_ENDPOINT, String.class); assertEquals("This is secured by annotation and role.", 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
/** * Verify that authentication is successful. *///from w w w . j av a2 s . c o m @Test public void testSuccessfulAuthentication() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentials(); OAuth2AccessToken token = null; try { token = restTemplate.getAccessToken(); } 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()); } assertNotNull(token.getValue()); }
From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java
/** * Test insecure endpoint with authentication *//*from ww w . jav a 2s.co m*/ @Test public void testInsecureEndpoint() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentials(); ResponseEntity<String> response = null; try { response = restTemplate.getForEntity(INSECURE_ENDPOINT, String.class); assertEquals("You are home.", 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
/** * Test successful authentication to method secure endpoint that requires only authentication * using client credentials// w w w . ja va 2 s . co m */ @Test public void testClientCredentialsMethodEndpoint() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentials(); ResponseEntity<String> response = null; try { response = restTemplate.getForEntity(METHOD_SECURE_ENDPOINT, String.class); assertEquals("This is secured by annotation", 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()); } }