Spring Bood 配置OAuth2 ClientDetailsServiceConfigurer

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 Set<String> scope; 
    private Set<String> types; 
    private Set<String> autoApproveScopes; 
    private Set<String> resourceIds; 
    private Map<String, Object> additionalInformation; 
    private Set<String> registeredRedirectUris; 
    private List<GrantedAuthority> 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<GrantedAuthority> getAuthorities() {
        return this.authorities;
    } 
    /**
     * new SimpleGrantedAuthority(role)
     * @param authorities
     */
    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = new ArrayList(authorities);
    } 
    public Set<String> getAutoApproveScopes() {
        return autoApproveScopes;
    } 
    /**
     * values ["authorization_code", "password", "refresh_token", "implicit"]
     * @return
     */
    public Set<String> getAuthorizedGrantTypes() {
        return this.types;
    } 
    public void setAuthorizedGrantTypes(Set<String> types){
        this.types = types;
    } 
    public Integer getAccessTokenValiditySeconds() {
        return 7200;
    } 
    public Integer getRefreshTokenValiditySeconds() {
        return 7200;
    } 
    public Map<String, Object> getAdditionalInformation() {
        return Collections.unmodifiableMap(this.additionalInformation);
    } 
    public void addAdditionalInformation(String key, Object value) {
        this.additionalInformation.put(key, value);
    } 
    public Set<String> getRegisteredRedirectUri() {
        return this.registeredRedirectUris;
    } 
    public void setRegisteredRedirectUri(Set<String> registeredRedirectUris) {
        this.registeredRedirectUris = registeredRedirectUris == null?null:new LinkedHashSet(registeredRedirectUris);
    } 
    public Set<String> getResourceIds() {
        return this.resourceIds;
    } 
    /**
     * values ["read", "write"]
     * @return
     */
    public Set<String> getScope() {
        return this.scope;
    } 
    public void setScope(Set<String> 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.
 */
@Service
public 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");
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        authorities.add(new SimpleGrantedAuthority("READ"));
        authorities.add(new SimpleGrantedAuthority("WRITE"));
        app.setAuthorities(authorities);
        // 授权类型
        Set<String> authorizedGrantTypes = new TreeSet<String>();
        authorizedGrantTypes.add("password");
        authorizedGrantTypes.add("refresh_token");
        authorizedGrantTypes.add("authorization_code");
        app.setAuthorizedGrantTypes(authorizedGrantTypes);
        Set<String> scope = new TreeSet<String>();
        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);
    }

转载于:https://my.oschina.net/yan5845hao/blog/912424

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值