Example usage for org.springframework.security.oauth2.client.resource OAuth2AccessDeniedException getMessage

List of usage examples for org.springframework.security.oauth2.client.resource OAuth2AccessDeniedException getMessage

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.client.resource OAuth2AccessDeniedException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.cloudfoundry.client.lib.oauth2.OauthClient.java

private OAuth2AccessToken createToken(String username, String password, String clientId, String clientSecret) {
    OAuth2ProtectedResourceDetails resource = getResourceDetails(username, password, clientId, clientSecret);
    AccessTokenRequest request = createAccessTokenRequest(username, password);

    ResourceOwnerPasswordAccessTokenProvider provider = createResourceOwnerPasswordAccessTokenProvider();
    try {/*from   w  w w . j  a v a  2s . co  m*/
        return provider.obtainAccessToken(resource, request);
    } catch (OAuth2AccessDeniedException oauthEx) {
        HttpStatus status = HttpStatus.valueOf(oauthEx.getHttpErrorCode());
        CloudFoundryException cfEx = new CloudFoundryException(status, oauthEx.getMessage());
        cfEx.setDescription(oauthEx.getSummary());
        throw cfEx;
    }
}

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

/**
 * Verify that authentication is successful.
 *///from ww w.  jav  a  2s .co 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//w  ww . j a  va2 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./*w ww  . ja  v a  2s . 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 ww w . j  a  v  a  2s  .c  o 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.ja v  a 2s.  c  om*/
@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
 *//*w  w w.j  a  va  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//  w ww .j av  a2  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());
    }
}

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

/**
 * Test secure endpoint with authentication
 *//*from  ww  w.  ja  v a  2  s.co  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:com.bcknds.demo.oauth2.security.PasswordAuthenticationTests.java

/**
 * Test using a bad username//from w  w w  .j av a  2  s.c  o  m
 */
@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());
    }
}