org.springframework.security.oauth2.client.DefaultOAuth2ClientContext Java Examples

The following examples show how to use org.springframework.security.oauth2.client.DefaultOAuth2ClientContext. 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: ApplicationTests.java    From Spring-Microservices with MIT License 6 votes vote down vote up
@Test
public void testOAuthService() {	
       ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
       resource.setUsername("guest");
       resource.setPassword("guest123");
       resource.setAccessTokenUri("http://localhost:8080/oauth/token");
       resource.setClientId("trustedclient");
       resource.setClientSecret("trustedclient123");
       resource.setGrantType("password");
 
       DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
       OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource, clientContext);

       Greet greet = restTemplate.getForObject("http://localhost:8080", Greet.class);

       Assert.assertEquals("Hello World!", greet.getMessage());
}
 
Example #2
Source File: ApplicationTests.java    From Microservices-Building-Scalable-Software with MIT License 6 votes vote down vote up
@Test
public void testOAuthService() {	
       ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
       resource.setUsername("guest");
       resource.setPassword("guest123");
       resource.setAccessTokenUri("http://localhost:8080/oauth/token");
       resource.setClientId("trustedclient");
       resource.setClientSecret("trustedclient123");
       resource.setGrantType("password");
 
       DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
       OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource, clientContext);

       Greet greet = restTemplate.getForObject("http://localhost:8080", Greet.class);

       Assert.assertEquals("Hello World!", greet.getMessage());
}
 
Example #3
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 #4
Source File: OAuth2RestOperationsConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 6 votes vote down vote up
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public DefaultOAuth2ClientContext oauth2ClientContext() {
	DefaultOAuth2ClientContext context = new DefaultOAuth2ClientContext(new DefaultAccessTokenRequest());
	Authentication principal = SecurityContextHolder.getContext().getAuthentication();
	if (principal instanceof OAuth2Authentication) {
		OAuth2Authentication authentication = (OAuth2Authentication) principal;
		Object details = authentication.getDetails();
		if (details instanceof OAuth2AuthenticationDetails) {
			OAuth2AuthenticationDetails oauthsDetails = (OAuth2AuthenticationDetails) details;
			String token = oauthsDetails.getTokenValue();
			context.setAccessToken(new DefaultOAuth2AccessToken(token));
		}
	}
	return context;
}
 
Example #5
Source File: ApplicationTests.java    From Learning-Path-Spring-5-End-to-End-Programming with MIT License 6 votes vote down vote up
@Test
public void testOAuthService() {	
       ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
       resource.setUsername("guest");
       resource.setPassword("guest123");
       resource.setAccessTokenUri("http://localhost:8080/oauth/token");
       resource.setClientId("trustedclient");
       resource.setClientSecret("trustedclient123");
       resource.setGrantType("password");
       resource.setScope(Arrays.asList(new String[]{"read","write","trust"}));
 
       DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
       OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource, clientContext);

       Greet greet = restTemplate.getForObject("http://localhost:8080", Greet.class);

       Assert.assertEquals("Hello World!", greet.getMessage());
}
 
Example #6
Source File: TodoControllerIT.java    From Mastering-Spring-5.1 with MIT License 5 votes vote down vote up
private OAuth2RestTemplate getOAuthTemplate() {
	ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
	resource.setUsername("user2");
	resource.setPassword("user2-password");
	resource.setAccessTokenUri(createUrl("/oauth/token"));
	resource.setClientId("YourClientID");
	resource.setClientSecret("TopSecretClientPassword");
	resource.setGrantType("password");
	OAuth2RestTemplate oauthTemplate = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext());
	return oauthTemplate;
}
 
Example #7
Source File: OAuth2FeignRequestInterceptorTests.java    From spring-cloud-security with Apache License 2.0 5 votes vote down vote up
@Test(expected = OAuth2AccessDeniedException.class)
public void tryToAcquireToken() {
	oAuth2FeignRequestInterceptor = new OAuth2FeignRequestInterceptor(
			new DefaultOAuth2ClientContext(),
			new BaseOAuth2ProtectedResourceDetails());
	OAuth2AccessToken oAuth2AccessToken = oAuth2FeignRequestInterceptor.getToken();
	Assert.assertTrue(oAuth2AccessToken.getValue() + " Must be null",
			oAuth2AccessToken.getValue() == null);
}
 
