Java Code Examples for org.springframework.security.oauth2.provider.client.BaseClientDetails#setScope()

The following examples show how to use org.springframework.security.oauth2.provider.client.BaseClientDetails#setScope() . 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: OAuth2AutoConfigurationTests.java    From spring-security-oauth2-boot with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthorizationServerOverride() {
	this.context = new AnnotationConfigServletWebServerApplicationContext();
	TestPropertyValues.of("security.oauth2.resourceId:resource-id").applyTo(this.context);
	this.context.register(AuthorizationAndResourceServerConfiguration.class, CustomAuthorizationServer.class,
			MinimalSecureWebApplication.class);
	this.context.refresh();
	BaseClientDetails config = new BaseClientDetails();
	config.setClientId("client");
	config.setClientSecret("secret");
	config.setResourceIds(Arrays.asList("resource-id"));
	config.setAuthorizedGrantTypes(Arrays.asList("password"));
	config.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList("USER"));
	config.setScope(Arrays.asList("read"));
	assertThat(countBeans(AUTHORIZATION_SERVER_CONFIG)).isEqualTo(1);
	assertThat(countBeans(CustomAuthorizationServer.class)).isEqualTo(1);
	assertThat(countBeans(RESOURCE_SERVER_CONFIG)).isEqualTo(1);
	verifyAuthentication(config);
}
 
Example 2
Source File: SysClientDetailService.java    From springcloud-oauth2 with MIT License 5 votes vote down vote up
/**
 * 根据客户端id查询
 * @param clientId
 * @return org.springframework.security.oauth2.provider.client.BaseClientDetails
 */
public BaseClientDetails selectById(String clientId) {
    BaseClientDetails clientDetails = new BaseClientDetails();
    clientDetails.setAuthorities(new ArrayList<>());
    clientDetails.setClientId("yaohw");
    // 这个客户端秘钥和密码一样存BCryptPasswordEncoder加密后的接口,具体看定义的加密器
    clientDetails.setClientSecret("$2a$10$CwIutywnbs9bifHaY3Ezu.gYkWi4Zano8gVPq08hXjal6.uj.Yzuy");
    // 设置accessToken和refreshToken的时效,如果不设置则使tokenServices的配置的
    clientDetails.setAccessTokenValiditySeconds((int) TimeUnit.HOURS.toSeconds(2));
    clientDetails.setRefreshTokenValiditySeconds((int)TimeUnit.DAYS.toSeconds(30));
    // 资源id列表,需要注意的是这里配置的需要与ResourceServerConfig中配置的相匹配
    List<String> resourceIds = new ArrayList<>();
    resourceIds.add("auth-server");
    resourceIds.add("resource-server");
    clientDetails.setResourceIds(resourceIds);
    List<String> scopes = new ArrayList<>(1);
    scopes.add("sever");
    clientDetails.setScope(scopes);
    List<String> grantTypes = new ArrayList<>(5);
    grantTypes.add("password");
    grantTypes.add("refresh_token");
    grantTypes.add("authorization_code");
    grantTypes.add("implicit");
    grantTypes.add("mobile");
    clientDetails.setAuthorizedGrantTypes(grantTypes);
    Set<String> sets = new HashSet<>(1);
    sets.add("http://www.baidu.com");
    clientDetails.setRegisteredRedirectUri(sets);
    List<String> autoApproveScopes = new ArrayList<>(1);
    autoApproveScopes.add("sever");
    // 自动批准作用于,授权码模式时使用,登录验证后直接返回code,不再需要下一步点击授权
    clientDetails.setAutoApproveScopes(autoApproveScopes);
    return clientDetails;
}
 
