List of usage examples for org.springframework.security.oauth2.client OAuth2RestTemplate getForEntity
@Override public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException
From source file:com.bcknds.demo.oauth2.security.PasswordAuthenticationTests.java
/** * Test authentication failure to role secure endpoint that requires the ROLE_PASSWORD role * which this user will not have/*w w w . j a v a2 s . co m*/ */ @Test public void testPasswordAuthenticationBadRole() { userService.remove(validUser); Role badRole = new Role("ROLE_INVALID"); validUser.setRoles(new ArrayList<Role>()); validUser.addRole(badRole); userService.save(validUser); OAuth2RestTemplate restTemplate = AuthenticationUtil.getPasswordCredentials(USERNAME, PASSWORD); try { restTemplate.getForEntity(ROLE_SECURE_ENDPOINT, String.class); fail("Expected exception. None was thrown"); } catch (UserDeniedAuthorizationException ex) { assertEquals(HttpStatus.BAD_REQUEST.value(), ex.getHttpErrorCode()); } catch (Exception ex) { fail(ex.getMessage()); } }
From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java
/** * Test authentication failure to role secure endpoint that requires the ROLE_PASSWORD role * which the client credentials client does not have *//* w w w .ja v a2s . c om*/ @Test public void testClientCredentialsInvalidRole() { OAuth2RestTemplate restTemplate = AuthenticationUtil.getClientCredentials(); try { restTemplate.getForEntity(ROLE_SECURE_ENDPOINT, String.class); fail("Expected exception. None was thrown"); } catch (UserDeniedAuthorizationException ex) { assertEquals(HttpStatus.BAD_REQUEST.value(), ex.getHttpErrorCode()); } 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 only authentication * using password authentication//from www. jav 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 . ja v a 2 s . c o m */ @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 successful authentication to method secure endpoint that requires the ROLE_PASSWORD * role.//from w w w. ja v 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
/** * Test insecure endpoint with authentication *///from w w w .ja v a 2 s. c o 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/*from w w w. ja va2s .com*/ */ @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()); } }
From source file:com.bcknds.demo.oauth2.security.ClientCredentialAuthenticationTests.java
/** * Test secure endpoint with authentication *//*ww w .j a v a 2s .c o m*/ @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:org.springframework.cloud.security.oauth2.UserInfoTokenServices.java
private Map<String, Object> getMap(String path, String accessToken) { logger.info("Getting user info from: " + path); BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails(); resource.setClientId(clientId);/*from w ww.j a va2s. co m*/ OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource); restTemplate.getOAuth2ClientContext().setAccessToken(new DefaultOAuth2AccessToken(accessToken)); @SuppressWarnings("rawtypes") Map map = restTemplate.getForEntity(path, Map.class).getBody(); @SuppressWarnings("unchecked") Map<String, Object> result = map; return result; }