Example #8
Source File: SmartlingClientConfiguration.java    From mojito with Apache License 2.0 5 votes vote down vote up
public OAuth2RestTemplate smartlingRestTemplate() {
    OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(smartling(), new DefaultOAuth2ClientContext());

    RestTemplateUtils restTemplateUtils = new RestTemplateUtils();
    restTemplateUtils.enableFeature(oAuth2RestTemplate, DeserializationFeature.UNWRAP_ROOT_VALUE);

    AccessTokenProviderChain accessTokenProviderChain = new AccessTokenProviderChain(Arrays.asList(
            new SmartlingAuthorizationCodeAccessTokenProvider())
    );
    oAuth2RestTemplate.setAccessTokenProvider(accessTokenProviderChain);

    DefaultUriTemplateHandler defaultUriTemplateHandler = new DefaultUriTemplateHandler();
    defaultUriTemplateHandler.setBaseUrl(baseUri);

    oAuth2RestTemplate.setUriTemplateHandler(defaultUriTemplateHandler);

    oAuth2RestTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
            try {
                super.handleError(response);
            } catch (HttpClientErrorException e) {
                if (resttemplateLogger.isDebugEnabled()) {
                    resttemplateLogger.debug(e.getResponseBodyAsString());
                }
                throw e;
            }
        }
    });

    return oAuth2RestTemplate;
}
 
Example #9
Source File: TodoControllerIT.java    From Mastering-Spring-5.0 with MIT License 5 votes vote down vote up
private OAuth2RestTemplate getOAuthTemplate() {
	ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
	resource.setUsername("user-name");
	resource.setPassword("user-password");
	resource.setAccessTokenUri(createUrl("/oauth/token"));
	resource.setClientId("clientId");
	resource.setClientSecret("clientSecret");
	resource.setGrantType("password");
	OAuth2RestTemplate oauthTemplate = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext());
	return oauthTemplate;
}
 
Example #10
Source File: BasicControllerIT.java    From Mastering-Spring-5.0 with MIT License 5 votes vote down vote up
private OAuth2RestTemplate getOAuthTemplate() {
	ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
	resource.setUsername("user-name");
	resource.setPassword("user-password");
	resource.setAccessTokenUri(createURL("/oauth/token"));
	resource.setClientId("clientId");
	resource.setClientSecret("clientSecret");
	resource.setGrantType("password");
	OAuth2RestTemplate oauthTemplate = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext());
	return oauthTemplate;
}
 
Example #11
Source File: UserInfoTokenServicesRefreshTokenTests.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void withRestTemplateChangesState() {
	OAuth2ProtectedResourceDetails resource = new AuthorizationCodeResourceDetails();
	OAuth2ClientContext context = new DefaultOAuth2ClientContext();
	context.setAccessToken(new DefaultOAuth2AccessToken("FOO"));
	this.services.setRestTemplate(new OAuth2RestTemplate(resource, context));
	assertThat(this.services.loadAuthentication("BAR").getName()).isEqualTo("me");
	assertThat(context.getAccessToken().getValue()).isEqualTo("BAR");
}
 
Example #12
Source File: UserInfoTokenServicesRefreshTokenTests.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Test
public void withRestTemplate() {
	OAuth2ProtectedResourceDetails resource = new AuthorizationCodeResourceDetails();
	OAuth2ClientContext context = new DefaultOAuth2ClientContext();
	DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
	token.setRefreshToken(new DefaultExpiringOAuth2RefreshToken("BAR", new Date(0L)));
	context.setAccessToken(token);
	this.services.setRestTemplate(new OAuth2RestTemplate(resource, context));
	assertThat(this.services.loadAuthentication("FOO").getName()).isEqualTo("me");
	assertThat(context.getAccessToken().getValue()).isEqualTo("FOO");
	// The refresh token is still intact
	assertThat(context.getAccessToken().getRefreshToken()).isEqualTo(token.getRefreshToken());
}
 
Example #13
Source File: BasicControllerIT.java    From Mastering-Spring-5.1 with MIT License 5 votes vote down vote up
private OAuth2RestTemplate getOAuthTemplate() {
	ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
	resource.setUsername("user2");
	resource.setPassword("user2-password");
	resource.setAccessTokenUri(createURL("/oauth/token"));
	resource.setClientId("YourClientID");
	resource.setClientSecret("TopSecretClientPassword");
	resource.setGrantType("password");
	// resource.setAuthenticationScheme(AuthenticationScheme.header);
	// resource.
	OAuth2RestTemplate oauthTemplate = new OAuth2RestTemplate(resource, new DefaultOAuth2ClientContext());
	return oauthTemplate;
}
 