Example 3
Source File: SophiaClientDetailsService.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
@Override
public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
    ApiResponse apiResponse = authorityClient.getOauthClientDetailsByClientId(clientId);
    OauthClientDetails model  = JSON.parseObject(JSON.toJSONString( apiResponse.getData(), true),OauthClientDetails.class);
    if (model == null) {
        throw new CommonException(SophiaHttpStatus.CLIENT_ERROR);
    }
    BaseClientDetails clientDetails = new BaseClientDetails();
    //客户端(client)id
    clientDetails.setClientId(model.getClientId());
    //客户端所能访问的资源id集合
    if (StringUtils.isNotEmpty(model.getResourceIds())) {
        clientDetails.setResourceIds(Arrays.asList(model.getResourceIds().split(",")));
    }
    //客户端(client)的访问密匙
    clientDetails.setClientSecret(new BCryptPasswordEncoder().encode(model.getClientSecret()));
    //客户端支持的grant_type授权类型
    clientDetails.setAuthorizedGrantTypes(Arrays.asList(model.getAuthorizedGrantTypes().split(",")));
    //客户端申请的权限范围
    clientDetails.setScope(Arrays.asList(model.getScope().split(",")));
    Integer accessTokenValidity = model.getAccessTokenValidity();
    if (accessTokenValidity != null && accessTokenValidity > 0) {
        //设置token的有效期,不设置默认12小时
        clientDetails.setAccessTokenValiditySeconds(accessTokenValidity);
    }
    Integer refreshTokenValidity = model.getRefreshTokenValidity();
    if (refreshTokenValidity != null && refreshTokenValidity > 0) {
        //设置刷新token的有效期,不设置默认30天
        clientDetails.setRefreshTokenValiditySeconds(refreshTokenValidity);
    }
    clientDetails.isAutoApprove(model.getAutoapprove());
    log.debug("clientId是:" + clientId);
    return clientDetails;
}
 
Example 4
Source File: SophiaClientDetailsService.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
@Override
public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
    ApiResponse apiResponse = authorityClient.getOauthClientDetailsByClientId(clientId);
    OauthClientDetails model  = JSON.parseObject(JSON.toJSONString( apiResponse.getData(), true),OauthClientDetails.class);
    if (model == null) {
        throw new CommonException(SophiaHttpStatus.CLIENT_ERROR);
    }
    BaseClientDetails clientDetails = new BaseClientDetails();
    //客户端(client)id
    clientDetails.setClientId(model.getClientId());
    //客户端所能访问的资源id集合
    if (StringUtils.isNotEmpty(model.getResourceIds())) {
        clientDetails.setResourceIds(Arrays.asList(model.getResourceIds().split(",")));
    }
    //客户端(client)的访问密匙
    clientDetails.setClientSecret(new BCryptPasswordEncoder().encode(model.getClientSecret()));
    //客户端支持的grant_type授权类型
    clientDetails.setAuthorizedGrantTypes(Arrays.asList(model.getAuthorizedGrantTypes().split(",")));
    //客户端申请的权限范围
    clientDetails.setScope(Arrays.asList(model.getScope().split(",")));
    Integer accessTokenValidity = model.getAccessTokenValidity();
    if (accessTokenValidity != null && accessTokenValidity > 0) {
        //设置token的有效期,不设置默认12小时
        clientDetails.setAccessTokenValiditySeconds(accessTokenValidity);
    }
    Integer refreshTokenValidity = model.getRefreshTokenValidity();
    if (refreshTokenValidity != null && refreshTokenValidity > 0) {
        //设置刷新token的有效期,不设置默认30天
        clientDetails.setRefreshTokenValiditySeconds(refreshTokenValidity);
    }
    clientDetails.isAutoApprove(model.getAutoapprove());
    log.debug("clientId是:" + clientId);
    return clientDetails;
}
 
Example 5
Source File: UaaClientOperationTest.java    From uaa-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdate() throws Exception {

	BaseClientDetails client = operations.findById(testClient.getClientId());

	client.setScope(Arrays.asList("foo"));
	BaseClientDetails updated = operations.update(client);

	assertNotEquals(testClient.getScope(), updated.getScope());
	assertEquals(client.getScope().iterator().next(), updated.getScope().iterator().next());
}