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

The following examples show how to use org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails#setAccessTokenUri() . 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: LocalServerSecurityWithOAuth2Tests.java    From spring-cloud-skipper with Apache License 2.0 6 votes vote down vote up
@Test
public void testAccessRootUrlWithOAuth2AccessToken() throws Exception {

	final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setGrantType("client_credentials");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

	final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
	final OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken();

	final String accessTokenAsString = accessToken.getValue();

	localSkipperResource.getMockMvc().perform(get("/api").header("Authorization", "bearer " + accessTokenAsString))
			.andDo(print()).andExpect(status().isOk());
}
 
Example 2
Source File: LocalServerSecurityWithOAuth2Tests.java    From spring-cloud-skipper with Apache License 2.0 6 votes vote down vote up
@Test
public void testAccessAboutUrlWithOAuth2AccessToken() throws Exception {

	final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setGrantType("client_credentials");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

	final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
	final OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken();

	final String accessTokenAsString = accessToken.getValue();

	localSkipperResource.getMockMvc()
			.perform(get("/api/about").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
			.andExpect(status().isOk())
			.andExpect(jsonPath("$.versionInfo.server.name", is("Spring Cloud Skipper Server")))
			.andExpect(jsonPath("$.versionInfo.server.version", notNullValue()));
}
 
Example 3
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 4
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 5
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 6
Source File: LocalServerSecurityWithOAuth2Tests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
public void testAccessRootUrlWithOAuth2AccessToken() throws Exception {

	final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setGrantType("client_credentials");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

	final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
	final OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken();

	final String accessTokenAsString = accessToken.getValue();

	localDataflowResource.getMockMvc().perform(get("/").header("Authorization", "bearer " + accessTokenAsString))
			.andDo(print()).andExpect(status().isOk());
}
 
Example 7
Source File: LocalServerSecurityWithOAuth2Tests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
public void testAccessSecurityInfoUrlWithOAuth2AccessToken() throws Exception {

	final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setGrantType("client_credentials");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

	final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
	final OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken();

	final String accessTokenAsString = accessToken.getValue();

	localDataflowResource.getMockMvc()
			.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
			.andExpect(status().isOk())
			.andExpect(jsonPath("$.authenticated", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.authenticationEnabled", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.roles", hasSize(7)));
}
 
Example 8
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;
}
 
Example 9
Source File: OpenRestTemplate.java    From open-cloud with MIT License 5 votes vote down vote up
/**
 * 构建网关Oauth2 client_credentials方式请求
 *
 * @param clientId
 * @param clientSecret
 * @param accessTokenUri
 * @return
 */
public OAuth2RestTemplate buildOAuth2ClientRequest(String clientId, String clientSecret, String accessTokenUri) {
    ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
    resource.setClientId(clientId);
    resource.setClientSecret(clientSecret);
    resource.setAccessTokenUri(accessTokenUri);
    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource);
    return restTemplate;
}
 
Example 10
Source File: AbstractOperationTest.java    From uaa-java-client with Apache License 2.0 5 votes vote down vote up
protected ClientCredentialsResourceDetails getDefaultClientCredentials() {

		ClientCredentialsResourceDetails credentials = new ClientCredentialsResourceDetails();
		credentials.setAccessTokenUri(UAA_BASE_URL + "/oauth/token");
		credentials.setClientAuthenticationScheme(AuthenticationScheme.header);
		credentials.setClientId("admin");
		credentials.setClientSecret("adminsecret");

		return credentials;
	}
 
Example 11
Source File: LocalServerSecurityWithOAuth2AndExternalAuthoritiesTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void testDataflowCallingExternalAuthoritiesServer() throws Exception {
	final String[] roles = {"VIEW", "CREATE", "MANAGE"};

	final ObjectMapper objectMapper = new ObjectMapper();
	externalAuthoritiesServer.enqueue(new MockResponse()
			.setBody(objectMapper.writeValueAsString(roles))
			.addHeader("Content-Type", "application/json"));

	final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setGrantType("client_credentials");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

	Assert.assertEquals(0, externalAuthoritiesServer.getRequestCount());

	final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
	final OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken();

	final String accessTokenAsString = accessToken.getValue();

	localDataflowResource.getMockMvc()
			.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
			.andExpect(status().isOk())
			.andExpect(jsonPath("$.authenticated", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.authenticationEnabled", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.roles", hasSize(3)));

	assertThat(externalAuthoritiesServer.getRequestCount(), is(1));
	final RecordedRequest recordedRequest = externalAuthoritiesServer.takeRequest();
	assertThat(recordedRequest.getHeader("Authorization"), is("Bearer " + accessTokenAsString));
}
 
Example 12
Source File: LocalServerSecurityWithOAuth2Tests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void testAccessSecurityInfoUrlWithOAuth2AccessToken2TimesAndLogout() throws Exception {

	final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails.setGrantType("client_credentials");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

	final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
	final OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken();

	final String accessTokenAsString = accessToken.getValue();

	localDataflowResource.getMockMvc()
			.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
			.andExpect(status().isOk())
			.andExpect(jsonPath("$.authenticated", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.authenticationEnabled", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.roles", hasSize(7)));

	boolean oAuthServerResponse = oAuth2RestTemplate.getForObject("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/revoke_token", Boolean.class);

	assertTrue(Boolean.valueOf(oAuthServerResponse));

	localDataflowResource.getMockMvc()
		.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
		.andExpect(status().isUnauthorized());

	localDataflowResource.getMockMvc()
		.perform(get("/logout").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
		.andExpect(status().isFound());

	localDataflowResource.getMockMvc()
		.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
		.andExpect(status().isUnauthorized());

}
 
Example 13
Source File: LocalServerSecurityWithOAuth2Tests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void testAccessSecurityInfoUrlWithOAuth2AccessToken2Times() throws Exception {

	final ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
	resourceDetails.setClientId("myclient");
	resourceDetails.setClientSecret("mysecret");
	resourceDetails
			.setAccessTokenUri("http://localhost:" + oAuth2ServerResource.getOauth2ServerPort() + "/oauth/token");

	final OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
	final OAuth2AccessToken accessToken = oAuth2RestTemplate.getAccessToken();

	final String accessTokenAsString = accessToken.getValue();

	localDataflowResource.getMockMvc()
			.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
			.andExpect(status().isOk())
			.andExpect(jsonPath("$.authenticated", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.authenticationEnabled", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.roles", hasSize(7)));

	localDataflowResource.getMockMvc()
			.perform(get("/security/info").header("Authorization", "bearer " + accessTokenAsString)).andDo(print())
			.andExpect(status().isOk())
			.andExpect(jsonPath("$.authenticated", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.authenticationEnabled", is(Boolean.TRUE)))
			.andExpect(jsonPath("$.roles", hasSize(7)));
}
 
Example 14
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 15
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 16
Source File: OAuth2FeignAutoConfiguration.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Resource details client credentials resource details.
 *
 * @return the client credentials resource details
 */
@Bean("paascloudClientCredentialsResourceDetails")
public ClientCredentialsResourceDetails resourceDetails() {
	ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
	details.setId(oauth2ClientProperties.getId());
	details.setAccessTokenUri(oauth2ClientProperties.getAccessTokenUrl());
	details.setClientId(oauth2ClientProperties.getClientId());
	details.setClientSecret(oauth2ClientProperties.getClientSecret());
	details.setAuthenticationScheme(AuthenticationScheme.valueOf(oauth2ClientProperties.getClientAuthenticationScheme()));
	return details;
}
 
Example 17
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;
}