本文整理汇总了Java中org.springframework.security.oauth2.client.OAuth2RestTemplate类的典型用法代码示例。如果您正苦于以下问题:Java OAuth2RestTemplate类的具体用法?Java OAuth2RestTemplate怎么用?Java OAuth2RestTemplate使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OAuth2RestTemplate类属于org.springframework.security.oauth2.client包,在下文中一共展示了OAuth2RestTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: general
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的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;
}
示例2: testAccessRootUrlWithOAuth2AccessToken
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
@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());
}
示例3: testAccessAboutUrlWithOAuth2AccessToken
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
@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()));
}
示例4: testConnectDirectlyToResourceServer
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
public void testConnectDirectlyToResourceServer() throws Exception {
ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
resource.setAccessTokenUri(tokenUrl);
resource.setId("microservice-test");
resource.setClientId("oauthClient1");
resource.setClientSecret("oauthClient1Password");
resource.setGrantType("password");
resource.setScope(Arrays.asList("openid"));
resource.setUsername("[email protected]");
resource.setPassword("user1");
OAuth2RestTemplate template = new OAuth2RestTemplate(resource);
logger.info(" CALLING: " + baseUrl+"/api");
String result = template.getForObject(baseUrl+"/api", String.class);
System.err.println(result);
assertEquals("Hello, Trusted User marissa", result);
}
示例5: ssoFilter
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
private Filter ssoFilter() {
CompositeFilter filter = new CompositeFilter();
List<Filter> filters = new ArrayList<>();
OAuth2ClientAuthenticationProcessingFilter facebookFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/facebook");
OAuth2RestTemplate facebookTemplate = new OAuth2RestTemplate(facebook(), oAuth2ClientContext);
facebookFilter.setRestTemplate(facebookTemplate);
facebookFilter.setTokenServices(new UserInfoTokenServices(facebookResource().getUserInfoUri(), facebook().getClientId()));
filters.add(facebookFilter);
OAuth2ClientAuthenticationProcessingFilter googleFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/google");
OAuth2RestTemplate googleTemplate = new OAuth2RestTemplate(google(), oAuth2ClientContext);
googleFilter.setRestTemplate(googleTemplate);
googleFilter.setTokenServices(new UserInfoTokenServices(googleResource().getUserInfoUri(), google().getClientId()));
filters.add(googleFilter);
filter.setFilters(filters);
return filter;
}
示例6: oAuth2RestOperations
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
@Bean
public OAuth2RestOperations oAuth2RestOperations() {
ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
resource.setAccessTokenUri(tokenUrl);
resource.setId(resourceId);
resource.setClientId(resourceClientId);
resource.setClientSecret(resourceClientSecret);
resource.setGrantType("password");
resource.setScope(Arrays.asList("openid"));
resource.setUsername("[email protected]");
resource.setPassword("user1");
OAuth2RestTemplate template = new OAuth2RestTemplate(resource);
// template.setRequestFactory(requestFactory);
return template;
}
示例7: wechat
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的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;
}
示例8: on
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
@When("^client_one does a PUT on (.*?) with (.*?) in zone 1$")
public void client_one_does_a_PUT_on_identifier_in_test_zone(final String api, final String identifier)
throws Throwable {
OAuth2RestTemplate acsTemplate = this.acsZone1Template;
try {
switch (api) {
case "subject":
this.privilegeHelper.putSubject(acsTemplate, this.subject, this.acsUrl, this.zone1Headers,
this.privilegeHelper.getDefaultAttribute());
break;
case "resource":
this.privilegeHelper.putResource(acsTemplate, this.resource, this.acsUrl, this.zone1Headers,
this.privilegeHelper.getDefaultAttribute());
break;
case "policy-set":
this.testPolicyName = "single-action-defined-policy-set";
this.policyHelper.createPolicySet("src/test/resources/single-action-defined-policy-set.json",
acsTemplate, this.zone1Headers);
break;
default:
Assert.fail("Api " + api + " does not match/is not yet implemented for this test code.");
}
} catch (HttpClientErrorException e) {
Assert.fail("Unable to PUT identifier: " + identifier + " for api: " + api);
}
}
示例9: testConnectDirectlyToResourceServer
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
public void testConnectDirectlyToResourceServer() throws Exception {
OAuth2RestTemplate template = template();
logger.info(" CALLING: " + baseUrl+"/api");
String result = template.getForObject(baseUrl+"/api", String.class);
logger.info(" RESULT: " + result);
assertEquals("{Hello API}", result);
logger.info(" CALLING: " + baseUrl+"/events/101/");
result = template.getForObject(baseUrl+"/events/101/", String.class);
logger.info(" RESULT: " + result);
// assertEquals("{[\"id\":101,\"summary\":\"Conference Call\",\"description\":\"Call with the client\",\"when\":1514059200000]}", result);
//
logger.info(" CALLING: " + baseUrl+"/events/my/");
result = template.getForObject(baseUrl+"/events/my/", String.class);
logger.info(" RESULT: " + result);
assertEquals("{Hello API}", result);
}
示例10: postMultipleSubjects
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
public ResponseEntity<Object> postMultipleSubjects(final OAuth2RestTemplate acsTemplate, final String endpoint,
final HttpHeaders headers, final BaseSubject... subjects) {
Attribute site = getDefaultAttribute();
Set<Attribute> attributes = new HashSet<>();
attributes.add(site);
List<BaseSubject> subjectsArray = new ArrayList<>();
for (BaseSubject s : subjects) {
s.setAttributes(attributes);
subjectsArray.add(s);
}
URI subjectUri = URI.create(endpoint + ACS_SUBJECT_API_PATH);
ResponseEntity<Object> responseEntity = acsTemplate.postForEntity(subjectUri,
new HttpEntity<>(subjectsArray, headers), Object.class);
return responseEntity;
}
示例11: postResources
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
public ResponseEntity<Object> postResources(final OAuth2RestTemplate acsTemplate, final String endpoint,
final BaseResource... resources) {
Attribute site = getDefaultAttribute();
Set<Attribute> attributes = new HashSet<>();
attributes.add(site);
List<BaseResource> resourcesArray = new ArrayList<>();
for (BaseResource r : resources) {
r.setAttributes(attributes);
resourcesArray.add(r);
}
URI resourceUri = URI.create(endpoint + ACS_RESOURCE_API_PATH);
ResponseEntity<Object> responseEntity = acsTemplate.postForEntity(resourceUri, resourcesArray, Object.class);
return responseEntity;
}
示例12: createAcsAdminClient
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
public OAuth2RestTemplate createAcsAdminClient(final List<String> acsZones) {
ArrayList<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("acs.zones.admin"));
authorities.add(new SimpleGrantedAuthority("acs.attributes.read"));
authorities.add(new SimpleGrantedAuthority("acs.attributes.write"));
authorities.add(new SimpleGrantedAuthority("acs.policies.read"));
authorities.add(new SimpleGrantedAuthority("acs.policies.write"));
authorities.add(new SimpleGrantedAuthority("acs.connectors.read"));
authorities.add(new SimpleGrantedAuthority("acs.connectors.write"));
for (int i = 0; i < acsZones.size(); i++) {
authorities.add(new SimpleGrantedAuthority("predix-acs.zones." + acsZones.get(i) + ".admin"));
authorities.add(new SimpleGrantedAuthority("predix-acs.zones." + acsZones.get(i) + ".user"));
}
OAuth2RestTemplate restTemplate = createScopeClient(acsZones.get(0), "super-admin");
this.createClientWithAuthorities(restTemplate.getResource().getClientId(),
restTemplate.getResource().getClientSecret(), authorities);
return restTemplate;
}
示例13: setupMockedAdapterResponse
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
static void setupMockedAdapterResponse(final ExternalAttributeReader externalAttributeReader,
final AttributeCache attributeCache, final String identifier) {
ResponseEntity<AttributesResponse> attributeResponseResponseEntity = new ResponseEntity<>(
new AttributesResponse(Collections.singleton(new Attribute("issuer", "name", "value")),
"attributesResponse"), HttpStatus.OK);
OAuth2RestTemplate mockRestTemplate = Mockito.mock(OAuth2RestTemplate.class);
Mockito.doReturn(attributeResponseResponseEntity).when(mockRestTemplate)
.getForEntity(Mockito.anyString(), Mockito.eq(AttributesResponse.class));
Mockito.doReturn(Collections.singleton(
new AttributeAdapterConnection("https://my-url", "https://my-uaa", "my-client", "my-secret")))
.when(externalAttributeReader).getAttributeAdapterConnections();
Mockito.doReturn(mockRestTemplate).when(externalAttributeReader)
.getAdapterOauth2RestTemplate(Mockito.any(AttributeAdapterConnection.class));
Mockito.when(attributeCache.getAttributes(identifier)).thenReturn(null);
}
示例14: createOAuth2RestTemplate
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的package包/类
private OAuth2RestTemplate createOAuth2RestTemplate(
OAuth2ProtectedResourceDetails details) {
if (this.oauth2ClientContext == null) {
return new OAuth2RestTemplate(details);
}
return new OAuth2RestTemplate(details, this.oauth2ClientContext);
}
开发者ID:spring-projects,项目名称:spring-security-oauth2-boot,代码行数:8,代码来源:DefaultUserInfoRestTemplateFactory.java
示例15: withRestTemplate
import org.springframework.security.oauth2.client.OAuth2RestTemplate; //导入依赖的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