Example usage for org.springframework.security.oauth2.client.token.grant.password ResourceOwnerPasswordResourceDetails getUsername

List of usage examples for org.springframework.security.oauth2.client.token.grant.password ResourceOwnerPasswordResourceDetails getUsername

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.client.token.grant.password ResourceOwnerPasswordResourceDetails getUsername.

Prototype

public String getUsername() 

Source Link

Usage

From source file:org.shaigor.rest.retro.client.oauth.OAuthPostAuthListener.java

@Override
public void onApplicationEvent(AbstractAuthenticationEvent event) {
    Authentication authentication = event.getAuthentication();

    if (event instanceof AuthenticationSuccessEvent) {
        ResourceOwnerPasswordResourceDetails resource = getResourceOwnerPasswordResourceDetails();
        resource.setScope(Arrays.asList("words"));
        resource.setUsername(authentication.getName());
        resource.setPassword(authentication.getCredentials().toString());

        try {//  w ww .  j  av a  2 s. c  om
            OAuth2AccessToken accessToken = accessTokenProvider.obtainAccessToken(resource,
                    new DefaultAccessTokenRequest());
            log.debug("Access token request succeeded for user: '{}', new token is '{}'",
                    resource.getUsername(), accessToken.getValue());
            if (authentication instanceof AbstractAuthenticationToken
                    && authentication.getDetails() instanceof CustomAuthenticationDetails) {
                ((CustomAuthenticationDetails) ((AbstractAuthenticationToken) authentication).getDetails())
                        .setBearer(accessToken.getValue());
                log.debug("Access token was added to authentication as details");
            } else if (log.isDebugEnabled()) {
                log.debug("Access token could not be added to authentication as details");
            }
        } catch (Exception e) {
            log.error("Access token request failed for user: '" + resource.getUsername() + "'", e);
        }
    }
    if (authentication instanceof CredentialsContainer) {
        // Authentication is complete. Remove credentials and other secret data from authentication
        ((CredentialsContainer) authentication).eraseCredentials();
    }

}