org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails Java Examples

The following examples show how to use org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails. 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: 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: 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 #4
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 #5
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 #6
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 #7
Source File: OAuth2AutoConfigurationTests.java    From spring-security-oauth2-boot with Apache License 2.0 6 votes vote down vote up
@Test
public void testCanUseClientCredentialsWithEnableOAuth2Client() {
	this.context = new AnnotationConfigServletWebServerApplicationContext();
	this.context.register(ClientConfiguration.class, MinimalSecureWebApplication.class);
	TestPropertyValues
			.of("security.oauth2.client.clientId=client", "security.oauth2.client.grantType=client_credentials")
			.applyTo(this.context);
	ConfigurationPropertySources.attach(this.context.getEnvironment());
	this.context.refresh();
	// The primary context is fine (not session scoped):
	OAuth2ClientContext bean = this.context.getBean(OAuth2ClientContext.class);
	assertThat(bean.getAccessTokenRequest()).isNotNull();
	assertThat(countBeans(ClientCredentialsResourceDetails.class)).isEqualTo(1);
	// Kind of a bug (should ideally be 1), but the cause is in Spring OAuth2 (there
	// is no need for the extra session-scoped bean). What this test proves is that
	// even if the user screws up and does @EnableOAuth2Client for client
	// credentials,
	// it will still just about work (because of the @Primary annotation on the
	// Boot-created instance of OAuth2ClientContext).
	assertThat(countBeans(OAuth2ClientContext.class)).isEqualTo(2);
}
 
Example #8
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 #9
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 #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: 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 #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: 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 #14
Source File: CustomConfigAuthorizationServerIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenOAuth2Context_whenAccessTokenIsRequestedByClientWithWriteScope_ThenAccessTokenIsNotNull() {
    ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung-admin", singletonList("write"));
    OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);

    OAuth2AccessToken accessToken = restTemplate.getAccessToken();

    assertNotNull(accessToken);
}
 
Example #15
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 #16
Source File: CustomConfigAuthorizationServerIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenOAuth2Context_whenAccessTokenIsRequested_ThenAccessTokenValueIsNotNull() {
    ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read"));
    OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);

    OAuth2AccessToken accessToken = restTemplate.getAccessToken();

    assertNotNull(accessToken);
}
 
Example #17
Source File: CustomConfigAuthorizationServerIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenOAuth2Context_whenAccessingAuthentication_ThenRespondTokenDetails() {
    ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read"));
    OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);

    String authentication = executeGetRequest(restTemplate, "/authentication");

    Pattern pattern = Pattern.compile("\\{\"remoteAddress\":\".*" +
            "\",\"sessionId\":null,\"tokenValue\":\".*" +
            "\",\"tokenType\":\"Bearer\",\"decodedDetails\":null}");
    assertTrue("authentication", pattern.matcher(authentication).matches());
}
 
Example #18
Source File: CustomConfigAuthorizationServerIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenOAuth2Context_whenAccessingPrincipal_ThenRespondBaeldung() {
    ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("read"));
    OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);

    String principal = executeGetRequest(restTemplate, "/principal");

    assertEquals("baeldung", principal);
}
 
Example #19
Source File: CustomConfigAuthorizationServerIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test(expected = OAuth2AccessDeniedException.class)
public void givenOAuth2Context_whenAccessTokenIsRequestedWithInvalidException_ThenExceptionIsThrown() {
    ClientCredentialsResourceDetails resourceDetails = getClientCredentialsResourceDetails("baeldung", singletonList("write"));
    OAuth2RestTemplate restTemplate = getOAuth2RestTemplate(resourceDetails);

    restTemplate.getAccessToken();
}
 
Example #20
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 #21
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 #22
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 #23
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 #24
Source File: OAuth2RestOperationsConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Bean
@ConfigurationProperties(prefix = "security.oauth2.client")
@Primary
public ClientCredentialsResourceDetails oauth2RemoteResource() {
	ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
	return details;
}
 
Example #25
Source File: OAuth2AutoConfigurationTests.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanUseClientCredentials() {
	this.context = new AnnotationConfigServletWebServerApplicationContext();
	this.context.register(TestSecurityConfiguration.class, MinimalSecureWebApplication.class);
	TestPropertyValues
			.of("security.oauth2.client.clientId=client", "security.oauth2.client.grantType=client_credentials")
			.applyTo(this.context);
	ConfigurationPropertySources.attach(this.context.getEnvironment());
	this.context.refresh();
	OAuth2ClientContext bean = this.context.getBean(OAuth2ClientContext.class);
	assertThat(bean.getAccessTokenRequest()).isNotNull();
	assertThat(countBeans(ClientCredentialsResourceDetails.class)).isEqualTo(1);
	assertThat(countBeans(OAuth2ClientContext.class)).isEqualTo(1);
}
 
Example #26
Source File: OAuth2AutoConfigurationTests.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientIsNotAuthCode() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.register(MinimalSecureNonWebApplication.class);
	TestPropertyValues.of("security.oauth2.client.clientId=client").applyTo(context);
	context.refresh();
	assertThat(countBeans(context, ClientCredentialsResourceDetails.class)).isEqualTo(1);
	context.close();
}
 
Example #27
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 #28
Source File: OAuth2RestOperationsConfigurationTests.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void clientCredentialsWithClientId() throws Exception {
	TestPropertyValues.of("security.oauth2.client.client-id=acme").applyTo(this.environment);
	initializeContext(OAuth2RestOperationsConfiguration.class, true);
	assertThat(this.context.getBean(OAuth2RestOperationsConfiguration.class)).isNotNull();
	assertThat(this.context.getBean(ClientCredentialsResourceDetails.class)).isNotNull();
}
 
Example #29
Source File: EurekaOAuth2RequestDecorator.java    From spring-cloud-services-connector with Apache License 2.0 4 votes vote down vote up
public EurekaOAuth2RequestDecorator(ClientCredentialsResourceDetails resourceDetails) {
	oauth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
	requestAuthenticator = new DefaultOAuth2RequestAuthenticator();
}
 
Example #30
Source File: OAuth2IntegrationTestSupport.java    From tutorials with MIT License 4 votes vote down vote up
protected OAuth2RestTemplate getOAuth2RestTemplate(final ClientCredentialsResourceDetails resourceDetails) {
    DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
    restTemplate.setMessageConverters(singletonList(new MappingJackson2HttpMessageConverter()));
    return restTemplate;
}