当前位置: 首页>>代码示例>>Java>>正文


Java OAuth2RestOperations类代码示例

本文整理汇总了Java中org.springframework.security.oauth2.client.OAuth2RestOperations的典型用法代码示例。如果您正苦于以下问题:Java OAuth2RestOperations类的具体用法?Java OAuth2RestOperations怎么用?Java OAuth2RestOperations使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


OAuth2RestOperations类属于org.springframework.security.oauth2.client包,在下文中一共展示了OAuth2RestOperations类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: oAuth2RestOperations

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
@Bean
    public OAuth2RestOperations oAuth2RestOperations() {

        ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
        resource.setAccessTokenUri(tokenUri);

        resource.setId(resourceId);
        resource.setClientId(resourceClientId);
        resource.setClientSecret(resourceClientSecret);

        resource.setGrantType("password");
        resource.setScope(Arrays.asList("openid"));

        resource.setUsername(resourceUserId);
        resource.setPassword(resourceUserPassword);

        OAuth2RestTemplate template = new OAuth2RestTemplate(resource);
//        template.setRequestFactory(requestFactory);
        return template;
    }
 
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:21,代码来源:JavaConfig.java

示例2: oAuth2RestOperations

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的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;
    }
 
开发者ID:PacktPublishing,项目名称:Spring-Security-Third-Edition,代码行数:21,代码来源:JavaConfig.java

示例3: authorizationCodeRestTemplate

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
/**
 * 演示 grant_type=authorization_code 时,获取资源的方法
 * -
 *
 * @param client_id
 * @param client_secret     取决于 AuthorizationServer 设置,如果 client 设置了secret,则此项参数为必需,否则可以没有
 * @param access_token_uri
 * @param authorization_uri
 * @param scope
 * @return
 */

public OAuth2RestOperations authorizationCodeRestTemplate(String client_id, String client_secret, String authorization_uri, String access_token_uri, String... scope) {

    // 防止 url 写错
    if (!access_token_uri.contains("token") || !authorization_uri.contains("authorize"))
        throw new RuntimeException("uri is wrong :  access_token_uri = " + access_token_uri + " , authorization_uri" + authorization_uri);


    AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
    details.setId("1");
    details.setClientId(client_id);
    if (client_secret != null && !client_secret.isEmpty())
        details.setClientSecret(client_secret);
    details.setAccessTokenUri(access_token_uri);
    details.setUserAuthorizationUri(authorization_uri);
    details.setUseCurrentUri(true); //将当前请求的 uri 作为参数 redirect_uri 接受返回值。设置为 faslse 是,需要设置 redirect_uri 参数, details.setPreEstablishedRedirectUri("http://anywhere");
    details.setScope(Arrays.asList(scope));
    return new OAuth2RestTemplate(details, oAuth2ClientContext);
}
 
开发者ID:h819,项目名称:spring-boot,代码行数:31,代码来源:Oauth2ClientRestTemplate.java

示例4: implicitResourceRestTemplate

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
/**
 * 该方式没有实验成功,设置为 Deprecated!
 * <p>
 * 演示 grant_type=implicit 时,获取资源的方法
 *
 * @param client_id
 * @param client_secret     取决于 AuthorizationServer 设置,如果 client 设置了secret,则此项参数为必需,否则可以没有
 * @param authorization_uri
 * @param access_token_uri
 * @param scope
 * @return
 */
@Deprecated
public OAuth2RestOperations implicitResourceRestTemplate(String client_id, String client_secret, String authorization_uri, String access_token_uri, String... scope) {

    // 防止 url 写错
    if (!authorization_uri.contains("authorize"))
        throw new RuntimeException("uri is wrong :  authorization_uri" + authorization_uri);

    ImplicitResourceDetails details = new ImplicitResourceDetails();
    details.setId("2");
    details.setClientId(client_id);
    if (client_secret != null && !client_secret.isEmpty())
        details.setClientSecret(client_secret);
    details.setAccessTokenUri(authorization_uri);
    details.setClientAuthenticationScheme(AuthenticationScheme.header);
    details.setUseCurrentUri(true);
    details.setScope(Arrays.asList(scope));
    // return restTemplate;
    return new OAuth2RestTemplate(details, oAuth2ClientContext);
}
 
开发者ID:h819,项目名称:spring-boot,代码行数:32,代码来源:Oauth2ClientRestTemplate.java

示例5: resourceOwnerPasswordRestTemplate

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
/**
 * 演示 grant_type=password 时,获取资源的方法
 * 用的场景还不知道,@Deprecated
 *
 * @param client_id
 * @param client_secret    取决于 AuthorizationServer 设置,如果 client 设置了secret,则此项参数为必需,否则可以没有
 * @param access_token_uri
 * @param username
 * @param password
 * @param scope
 * @return
 */
