當前位置: 首頁>>代碼示例>>Java>>正文


Java OAuth2ClientContext類代碼示例

本文整理匯總了Java中org.springframework.security.oauth2.client.OAuth2ClientContext的典型用法代碼示例。如果您正苦於以下問題:Java OAuth2ClientContext類的具體用法?Java OAuth2ClientContext怎麽用?Java OAuth2ClientContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


OAuth2ClientContext類屬於org.springframework.security.oauth2.client包,在下文中一共展示了OAuth2ClientContext類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: general

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
public static Filter general(AuthorizationCodeResourceDetails client, ResourceServerProperties resourceServerProperties, String path, OAuth2ClientContext oauth2ClientContext) {
	OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter(path){
		protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
		                                        FilterChain chain, Authentication authResult) throws IOException, ServletException {
			super.successfulAuthentication(request, response, chain, authResult);
			OAuth2AccessToken accessToken = restTemplate.getAccessToken();
			log.warn(new Gson().toJson(authResult));
			log.warn(new Gson().toJson(accessToken));
		}
	};
	OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client, oauth2ClientContext);
	oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
	UserInfoTokenServices tokenServices = new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(), client.getClientId());
	tokenServices.setRestTemplate(oAuth2RestTemplate);
	oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
	return oAuth2ClientAuthenticationFilter;
}
 
開發者ID:DataAgg,項目名稱:DAFramework,代碼行數:18,代碼來源:OAuth2Util.java

示例2: testCanUseClientCredentials

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@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);
}
 
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:17,代碼來源:OAuth2AutoConfigurationTests.java

示例3: testCanUseClientCredentialsWithEnableOAuth2Client

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@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);
}
 
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:23,代碼來源:OAuth2AutoConfigurationTests.java

示例4: restTemplate

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@Bean
public OAuth2RestTemplate restTemplate(OAuth2ClientContext context) {
    OAuth2RestTemplate rest = new OAuth2RestTemplate(resourceDetails(), context);
    AccessTokenProviderChain providerChain = new AccessTokenProviderChain(
            Arrays.asList(new AuthorizationCodeAccessTokenProvider()));
    rest.setAccessTokenProvider(providerChain);
    return rest;
}
 
開發者ID:PacktPublishing,項目名稱:OAuth-2.0-Cookbook,代碼行數:9,代碼來源:GoogleConfiguration.java

示例5: wechat

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
public static Filter wechat(AuthorizationCodeResourceDetails client, ResourceServerProperties resourceServerProperties, String path, OAuth2ClientContext oauth2ClientContext) {
	OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter(path);

	OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client, oauth2ClientContext);
	AuthorizationCodeAccessTokenProvider accessTokenProvider = new AuthorizationCodeAccessTokenProvider();
	accessTokenProvider.setAuthorizationRequestEnhancer((request, resource, form, headers) -> {
		form.set("appid", resource.getClientId());
		form.set("secret", resource.getClientSecret());
		form.set("scope", "snsapi_userinfo");
		form.set("response_type", "code");
		form.set("#wechat_redirect", "");
	});
	accessTokenProvider.setMessageConverters(converters());
	oAuth2RestTemplate.setAccessTokenProvider(accessTokenProvider);

	oAuth2RestTemplate.setRetryBadAccessTokens(true);
	oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);

	UserInfoTokenServices tokenServices = new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(), client.getClientId());
	tokenServices.setRestTemplate(oAuth2RestTemplate);
	oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
	return oAuth2ClientAuthenticationFilter;
}
 
開發者ID:DataAgg,項目名稱:DAFramework,代碼行數:24,代碼來源:OAuth2Util.java