Example #14
Source File: OAuth2InterceptedFeignConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
@Bean(name = "oauth2RequestInterceptor")
public RequestInterceptor getOAuth2RequestInterceptor() throws IOException {
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), loadBalancedResourceDetails);
}
 
Example #15
Source File: OAuth2InterceptedFeignConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Bean(name = "oauth2RequestInterceptor")
public RequestInterceptor getOAuth2RequestInterceptor() throws IOException {
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), loadBalancedResourceDetails);
}
 
Example #16
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;
}
 
Example #17
Source File: OAuth2HttpClientTest.java    From feign-oauth2-spring-cloud-starter with Apache License 2.0 4 votes vote down vote up
@Bean
public OAuth2ClientContext auth2ClientContext() {
    return new DefaultOAuth2ClientContext();
}
 
Example #18
Source File: OAuth2InterceptedFeignConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
@Bean(name = "oauth2RequestInterceptor")
public RequestInterceptor getOAuth2RequestInterceptor() throws IOException {
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), loadBalancedResourceDetails);
}
 
Example #19
Source File: OAuth2InterceptedFeignConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
@Bean(name = "oauth2RequestInterceptor")
public RequestInterceptor getOAuth2RequestInterceptor() throws IOException {
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), loadBalancedResourceDetails);
}
 
Example #20
Source File: OAuth2InterceptedFeignConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
@Bean(name = "oauth2RequestInterceptor")
public RequestInterceptor getOAuth2RequestInterceptor() throws IOException {
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), loadBalancedResourceDetails);
}
 
Example #21
Source File: OAuth2InterceptedFeignConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
@Bean(name = "oauth2RequestInterceptor")
public RequestInterceptor getOAuth2RequestInterceptor() throws IOException {
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), loadBalancedResourceDetails);
}
 
Example #22
Source File: OAuth2InterceptedFeignConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
@Bean(name = "oauth2RequestInterceptor")
public RequestInterceptor getOAuth2RequestInterceptor() throws IOException {
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), loadBalancedResourceDetails);
}
 
Example #23
Source File: ClientConfiguration.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnProperty("openapipetstore.security.petstoreAuth.client-id")
public OAuth2ClientContext oAuth2ClientContext() {
  return new DefaultOAuth2ClientContext();
}
 
Example #24
Source File: AccountApplication.java    From microservice-skeleton with MIT License 4 votes vote down vote up
@Bean
public RequestInterceptor oauth2FeignRequestInterceptor() {
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), clientCredentialsResourceDetails());
}
 
Example #25
Source File: CoreServiceApplication.java    From DAFramework with MIT License 4 votes vote down vote up
@Bean
public RequestInterceptor oauth2FeignRequestInterceptor() {
	return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), clientCredentialsResourceDetails());
}
 
Example #26
Source File: OAuth2RestOperationsConfigurationTests.java    From spring-security-oauth2-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void sessionScopedWithNoClientId() throws Exception {
	initializeContext(ConfigForSessionScopedConfiguration.class, false);
	this.thrown.expect(NoSuchBeanDefinitionException.class);
	this.context.getBean(DefaultOAuth2ClientContext.class);
}
 
Example #27
Source File: OAuth2RestOperationsConfigurationTests.java    From spring-security-oauth2-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void requestScopedWithNoClientId() throws Exception {
	initializeContext(ConfigForRequestScopedConfiguration.class, false);
	this.thrown.expect(NoSuchBeanDefinitionException.class);
	this.context.getBean(DefaultOAuth2ClientContext.class);
}
 
Example #28
Source File: OAuth2InterceptedFeignConfiguration.java    From cubeai with Apache License 2.0 4 votes vote down vote up
@Bean(name = "oauth2RequestInterceptor")
public RequestInterceptor getOAuth2RequestInterceptor() throws IOException {
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), loadBalancedResourceDetails);
}
 
Example #29
Source File: OauthClientApplication.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public OAuth2RestTemplate oAuth2RestTemplate() {
    return new OAuth2RestTemplate(oAuth2ProtectedResourceDetails(), new DefaultOAuth2ClientContext());
}
 
Example #30
Source File: OAuth2ClientFeignConfiguration.java    From cola-cloud with MIT License 4 votes vote down vote up
@Bean
public RequestInterceptor oauth2FeignRequestInterceptor(){
    return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), clientCredentialsResourceDetails);
}