@Deprecated
public OAuth2RestOperations resourceOwnerPasswordRestTemplate(String client_id, String client_secret, String access_token_uri, String username, String password, String... scope) {

    // 防止 url 写错
    if (!access_token_uri.contains("token"))
        throw new RuntimeException("uri is wrong :  access_token_uri = " + access_token_uri);

    // 防止 client_secret 写错
    if (username == null || password == null || username.isEmpty() || password.isEmpty())
        throw new RuntimeException("username or password  is wrong :  username or password is a required parameter");

    ResourceOwnerPasswordResourceDetails details = new ResourceOwnerPasswordResourceDetails();
    details.setId("3");
    details.setClientId(client_id);
    if (client_secret != null && !client_secret.isEmpty())
        details.setClientSecret(client_secret);
    details.setAccessTokenUri(access_token_uri);
    details.setUsername(username);
    details.setPassword(password);
    details.setScope(Arrays.asList(scope));
    return new OAuth2RestTemplate(details, oAuth2ClientContext);


}
 
开发者ID:h819,项目名称:spring-boot,代码行数:37,代码来源:Oauth2ClientRestTemplate.java

示例6: clientCredentialsRestTemplate

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
/**
 * 演示 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);
}
 
开发者ID:h819,项目名称:spring-boot,代码行数:25,代码来源:Oauth2ClientRestTemplate.java

示例7: getMap

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
@SuppressWarnings({ "unchecked" })
private Map<String, Object> getMap(String path, String accessToken) {
	this.logger.info("Getting user info from: " + path);
	try {
		OAuth2RestOperations restTemplate = this.restTemplate;
		if (restTemplate == null) {
			BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
			resource.setClientId(this.clientId);
			restTemplate = new OAuth2RestTemplate(resource);
		}
		DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(accessToken);
		token.setTokenType(this.tokenType);
		restTemplate.getOAuth2ClientContext().setAccessToken(token);
		return restTemplate.getForEntity(path, Map.class).getBody();
	}
	catch (Exception ex) {
		this.logger.info("Could not fetch user details: " + ex.getClass() + ", "
				+ ex.getMessage());
		return Collections.<String, Object>singletonMap("error",
				"Could not fetch user details");
	}
}
 
开发者ID:Nephilim84,项目名称:contestparser,代码行数:23,代码来源:UserInfoTokenServices.java

示例8: tokenRelayedWithRestTemplate

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
@Test
public void tokenRelayedWithRestTemplate() {
	OAuth2RestOperations restTemplate = Mockito.mock(OAuth2RestOperations.class);
	AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
	resource.setClientId("client");
	Mockito.when(restTemplate.getResource()).thenReturn(resource);
	Mockito.when(restTemplate.getAccessToken())
			.thenReturn(new DefaultOAuth2AccessToken("BAR"));
	filter.setRestTemplate(restTemplate);
	assertNotNull(RequestContext.getCurrentContext());
	SecurityContextHolder.getContext().setAuthentication(auth);
	assertTrue(filter.shouldFilter());
	assertEquals("FOO", RequestContext.getCurrentContext().get("ACCESS_TOKEN"));
	filter.run();
	assertEquals("bearer BAR", RequestContext.getCurrentContext()
			.getZuulRequestHeaders().get("authorization"));
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-security,代码行数:18,代码来源:OAuth2TokenRelayFilterTests.java

示例9: unauthorizedWithRestTemplate

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
@Test
public void unauthorizedWithRestTemplate() {
	OAuth2RestOperations restTemplate = Mockito.mock(OAuth2RestOperations.class);
	AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
	resource.setClientId("client");
	Mockito.when(restTemplate.getResource()).thenReturn(resource);
	Mockito.when(restTemplate.getAccessToken()).thenThrow(new RuntimeException());
	filter.setRestTemplate(restTemplate);
	assertNotNull(RequestContext.getCurrentContext());
	SecurityContextHolder.getContext().setAuthentication(auth);
	assertTrue(filter.shouldFilter());
	try {
		filter.run();
		fail("Expected BadCredentialsException");
	}
	catch (BadCredentialsException e) {
		assertEquals(401,
				RequestContext.getCurrentContext().get("error.status_code"));

	}
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-security,代码行数:22,代码来源:OAuth2TokenRelayFilterTests.java

示例10: getDataForSingleDate

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
private ResponseEntity<ShimDataResponse> getDataForSingleDate(
        OAuth2RestOperations restTemplate,
        LocalDate date,
        FitbitDataType dataType,
        boolean normalize) throws ShimException {

    String detailLevel = "";

    // TODO generalise this
    if (fitbitClientSettings.isIntradayDataAvailable()) {
        if (dataType == STEP_COUNT || dataType == HEART_RATE) {
            detailLevel = format("/1d/%dmin", fitbitClientSettings.getIntradayDataGranularityInMinutes());
        }
    }

    URI url = UriComponentsBuilder
            .fromUriString(DATA_URL)
            .path("/{apiVersion}/user/-/{endpoint}/date/{date}{detailLevel}.json")
            .buildAndExpand(dataType.getVersion(), dataType.getEndpoint(), date.toString(), detailLevel)
            .encode()
            .toUri();

    return executeRequest(restTemplate, url, normalize, dataType, date);
}
 
开发者ID:openmhealth,项目名称:shimmer,代码行数:25,代码来源:FitbitShim.java

示例11: oauth2SsoFilter

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
private OAuth2ClientAuthenticationProcessingFilter oauth2SsoFilter(
		OAuth2SsoProperties sso) {
	OAuth2RestOperations restTemplate = this.applicationContext
			.getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate();
	ResourceServerTokenServices tokenServices = this.applicationContext
			.getBean(ResourceServerTokenServices.class);
	OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(
			sso.getLoginPath());
	filter.setRestTemplate(restTemplate);
	filter.setTokenServices(tokenServices);
	filter.setApplicationEventPublisher(this.applicationContext);
	return filter;
}
 
开发者ID:spring-projects,项目名称:spring-security-oauth2-boot,代码行数:14,代码来源:SsoSecurityConfigurer.java

示例12: testDefaultConfiguration

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
@Test
public void testDefaultConfiguration() {
	this.context = new AnnotationConfigServletWebServerApplicationContext();
	this.context.register(AuthorizationAndResourceServerConfiguration.class,
			MinimalSecureWebApplication.class);
	this.context.refresh();
	this.context.getBean(AUTHORIZATION_SERVER_CONFIG);
	this.context.getBean(RESOURCE_SERVER_CONFIG);
	this.context.getBean(OAuth2MethodSecurityConfiguration.class);
	ClientDetails config = this.context.getBean(BaseClientDetails.class);
	AuthorizationEndpoint endpoint = this.context
			.getBean(AuthorizationEndpoint.class);
	UserApprovalHandler handler = (UserApprovalHandler) ReflectionTestUtils
			.getField(endpoint, "userApprovalHandler");
	ClientDetailsService clientDetailsService = this.context
			.getBean(ClientDetailsService.class);
	ClientDetails clientDetails = clientDetailsService
			.loadClientByClientId(config.getClientId());
	assertThat(AopUtils.isJdkDynamicProxy(clientDetailsService)).isTrue();
	assertThat(AopUtils.getTargetClass(clientDetailsService).getName())
			.isEqualTo(InMemoryClientDetailsService.class.getName());
	assertThat(handler).isInstanceOf(ApprovalStoreUserApprovalHandler.class);
	assertThat(clientDetails).isEqualTo(config);
	verifyAuthentication(config);
	assertThat(this.context.getBeanNamesForType(OAuth2RestOperations.class))
			.isEmpty();
}
 
开发者ID:spring-projects,项目名称:spring-security-oauth2-boot,代码行数:28,代码来源:OAuth2AutoConfigurationTests.java

示例13: authoritiesExtractor

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
@Bean
@SuppressWarnings("unchecked")
public AuthoritiesExtractor authoritiesExtractor(OAuth2RestOperations template) {
	return map -> {
		ArrayList<String> list = (ArrayList<String>) map.get("authorities");
		if (!list.isEmpty()) {
			return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils.collectionToCommaDelimitedString(list));
		}
		return AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
	};
}
 
开发者ID:allianzit,项目名称:ait-platform,代码行数:12,代码来源:AitZuulServer.java

示例14: getRestTemplate

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
/**
 * Builds proxy instance of {@link RestTemplate} which load OAuth resouce details from DB on each operation
 *
 * @param name                Name/ID of resource of {@link RestTemplate}
 * @param oauth2ClientContext OAuth Client context
 * @return Proxy instance of {@link RestTemplate}
 */
