本文整理汇总了Java中org.springframework.social.connect.support.OAuth2ConnectionFactory类的典型用法代码示例。如果您正苦于以下问题:Java OAuth2ConnectionFactory类的具体用法?Java OAuth2ConnectionFactory怎么用?Java OAuth2ConnectionFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OAuth2ConnectionFactory类属于org.springframework.social.connect.support包,在下文中一共展示了OAuth2ConnectionFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: completeConnection
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
/**
* Complete the connection to the OAuth2 provider.
*
* @param connectionFactory the service provider's connection factory e.g. FacebookConnectionFactory
* @param request the current web request
* @return a new connection to the service provider
*/
public Connection completeConnection(OAuth2ConnectionFactory<?> connectionFactory, NativeWebRequest request) {
if (connectionFactory.supportsStateParameter()) {
verifyStateParameter(request);
}
String code = request.getParameter("code");
try {
AccessGrant accessGrant = connectionFactory.getOAuthOperations()
.exchangeForAccess(code, callbackUrl(request), null);
return connectionFactory.createConnection(accessGrant);
} catch (HttpClientErrorException e) {
log.warn("HttpClientErrorException while completing connection: " + e.getMessage());
log.warn(" Response body: " + e.getResponseBodyAsString());
throw e;
}
}
示例2: create
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
@Override
public CredentialProvider create(final SocialProperties properties) {
final OAuth2ConnectorProperties oauth2Properties = (OAuth2ConnectorProperties) properties;
final String appId = oauth2Properties.getAppId();
final String appSecret = oauth2Properties.getAppSecret();
final String authorizationUrl = oauth2Properties.getAuthorizationUrl();
final String authenticationUrl = oauth2Properties.getAuthenticationUrl();
final String accessTokenUrl = oauth2Properties.getAccessTokenUrl();
final boolean useParametersForClientCredentials = oauth2Properties.isUseParametersForClientCredentials();
final TokenStrategy tokenStrategy = oauth2Properties.getTokenStrategy();
final String scope = oauth2Properties.getScope();
final OAuth2ServiceProvider<RestOperations> serviceProvider = new GenericOAuth2ServiceProvider(appId, appSecret, authorizationUrl,
authenticationUrl, accessTokenUrl, useParametersForClientCredentials, tokenStrategy);
final OAuth2ConnectionFactory<RestOperations> connectionFactory = new OAuth2ConnectionFactory<>("oauth2", serviceProvider, null);
connectionFactory.setScope(scope);
final OAuth2Applicator applicator = new OAuth2Applicator(properties);
applicator.setAccessTokenProperty("accessToken");
return new OAuth2CredentialProvider<>("oauth2", connectionFactory, applicator);
}
示例3: oauth2Callback
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
@GetMapping(value = "/signin/{providerId}", params = "code")
public RedirectView oauth2Callback(@PathVariable String providerId, @RequestParam("code") String code,
NativeWebRequest request) {
try {
OAuth2ConnectionFactory<?> connectionFactory = (OAuth2ConnectionFactory<?>) connectionFactoryLocator
.getConnectionFactory(providerId);
Connection<?> connection = connectSupport.completeConnection(connectionFactory, request);
return handleSignIn(connection, connectionFactory, request);
} catch (Exception e) {
log.error("Exception while completing OAuth 2 connection: ", e);
return redirectOnError(providerId);
}
}
示例4: buildOAuthUrl
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
/**
* Builds the provider URL to redirect the user to for connection authorization.
*
* @param connectionFactory the service provider's connection factory e.g. FacebookConnectionFactory
* @param request the current web request
* @param additionalParameters parameters to add to the authorization URL.
* @return the URL to redirect the user to for authorization
* @throws IllegalArgumentException if the connection factory is not OAuth1 based.
*/
public String buildOAuthUrl(ConnectionFactory<?> connectionFactory, NativeWebRequest request,
MultiValueMap<String, String> additionalParameters) {
if (connectionFactory instanceof OAuth1ConnectionFactory) {
return buildOAuth1Url((OAuth1ConnectionFactory<?>) connectionFactory, request, additionalParameters);
} else if (connectionFactory instanceof OAuth2ConnectionFactory) {
return buildOAuth2Url((OAuth2ConnectionFactory<?>) connectionFactory, request, additionalParameters);
} else {
throw new IllegalArgumentException("ConnectionFactory not supported");
}
}
示例5: buildOAuth2Url
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
private String buildOAuth2Url(OAuth2ConnectionFactory<?> connectionFactory, NativeWebRequest request,
MultiValueMap<String, String> additionalParameters) {
OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();
String defaultScope = connectionFactory.getScope();
OAuth2Parameters parameters = getOAuth2Parameters(request, defaultScope, additionalParameters);
String state = connectionFactory.generateState();
parameters.add("state", state);
sessionStrategy.setAttribute(request, OAUTH2_STATE_ATTRIBUTE, state);
return oauthOperations.buildAuthenticateUrl(parameters);
}
示例6: setup
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
@Before
@SneakyThrows
public void setup() {
TenantProperties properties = new TenantProperties();
properties.setSocial(asList(
new TenantProperties.Social("twitter", "xxx", "yyy", DEFAULT_DOMAIN),
new TenantProperties.Social("facebook", "xxx", "yyy", DEFAULT_DOMAIN)
));
tenantPropertiesService.onRefresh("/config/tenants/"+ DEFAULT_TENANT + "/uaa/uaa.yml",
new ObjectMapper(new YAMLFactory()).writeValueAsString(properties));
MockitoAnnotations.initMocks(this);
SocialController socialController = new SocialController(socialService, providerSignInUtils,
connectionFactoryLocator, usersConnectionRepository, signInAdapter, connectSupport, sessionStrategy, socialConfigRepository);
this.restUserMockMvc = MockMvcBuilders.standaloneSetup(socialController)
.setControllerAdvice(exceptionTranslator)
.setMessageConverters(jacksonMessageConverter)
.build();
when(sessionStrategy.getAttribute(any(RequestAttributes.class), eq(ProviderSignInAttempt.SESSION_ATTRIBUTE)))
.thenReturn(providerSignInAttempt);
when(connection.fetchUserProfile())
.thenReturn(new UserProfile("id", "name", "fname", "lname", "email", "username"));
when(connection.createData()).thenReturn(
new ConnectionData("twitter", "providerUserId", "displayName", "profileUrl", "imageUrl", "", "secret",
"refreshToken", 1000L));
Mockito.<Connection<?>>when(connectSupport.completeConnection(any(OAuth1ConnectionFactory.class), any()))
.thenReturn(connection);
Mockito.<Connection<?>>when(connectSupport.completeConnection(any(OAuth2ConnectionFactory.class), any()))
.thenReturn(connection);
when(connectSupport.buildOAuthUrl(any(), any(), any())).thenReturn("SomeCallbackUrl");
Mockito.<Connection<?>>when(providerSignInAttempt.getConnection(any())).thenReturn(connection);
}
示例7: SocialTokenServicesConfiguration
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
public SocialTokenServicesConfiguration(ResourceServerProperties sso,
ObjectProvider<OAuth2ConnectionFactory<?>> connectionFactory,
UserInfoRestTemplateFactory restTemplateFactory,
ObjectProvider<AuthoritiesExtractor> authoritiesExtractor,
ObjectProvider<PrincipalExtractor> principalExtractor) {
this.sso = sso;
this.connectionFactory = connectionFactory.getIfAvailable();
this.restTemplate = restTemplateFactory.getUserInfoRestTemplate();
this.authoritiesExtractor = authoritiesExtractor.getIfAvailable();
this.principalExtractor = principalExtractor.getIfAvailable();
}
开发者ID:spring-projects,项目名称:spring-security-oauth2-boot,代码行数:12,代码来源:ResourceServerTokenServicesConfiguration.java
示例8: create
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
@Override
public CredentialProvider create(final SocialProperties properties) {
@SuppressWarnings("unchecked")
final OAuth2ConnectionFactory<Object> connectionFactory = mock(OAuth2ConnectionFactory.class);
when(connectionFactory.generateState()).thenReturn("test-state");
properties.setAppId("appId");
properties.setAppSecret("appSecret");
final OAuth2Applicator applicator = new OAuth2Applicator(properties);
applicator.setAccessTokenProperty("accessToken");
applicator.setClientIdProperty("clientId");
applicator.setClientSecretProperty("clientSecret");
applicator.setRefreshTokenProperty("refreshToken");
final CredentialProvider credentialProvider = new OAuth2CredentialProvider<>("test-provider", connectionFactory,
applicator);
@SuppressWarnings({ "unchecked", "rawtypes" })
final Class<MultiValueMap<String, String>> additionalParametersType = (Class) MultiValueMap.class;
final OAuth2Operations operations = spy(new OAuth2Template("testClientId", "testClientSecret",
"https://test/oauth2/authorize", "https://test/oauth2/token"));
doReturn(new AccessGrant("token")).when(operations).exchangeForAccess(Matchers.anyString(),
Matchers.anyString(), Matchers.any(additionalParametersType));
when(connectionFactory.getOAuthOperations()).thenReturn(operations);
return credentialProvider;
}
示例9: shouldAcquireOAuth2Credentials
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
@Test
public void shouldAcquireOAuth2Credentials() {
final OAuth2ConnectionFactory<?> oauth2 = mock(OAuth2ConnectionFactory.class);
@SuppressWarnings("unchecked")
final Applicator<AccessGrant> applicator = mock(Applicator.class);
when(locator.providerWithId("providerId"))
.thenReturn(new OAuth2CredentialProvider<>("providerId", oauth2, applicator));
when(oauth2.getScope()).thenReturn("scope");
when(oauth2.generateState()).thenReturn("state-token");
final OAuth2Operations operations = mock(OAuth2Operations.class);
when(oauth2.getOAuthOperations()).thenReturn(operations);
final ArgumentCaptor<OAuth2Parameters> parameters = ArgumentCaptor.forClass(OAuth2Parameters.class);
when(operations.buildAuthorizeUrl(parameters.capture())).thenReturn("https://provider.io/oauth/authorize");
final AcquisitionFlow acquisition = credentials.acquire("providerId", URI.create("https://syndesis.io/api/v1/"),
URI.create("/ui#state"));
final CredentialFlowState expectedFlowState = new OAuth2CredentialFlowState.Builder().key("state-token")
.providerId("providerId").redirectUrl("https://provider.io/oauth/authorize")
.returnUrl(URI.create("/ui#state")).build();
final AcquisitionFlow expected = new AcquisitionFlow.Builder().type(Type.OAUTH2)
.redirectUrl("https://provider.io/oauth/authorize").state(expectedFlowState).build();
assertThat(acquisition).isEqualTo(expected);
final OAuth2Parameters capturedParameters = parameters.getValue();
assertThat(capturedParameters.getRedirectUri()).isEqualTo("https://syndesis.io/api/v1/credentials/callback");
assertThat(capturedParameters.getScope()).isEqualTo("scope");
assertThat(capturedParameters.getState()).isEqualTo("state-token");
}
示例10: shouldFinishOAuth2Acquisition
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
@Test
public void shouldFinishOAuth2Acquisition() {
final OAuth2ConnectionFactory<?> oauth2 = mock(OAuth2ConnectionFactory.class);
final OAuth2Applicator applicator = new OAuth2Applicator(properties);
applicator.setAccessTokenProperty("accessTokenProperty");
applicator.setClientIdProperty("clientIdProperty");
applicator.setClientSecretProperty("clientSecretProperty");
applicator.setRefreshTokenProperty("refreshTokenProperty");
when(locator.providerWithId("providerId"))
.thenReturn(new OAuth2CredentialProvider<>("providerId", oauth2, applicator));
final OAuth2Operations operations = mock(OAuth2Operations.class);
when(oauth2.getOAuthOperations()).thenReturn(operations);
final AccessGrant accessGrant = new AccessGrant("accessToken", "scope", "refreshToken", 1L);
when(operations.exchangeForAccess("code", "https://syndesis.io/api/v1/credentials/callback", null))
.thenReturn(accessGrant);
final CredentialFlowState flowState = new OAuth2CredentialFlowState.Builder().providerId("providerId")
.returnUrl(URI.create("/ui#state")).code("code").state("state").build();
final CredentialFlowState finalFlowState = credentials.finishAcquisition(flowState,
URI.create("https://syndesis.io/api/v1/"));
assertThat(finalFlowState)
.isEqualTo(new OAuth2CredentialFlowState.Builder().createFrom(flowState).accessGrant(accessGrant).build());
}
示例11: shouldCreateAcquisitionMethod
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
@Test
public void shouldCreateAcquisitionMethod() {
@SuppressWarnings("unchecked")
final OAuth2CredentialProvider<?> oauth2 = new OAuth2CredentialProvider<>("provider2",
mock(OAuth2ConnectionFactory.class), mock(Applicator.class));
final AcquisitionMethod method2 = new AcquisitionMethod.Builder().description("provider2")
.label("provider2").icon("provider2").type(Type.OAUTH2).build();
assertThat(oauth2.acquisitionMethod()).isEqualTo(method2);
}
示例12: SocialTokenServicesConfiguration
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
public SocialTokenServicesConfiguration(ResourceServerProperties sso,
ObjectProvider<OAuth2ConnectionFactory<?>> connectionFactoryProvider,
UserInfoRestTemplateFactory restTemplateFactory,
ObjectProvider<AuthoritiesExtractor> authoritiesExtractor,
ObjectProvider<PrincipalExtractor> principalExtractor) {
this.sso = sso;
this.connectionFactory = connectionFactoryProvider.getIfAvailable();
this.restTemplate = restTemplateFactory.getUserInfoRestTemplate();
this.authoritiesExtractor = authoritiesExtractor.getIfAvailable();
this.principalExtractor = principalExtractor.getIfAvailable();
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:12,代码来源:ResourceServerTokenServicesConfiguration.java
示例13: create
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
@Override
public CredentialProvider create(final SocialProperties properties) {
@SuppressWarnings("unchecked")
final OAuth2ConnectionFactory<Object> connectionFactory = mock(OAuth2ConnectionFactory.class);
when(connectionFactory.generateState()).thenReturn("test-state");
properties.setAppId("appId");
properties.setAppSecret("appSecret");
final OAuth2Applicator applicator = new OAuth2Applicator(properties);
applicator.setAccessTokenProperty("accessToken");
applicator.setClientIdProperty("clientId");
applicator.setClientSecretProperty("clientSecret");
applicator.setRefreshTokenProperty("refreshToken");
final CredentialProvider credentialProvider = new OAuth2CredentialProvider<>("test-provider", connectionFactory,
applicator);
final OAuth2Operations operations = spy(new OAuth2Template("testClientId", "testClientSecret",
"https://test/oauth2/authorize", "https://test/oauth2/token"));
doReturn(new AccessGrant("token")).when(operations).exchangeForAccess(Matchers.anyString(),
Matchers.anyString(), Matchers.any(MultiValueMap.class));
when(connectionFactory.getOAuthOperations()).thenReturn(operations);
return credentialProvider;
}
示例14: shouldCreateAcquisitionMethod
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
@Test
public void shouldCreateAcquisitionMethod() {
@SuppressWarnings("unchecked")
final OAuth2CredentialProvider<?> oauth2 = new OAuth2CredentialProvider<>("provider2",
mock(OAuth2ConnectionFactory.class), mock(Applicator.class));
final ImmutableAcquisitionMethod method2 = new AcquisitionMethod.Builder().description("provider2")
.label("provider2").icon("provider2").type(Type.OAUTH2).build();
assertThat(oauth2.acquisitionMethod()).isEqualTo(method2);
}
示例15: SpringSocialTokenServices
import org.springframework.social.connect.support.OAuth2ConnectionFactory; //导入依赖的package包/类
public SpringSocialTokenServices(OAuth2ConnectionFactory<?> connectionFactory,
String clientId) {
this.connectionFactory = connectionFactory;
this.clientId = clientId;
}