Java Code Examples for org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer#inMemory()

The following examples show how to use org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer#inMemory() . 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: AuthorizationServerConfiguration.java    From fw-spring-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * 配置客户端一些信息
 *
 * @param clients
 * @throws Exception
 */
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    InMemoryClientDetailsServiceBuilder build = clients.inMemory();
    if (ArrayUtil.isNotEmpty(securityProperties.getOauth().getClients())) {
        for (OAuth2ClientProperties config : securityProperties.getOauth().getClients()) {
            build.withClient(config.getClientId())
                    .secret(passwordEncoder.encode(config.getClientSecret()))
                    .accessTokenValiditySeconds(config.getAccessTokenValiditySeconds())
                    .refreshTokenValiditySeconds(60 * 60 * 24 * 15)
                    .authorizedGrantTypes("refresh_token", "password", "authorization_code")//OAuth2支持的验证模式
                    .redirectUris("http://www.baidu.com")
                    .scopes("all");
        }
    }
}
 
Example 2
Source File: AuthorizationServerConfiguration.java    From fw-spring-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * 配置客户端一些信息
 *
 * @param clients
 * @throws Exception
 */
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    InMemoryClientDetailsServiceBuilder build = clients.inMemory();
    if (ArrayUtil.isNotEmpty(securityProperties.getOauth().getClients())) {
        for (OAuth2ClientProperties config : securityProperties.getOauth().getClients()) {
            build.withClient(config.getClientId())
                    .secret(passwordEncoder.encode(config.getClientSecret()))
                    .accessTokenValiditySeconds(config.getAccessTokenValiditySeconds())
                    .refreshTokenValiditySeconds(config.getRefreshTokenValiditySecond())
                    .authorizedGrantTypes("refresh_token", "password", "authorization_code")//OAuth2支持的验证模式
                    .redirectUris(config.getRedirectUri())
                    .autoApprove(config.getAutoApprove())//设置自动认证
                    .scopes(config.getScope());
        }
    }
}
 
Example 3
Source File: SophiaAuthorizationServerConfig.java    From sophia_scaffolding with Apache License 2.0 6 votes vote down vote up
/**
 * 配置客户端详情信息,客户端详情信息在这里进行初始化,通过数据库来存储调取详情信息
 */
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    InMemoryClientDetailsServiceBuilder builder = clients.inMemory();
    if (ArrayUtils.isNotEmpty(securityProperties.getOauth2().getClients())) {
        for (OAuth2ClientProperties client : securityProperties.getOauth2().getClients()) {
            builder
                    .withClient(client.getClientId())
                    .secret(new BCryptPasswordEncoder().encode(client.getClientSecret()))
                    // .resourceIds("admin","auth")
                    //设置token的有效期,不设置默认12小时
                    .accessTokenValiditySeconds(client.getAccessTokenValidatySeconds())
                    //设置刷新token的有效期,不设置默认30天
                    .refreshTokenValiditySeconds(client.getRefreshTokenValiditySeconds())
                    .redirectUris("http://www.baidu.com")
                    .authorizedGrantTypes("authorization_code","client_credentials", "refresh_token", "password")
                    .scopes("all", "read", "write")
                    .autoApprove(true);
        }
    }
}
 
Example 4
Source File: AuthorizationServerConfig.java    From springboot-seed with MIT License 6 votes vote down vote up
/**
 * 配置客户端
 *
 * @param clients
 * @throws Exception
 */
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    InMemoryClientDetailsServiceBuilder build = clients.inMemory();
    if (ArrayUtils.isNotEmpty(oAuth2Properties.getClients())) {
        for (OAuth2ClientProperties config : oAuth2Properties.getClients()) {
            String password = passwordEncoder.encode(config.getClientSecret());
            build.withClient(config.getClientId())
                    .secret(password)
                    .accessTokenValiditySeconds(config.getAccessTokenValiditySeconds())
                    .refreshTokenValiditySeconds(config.getRefreshTokenValiditySeconds())
                    .authorizedGrantTypes("refresh_token", "password", "authorization_code")
                    .redirectUris(config.getRedirectUri())
                    .scopes("all");
        }
    }
}
 
Example 5
Source File: ApiBootAuthorizationMemoryServerAutoConfiguration.java    From beihu-boot with Apache License 2.0 5 votes vote down vote up
/**
 * configuration clients
 *
 * @param clients client details service configuration
 * @throws Exception exception
 */
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    InMemoryClientDetailsServiceBuilder inMemoryClientDetailsServiceBuilder = clients.inMemory();
    apiBootOauthProperties.getClients().stream().forEach(client -> inMemoryClientDetailsServiceBuilder.withClient(client.getClientId())
            .secret(passwordEncoder().encode(client.getClientSecret()))
            .authorizedGrantTypes(client.getGrantTypes())
            .scopes(client.getScopes())
            .resourceIds(client.getResourceId())
            .accessTokenValiditySeconds(client.getAccessTokenValiditySeconds()));
}
 
Example 6
Source File: ApiBootAuthorizationServerRedisAutoConfiguration.java    From beihu-boot with Apache License 2.0 5 votes vote down vote up
/**
 * configuration clients
 *
 * @param clients client details service configuration
 * @throws Exception exception
 */
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    InMemoryClientDetailsServiceBuilder inMemoryClientDetailsServiceBuilder = clients.inMemory();
    apiBootOauthProperties.getClients().stream().forEach(client -> inMemoryClientDetailsServiceBuilder.withClient(client.getClientId())
            .secret(passwordEncoder().encode(client.getClientSecret()))
            .authorizedGrantTypes(client.getGrantTypes())
            .scopes(client.getScopes())
            .resourceIds(client.getResourceId())
            .accessTokenValiditySeconds(client.getAccessTokenValiditySeconds()));
}
 