public OAuth2RestOperations getRestTemplate(String name, OAuth2ClientContext oauth2ClientContext) {
	return newProxy(OAuth2RestOperations.class, (proxy, method, args) -> {
		try {
			return method.invoke(new OAuth2RestTemplate(loadResourceDetails(name), oauth2ClientContext), args);

		} catch (InvocationTargetException e) {
			throw e.getTargetException();
		}
	});
}
 
开发者ID:reportportal,项目名称:service-authorization,代码行数:18,代码来源:AuthConfigService.java

示例15: authorizationCode

import org.springframework.security.oauth2.client.OAuth2RestOperations; //导入依赖的package包/类
/**
 * 演示 grant_type=authorization_code 时,获取资源的方法
 *
 * @return
 */
@RequestMapping("/authorization_code")
@ResponseBody
public JsonNode authorizationCode() {
    logger.info("grant_type=authorization_code  ... ");
    OAuth2RestOperations operations = restTemplate.authorizationCodeRestTemplate(client_id, client_secret, user_authorization_uri, access_token_uri, scopes);
    logger.info("access_token:" + operations.getAccessToken().getValue());

    return operations.getForObject(resource_server_url +"/foos/1", JsonNode.class);  // getForObject 发送 get 方法,获取的数据类型由具体的数据类型确定,此处 JsonNode 表示返回值为 json 类型
}
 
开发者ID:h819,项目名称:spring-boot,代码行数:15,代码来源:SpringOauth2ClientController.java


注:本文中的org.springframework.security.oauth2.client.OAuth2RestOperations类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。