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

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

Introduction

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

Prototype

public int getHttpErrorCode() 

Source Link

Document

The HTTP error code associated with this error.

Usage

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//from  w w w .  java 2 s  . c om
 */
@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
 *//*from w  ww  .  jav  a  2  s.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());
    }
}