Example 7
Source File: Oauth2AuthorizationServerConfig.java    From spring-security-oauth2-demo with GNU General Public License v3.0 5 votes vote down vote up
private void configClient(ClientDetailsServiceConfigurer clients) throws Exception {
    InMemoryClientDetailsServiceBuilder builder = clients.inMemory();
    for (BaseClientDetails client : clientDetails.getClient()) {
        ClientDetailsServiceBuilder<InMemoryClientDetailsServiceBuilder>.ClientBuilder clientBuilder =
                builder.withClient(client.getClientId());
        clientBuilder
                .secret(client.getClientSecret())
                .resourceIds(client.getResourceIds().toArray(new String[0]))
                .authorizedGrantTypes(client.getAuthorizedGrantTypes().toArray(new String[0]))
                .authorities(
                        AuthorityUtils.authorityListToSet(client.getAuthorities())
                                .toArray(new String[0]))
                .scopes(client.getScope().toArray(new String[0]));
        if (client.getAutoApproveScopes() != null) {
            clientBuilder.autoApprove(
                    client.getAutoApproveScopes().toArray(new String[0]));
        }
        if (client.getAccessTokenValiditySeconds() != null) {
            clientBuilder.accessTokenValiditySeconds(
                    client.getAccessTokenValiditySeconds());
        }
        if (client.getRefreshTokenValiditySeconds() != null) {
            clientBuilder.refreshTokenValiditySeconds(
                    client.getRefreshTokenValiditySeconds());
        }
        if (client.getRegisteredRedirectUri() != null) {
            clientBuilder.redirectUris(
                    client.getRegisteredRedirectUri().toArray(new String[0]));
        }
    }
}
 
Example 8
Source File: ApiBootAuthorizationMemoryServerAutoConfiguration.java    From api-boot with Apache License 2.0 5 votes vote down vote up
/**
 * configuration clients
 *
 * @param clients client details service configuration
 * @throws Exception exception
 */
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    InMemoryClientDetailsServiceBuilder inMemoryClientDetailsServiceBuilder = clients.inMemory();
    apiBootOauthProperties.getClients().stream().forEach(client -> inMemoryClientDetailsServiceBuilder.withClient(client.getClientId())
        .secret(passwordEncoder().encode(client.getClientSecret()))
        .authorizedGrantTypes(client.getGrantTypes())
        .scopes(client.getScopes())
        .resourceIds(client.getResourceId())
        .accessTokenValiditySeconds(client.getAccessTokenValiditySeconds())
        .refreshTokenValiditySeconds(client.getRefreshTokenValiditySeconds()));
}
 
Example 9
Source File: ApiBootAuthorizationServerRedisAutoConfiguration.java    From api-boot with Apache License 2.0 5 votes vote down vote up
/**
 * configuration clients
 *
 * @param clients client details service configuration
 * @throws Exception exception
 */
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    InMemoryClientDetailsServiceBuilder inMemoryClientDetailsServiceBuilder = clients.inMemory();
    apiBootOauthProperties.getClients().stream().forEach(client -> inMemoryClientDetailsServiceBuilder.withClient(client.getClientId())
        .secret(passwordEncoder().encode(client.getClientSecret()))
        .authorizedGrantTypes(client.getGrantTypes())
        .scopes(client.getScopes())
        .resourceIds(client.getResourceId())
        .accessTokenValiditySeconds(client.getAccessTokenValiditySeconds())
        .refreshTokenValiditySeconds(client.getRefreshTokenValiditySeconds()));
}
 
Example 10
Source File: AuthorizationServerConfiguration.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
protected void configInMemory(ClientDetailsServiceConfigurer clients) throws Exception{
	Map<String, MemoryUser> clientUsers = oauth2Properties.getAuthorizationServer().getClientDetails();
	InMemoryClientDetailsServiceBuilder inMemory = clients.inMemory();
	clientUsers.forEach((user, config)->{
		ClientBuilder cb = inMemory.withClient(user).secret(config.getSecret());
		
		if(!LangUtils.isEmpty(config.getScopes())){
			cb.scopes(config.getScopes());
		}
		if(!LangUtils.isEmpty(config.getAuthorities())){
			cb.authorities(config.getAuthorities());
		}
		if(config.getAccessTokenValiditySeconds()!=null){
			cb.accessTokenValiditySeconds(config.getAccessTokenValiditySeconds());
		}
		cb.autoApprove(config.isAutoApprove());
		if(!LangUtils.isEmpty(config.getAutoApproveScopes())){
			cb.autoApprove(config.getAutoApproveScopes());
		}
		if(!LangUtils.isEmpty(config.getResourceIds())){
			cb.resourceIds(config.getResourceIds());
		}
		if(!LangUtils.isEmpty(config.getAuthorizedGrantTypes())){
			cb.authorizedGrantTypes(config.getAuthorizedGrantTypes());
		}
		if(config.getRefreshTokenValiditySeconds()!=null){
			cb.refreshTokenValiditySeconds(config.getRefreshTokenValiditySeconds());
		}
		if(!LangUtils.isEmpty(config.getRegisteredRedirectUris())){
			cb.redirectUris(config.getRegisteredRedirectUris());
		}
	});
	inMemory.build();
}