ClientDetailsService配置接口
- 实现ClientDetails接口
package com.uwo.oss.security.oauth2.configuration;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.oauth2.provider.ClientDetails;import java.util.*;/** * Created by yanhao on 2017/5/26. */public class OssApp implements ClientDetails { private String appId; private String secret; private String role; private Setscope; private Set types; private Set autoApproveScopes; private Set resourceIds; private Map additionalInformation; private Set registeredRedirectUris; private List authorities; public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getSecret() { return secret; } public void setSecret(String secret) { this.secret = secret; } public String getClientId() { return appId; } public String getClientSecret() { return secret; } public Collection getAuthorities() { return this.authorities; } /** * new SimpleGrantedAuthority(role) * @param authorities */ public void setAuthorities(Collection authorities) { this.authorities = new ArrayList(authorities); } public Set getAutoApproveScopes() { return autoApproveScopes; } /** * values ["authorization_code", "password", "refresh_token", "implicit"] * @return */ public Set getAuthorizedGrantTypes() { return this.types; } public void setAuthorizedGrantTypes(Set types){ this.types = types; } public Integer getAccessTokenValiditySeconds() { return 7200; } public Integer getRefreshTokenValiditySeconds() { return 7200; } public Map getAdditionalInformation() { return Collections.unmodifiableMap(this.additionalInformation); } public void addAdditionalInformation(String key, Object value) { this.additionalInformation.put(key, value); } public Set getRegisteredRedirectUri() { return this.registeredRedirectUris; } public void setRegisteredRedirectUri(Set registeredRedirectUris) { this.registeredRedirectUris = registeredRedirectUris == null?null:new LinkedHashSet(registeredRedirectUris); } public Set getResourceIds() { return this.resourceIds; } /** * values ["read", "write"] * @return */ public Set getScope() { return this.scope; } public void setScope(Set scope){ this.scope = scope; } public boolean isAutoApprove(String scope) { if(this.autoApproveScopes == null) { return false; } else { Iterator var2 = this.autoApproveScopes.iterator(); String auto; do { if(!var2.hasNext()) { return false; } auto = (String)var2.next(); } while(!auto.equals("true") && !scope.matches(auto)); return true; } } public boolean isSecretRequired() { return this.secret != null; } public boolean isScoped() { return this.scope != null && !this.scope.isEmpty(); }}
- 实现ClientDetailsService接口
package com.uwo.oss.security.oauth2.configuration; import org.apache.log4j.Logger;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.authority.SimpleGrantedAuthority;import org.springframework.security.oauth2.provider.ClientDetails;import org.springframework.security.oauth2.provider.ClientDetailsService;import org.springframework.security.oauth2.provider.ClientRegistrationException;import org.springframework.stereotype.Service; import java.util.ArrayList;import java.util.List;import java.util.Set;import java.util.TreeSet; /** * Created by yanhao on 2017/5/26. */@Servicepublic class OssClientDetailsService implements ClientDetailsService{ private final Logger log = Logger.getLogger(OssClientDetailsService.class); public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException { log.warn("loadClientByClientId"); OssApp app = new OssApp(); app.setAppId("123456"); app.setSecret("654321"); Listauthorities = new ArrayList (); authorities.add(new SimpleGrantedAuthority("READ")); authorities.add(new SimpleGrantedAuthority("WRITE")); app.setAuthorities(authorities); // 授权类型 Set authorizedGrantTypes = new TreeSet (); authorizedGrantTypes.add("password"); authorizedGrantTypes.add("refresh_token"); authorizedGrantTypes.add("authorization_code"); app.setAuthorizedGrantTypes(authorizedGrantTypes); Set scope = new TreeSet (); scope.add("openid"); app.setScope(scope); return app; }}
- 具体使用
/** * 管理客户端详情 * @param clients * @throws Exception */ @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { log.warn("configure ClientDetailsServiceConfigurer"); clients.withClientDetails(clientDetailsService); }