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

The following examples show how to use org.springframework.security.oauth2.provider.client.BaseClientDetails#setAuthorities() . 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: UaaClientOperationTest.java    From uaa-java-client with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {

	operations = getConnection().clientOperations();

	try {
		operations.delete("test");
	} catch (Exception ignore) {}

	testClientDetails = new BaseClientDetails();
	testClientDetails.setClientId("test");
	testClientDetails.setClientSecret("testsecret");
	testClientDetails.setAccessTokenValiditySeconds(3600);
	testClientDetails.setAuthorizedGrantTypes(Arrays.asList(UaaTokenGrantType.authorization_code.toString(),
			UaaTokenGrantType.client_credentials.toString()));
	testClientDetails.setRefreshTokenValiditySeconds(86400);
	testClientDetails.setAuthorities(AuthorityUtils.createAuthorityList("uaa.resource", "clients.secret"));

	testClient = operations.create(testClientDetails);
}
 
Example 3
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 4
Source File: OAuth2AuthorizationServerConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@Bean
@ConfigurationProperties(prefix = "security.oauth2.client")
public BaseClientDetails oauth2ClientDetails() {
	BaseClientDetails details = new BaseClientDetails();
	if (this.client.getClientId() == null) {
		this.client.setClientId(UUID.randomUUID().toString());
	}
	details.setClientId(this.client.getClientId());
	details.setClientSecret(this.client.getClientSecret());
	details.setAuthorizedGrantTypes(Arrays.asList("authorization_code", "password", "client_credentials",
			"implicit", "refresh_token"));
	details.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
	details.setRegisteredRedirectUri(Collections.<String>emptySet());
	return details;
}