示例6: clientConfigured

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@Test
public void clientConfigured() throws Exception {
	this.context = new SpringApplicationBuilder(ClientConfiguration.class)
			.properties("spring.config.name=test", "server.port=0",
					"security.oauth2.resource.userInfoUri:http://example.com",
					"security.oauth2.client.clientId=foo")
			.run();
	RequestContextHolder.setRequestAttributes(
			new ServletRequestAttributes(new MockHttpServletRequest()));
	OAuth2ClientContext client = this.context.getBean(OAuth2ClientContext.class);
	assertNull(client.getAccessToken());
	UserInfoTokenServices services = context.getBean(UserInfoTokenServices.class);
	OAuth2RestTemplate template = (OAuth2RestTemplate) ReflectionTestUtils
			.getField(services, "restTemplate");
	MockRestServiceServer server = MockRestServiceServer.createServer(template);
	server.expect(requestTo("http://example.com"))
			.andRespond(withSuccess("{\"id\":\"user\"}", MediaType.APPLICATION_JSON));
	services.loadAuthentication("FOO");
	assertEquals("FOO", client.getAccessToken().getValue());
	server.verify();
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-security,代碼行數:22,代碼來源:ResourceServerTokenRelayAutoConfigurationTests.java

示例7: authenticate

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@Override
public void authenticate(OAuth2ProtectedResourceDetails resource, OAuth2ClientContext clientContext,
        ClientHttpRequest request) {

    OAuth2AccessToken accessToken = clientContext.getAccessToken();
    if (accessToken == null) {
        throw new AccessTokenRequiredException(resource);
    }

    String tokenType = accessToken.getTokenType();

    if (!StringUtils.hasText(tokenType) || tokenType.equalsIgnoreCase(OAuth2AccessToken.BEARER_TYPE)) {
        tokenType = OAuth2AccessToken.BEARER_TYPE; // we'll assume basic bearer token type if none is specified.
    }

    request.getHeaders().set("Authorization", String.format("%s %s", tokenType, accessToken.getValue()));
}
 
開發者ID:openmhealth,項目名稱:shimmer,代碼行數:18,代碼來源:CaseStandardizingOAuth2RequestAuthenticator.java

示例8: userInfoRestTemplateFactory

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@Bean
@ConditionalOnMissingBean
public UserInfoRestTemplateFactory userInfoRestTemplateFactory(
		ObjectProvider<List<UserInfoRestTemplateCustomizer>> customizers,
		ObjectProvider<OAuth2ProtectedResourceDetails> details,
		ObjectProvider<OAuth2ClientContext> oauth2ClientContext) {
	return new DefaultUserInfoRestTemplateFactory(customizers, details,
			oauth2ClientContext);
}
 
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:10,代碼來源:ResourceServerTokenServicesConfiguration.java

示例9: DefaultUserInfoRestTemplateFactory

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
public DefaultUserInfoRestTemplateFactory(
		ObjectProvider<List<UserInfoRestTemplateCustomizer>> customizers,
		ObjectProvider<OAuth2ProtectedResourceDetails> details,
		ObjectProvider<OAuth2ClientContext> oauth2ClientContext) {
	this.customizers = customizers.getIfAvailable();
	this.details = details.getIfAvailable();
	this.oauth2ClientContext = oauth2ClientContext.getIfAvailable();
}
 
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:9,代碼來源:DefaultUserInfoRestTemplateFactory.java

示例10: withRestTemplate

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@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());
}
 
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:15,代碼來源:UserInfoTokenServicesRefreshTokenTests.java

示例11: withRestTemplateChangesState

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@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");
}
 
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:10,代碼來源:UserInfoTokenServicesRefreshTokenTests.java

示例12: init

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@Before
public void init() {
	this.resource.setClientId("foo");
	given(this.template.getForEntity(any(String.class), eq(Map.class)))
			.willReturn(new ResponseEntity<>(this.map, HttpStatus.OK));
	given(this.template.getAccessToken())
			.willReturn(new DefaultOAuth2AccessToken("FOO"));
	given(this.template.getResource()).willReturn(this.resource);
	given(this.template.getOAuth2ClientContext())
			.willReturn(mock(OAuth2ClientContext.class));
}
 
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:12,代碼來源:UserInfoTokenServicesTests.java

示例13: testClientIsNotResourceServer

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@Test
public void testClientIsNotResourceServer() {
	this.context = new AnnotationConfigServletWebServerApplicationContext();
	this.context.register(ClientConfiguration.class,
			MinimalSecureWebApplication.class);
	this.context.refresh();
	assertThat(countBeans(RESOURCE_SERVER_CONFIG)).isEqualTo(0);
	assertThat(countBeans(AUTHORIZATION_SERVER_CONFIG)).isEqualTo(0);
	// Scoped target and proxy:
	assertThat(countBeans(OAuth2ClientContext.class)).isEqualTo(2);
}
 
開發者ID:spring-projects,項目名稱:spring-security-oauth2-boot,代碼行數:12,代碼來源:OAuth2AutoConfigurationTests.java

示例14: FeignOAuthInterceptor

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
public FeignOAuthInterceptor(OAuth2ClientContext oAuth2ClientContext) {
	super(oAuth2ClientContext, null);
	this.oAuth2ClientContext = oAuth2ClientContext;
	this.accessTokenProvider = new AccessTokenProviderChain(Arrays.asList(new AuthorizationCodeAccessTokenProvider(),
			new ImplicitAccessTokenProvider(), new ResourceOwnerPasswordAccessTokenProvider(),
			new ClientCredentialsAccessTokenProvider()));
}
 
開發者ID:rhawan,項目名稱:microservices-tcc-alfa,代碼行數:8,代碼來源:FeignOAuthInterceptor.java

示例15: restTemplate

import org.springframework.security.oauth2.client.OAuth2ClientContext; //導入依賴的package包/類
@Bean
public OAuth2RestTemplate restTemplate(OAuth2ClientContext context) {
    OAuth2RestTemplate rest = new OAuth2RestTemplate(resourceDetails(), context);
    rest.setAccessTokenProvider(
        new AccessTokenProviderChain(
            Arrays.asList(new AuthorizationCodeAccessTokenProvider())));
    return rest;
}
 
開發者ID:PacktPublishing,項目名稱:OAuth-2.0-Cookbook,代碼行數:9,代碼來源:FacebookConfiguration.java


注:本文中的org.springframework.security.oauth2.client.OAuth2ClientContext類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。