Java Code Examples for org.springframework.security.oauth2.common.DefaultOAuth2AccessToken#setTokenType()

The following examples show how to use org.springframework.security.oauth2.common.DefaultOAuth2AccessToken#setTokenType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Oauth2ClientRestTemplateTest.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientCredentialsRestTemplate() throws Exception {

    ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
     details.setId("4");
    details.setClientId(client_id);
    details.setClientSecret(client_secret);
    details.setAccessTokenUri(access_token_uri);
   // details.setScope(Arrays.asList("read write"));
    OAuth2RestTemplate operations = new OAuth2RestTemplate(details,new DefaultOAuth2ClientContext());
   // OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
    operations.setAccessTokenProvider(new ClientCredentialsAccessTokenProvider());

  //  OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails(),oAuth2ClientContext());
    DefaultOAuth2AccessToken token=(DefaultOAuth2AccessToken)operations.getAccessToken();
    token.setTokenType("Bearer");

    System.out.println("client_id : " + client_id);
    System.out.println("source_url : " + source_url);

  //  OAuth2RestOperations operations = restTemplate.clientCredentialsRestTemplate(client_id, client_secret, access_token_uri, scopes);  // getForObject 发送 get 方法
    System.out.println(JSON.toJSONString(operations.getForObject(source_url, JsonNode.class)));  // getForObject 发送 get 方法

}
 
Example 2
Source File: AccessTokenContextRelay.java    From spring-cloud-security with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt to copy an access token from the security context into the oauth2 context.
 * @return true if the token was copied
 */
public boolean copyToken() {
	if (context.getAccessToken() == null) {
		Authentication authentication = SecurityContextHolder.getContext()
				.getAuthentication();
		if (authentication != null) {
			Object details = authentication.getDetails();
			if (details instanceof OAuth2AuthenticationDetails) {
				OAuth2AuthenticationDetails holder = (OAuth2AuthenticationDetails) details;
				String token = holder.getTokenValue();
				DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(
						token);
				String tokenType = holder.getTokenType();
				if (tokenType != null) {
					accessToken.setTokenType(tokenType);
				}
				context.setAccessToken(accessToken);
				return true;
			}
		}
	}
	return false;
}