博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Bood 配置OAuth2 ClientDetailsServiceConfigurer
阅读量:6089 次
发布时间:2019-06-20

本文共 4866 字,大约阅读时间需要 16 分钟。

hot3.png

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
scope; 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");        List
authorities = 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);    }

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

你可能感兴趣的文章
CSS3 transforms 3D翻开
查看>>
java基础---->正则表达式
查看>>
2.2013/06/13_log(n)+1
查看>>
关于加载iframe时进度条不消失的问题
查看>>
poj 3984迷宫问题【广搜】
查看>>
oracle ORA-01840:输入值对于日期格式不够长
查看>>
python基础知识~logger模块
查看>>
SIP入门(二):建立SIPserver
查看>>
Servlet3.0的异步
查看>>
WebService连接postgresql( 失败尝试)
查看>>
从头认识java-13.11 对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题?...
查看>>
Python-MacOSX下SIP引起的pip权限问题解决方案(非取消SIP机制)
查看>>
从MFQ方法到需求分析
查看>>
android.view.WindowManager$BadTokenException: Unable to add window
查看>>
HDU5012:Dice(bfs模板)
查看>>
iphone openssh
查看>>
Linux下MEncoder的编译
查看>>
spark高级排序彻底解秘
查看>>
ylbtech-LanguageSamples-PartialTypes(部分类型)
查看>>
福建省促进大数据发展:变分散式管理为统筹集中式管理
查看>>