Java Code Examples for org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails#setScope()

The following examples show how to use org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails#setScope() . 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: ClientConfiguration.java    From OAuth-2.0-Cookbook with MIT License 6 votes vote down vote up
@Bean
public OAuth2ProtectedResourceDetails passwordResourceDetails() {
    //@formatter:off
ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();

details.setId("oauth2server");
details.setTokenName("oauth_token");
details.setClientId("clientadmin");
details.setClientSecret("123");
details.setAccessTokenUri("http://localhost:8080/oauth/token");
details.setScope(Arrays.asList("admin"));

details.setClientAuthenticationScheme(AuthenticationScheme.header);
//@formatter:on

    return details;
}
 
Example 2
Source File: Oauth2ClientRestTemplate.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 演示 grant_type=client_credentials 时,获取资源的方法
 *
 * @param client_id
 * @param client_secret    取决于 AuthorizationServer 设置,如果 client 设置了secret,则此项参数为必需,否则可以没有
 * @param access_token_uri
 * @param scope
 * @return
 */
public OAuth2RestOperations clientCredentialsRestTemplate(String client_id, String client_secret, String access_token_uri, String... scope) {

    // 防止 url 写错
    if (!access_token_uri.contains("token"))
        throw new RuntimeException("uri is wrong :  access_token_uri = " + access_token_uri);

    ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
    details.setId("4");
    details.setClientId(client_id);
    if (client_secret != null && !client_secret.isEmpty())
        details.setClientSecret(client_secret);
    details.setAccessTokenUri(access_token_uri);
    details.setScope(Arrays.asList(scope));
    return new OAuth2RestTemplate(details, oAuth2ClientContext);
}
 
Example 3
Source File: UaaClientOperationTest.java    From uaa-java-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testChangeClientSecretForTestUser() throws Exception {

	ClientCredentialsResourceDetails testUserCredentials = getDefaultClientCredentials();
	testUserCredentials.setClientId(testClientDetails.getClientId());
	testUserCredentials.setClientSecret(testClientDetails.getClientSecret());
	testUserCredentials.setScope(new ArrayList<String>(testClientDetails.getScope()));

	UaaClientOperations clientOperations = getConnection(testUserCredentials).clientOperations();

	String clientSecret = testClientDetails.getClientSecret();

	assertTrue("Could not change password",
			clientOperations.changeClientSecret(testClientDetails.getClientId(), clientSecret, "newSecret"));

	try {
		clientOperations.changeClientSecret(testClientDetails.getClientId(), clientSecret, "shouldfail");
		fail("First password change failed");
	} catch (Exception expected) {
		expected.toString(); // to avoid empty catch-block.
	}
}
 
Example 4
Source File: OAuth2FeignAutoConfiguration.java    From spring-cloud-shop with MIT License 5 votes vote down vote up
/**
 * Resource details client credentials resource details.
 *
 * @return the client credentials resource details
 */
@Bean("resourceOwnerPasswordResourceDetails")
public ClientCredentialsResourceDetails resourceDetails() {
    ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
    details.setAccessTokenUri(oauth2ClientProperties.getAccessTokenUri());
    details.setClientId(oauth2ClientProperties.getClientId());
    details.setClientSecret(oauth2ClientProperties.getClientSecret());
    details.setClientAuthenticationScheme(AuthenticationScheme.header);
    details.setScope(!CollectionUtils.isEmpty(oauth2ClientProperties.getScopes()) ? oauth2ClientProperties.getScopes() : Collections.singletonList("app"));

    return details;
}
 
Example 5
Source File: UserApplication.java    From cloud-native-microservice-strangler-example with GNU General Public License v3.0 5 votes vote down vote up
@LoadBalanced
@Bean
public OAuth2RestTemplate loadBalancedOauth2RestTemplate(ClientCredentialsResourceDetails resource) {
    ClientCredentialsResourceDetails clientCredentialsResourceDetails = new ClientCredentialsResourceDetails();
    clientCredentialsResourceDetails.setAccessTokenUri(resource.getAccessTokenUri());
    clientCredentialsResourceDetails.setClientId(resource.getClientId());
    clientCredentialsResourceDetails.setClientSecret(resource.getClientSecret());
    clientCredentialsResourceDetails.setClientAuthenticationScheme(resource.getClientAuthenticationScheme());
    clientCredentialsResourceDetails.setScope(resource.getScope());
    clientCredentialsResourceDetails.setGrantType(resource.getGrantType());
    OAuth2RestTemplate auth2RestTemplate = new OAuth2RestTemplate(clientCredentialsResourceDetails);
    auth2RestTemplate.setAccessTokenProvider(new ClientCredentialsAccessTokenProvider());
    return auth2RestTemplate;
}
 
Example 6
Source File: LegacyEdgeApplication.java    From cloud-native-microservice-strangler-example with GNU General Public License v3.0 5 votes vote down vote up
@LoadBalanced
@Bean
public OAuth2RestTemplate loadBalancedOauth2RestTemplate(
        ClientCredentialsResourceDetails resource) {
    ClientCredentialsResourceDetails clientCredentialsResourceDetails = new ClientCredentialsResourceDetails();
    clientCredentialsResourceDetails.setAccessTokenUri(resource.getAccessTokenUri());
    clientCredentialsResourceDetails.setClientId(resource.getClientId());
    clientCredentialsResourceDetails.setClientSecret(resource.getClientSecret());
    clientCredentialsResourceDetails.setClientAuthenticationScheme(resource.getClientAuthenticationScheme());
    clientCredentialsResourceDetails.setScope(resource.getScope());
    clientCredentialsResourceDetails.setGrantType(resource.getGrantType());
    OAuth2RestTemplate auth2RestTemplate = new OAuth2RestTemplate(clientCredentialsResourceDetails);
    auth2RestTemplate.setAccessTokenProvider(new ClientCredentialsAccessTokenProvider());
    return auth2RestTemplate;
}
 
Example 7
Source File: OAuth2IntegrationTestSupport.java    From tutorials with MIT License 5 votes vote down vote up
protected ClientCredentialsResourceDetails getClientCredentialsResourceDetails(final String clientId, final List<String> scopes) {
    ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
    resourceDetails.setAccessTokenUri(format("http://localhost:%d/oauth/token", port));
    resourceDetails.setClientId(clientId);
    resourceDetails.setClientSecret("baeldung");
    resourceDetails.setScope(scopes);
    resourceDetails.setGrantType("client_credentials");
    return resourceDetails;
}