首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring OAuth2 -创建访问令牌

Spring OAuth2 -创建访问令牌
EN

Stack Overflow用户
提问于 2015-02-12 17:22:44
回答 1查看 8.8K关注 0票数 6

我使用这里描述的方法来创建一个OAuth2访问令牌:

Spring OAuth2 - Manually creating an access token in the token store

此方法适用于spring-security-oauth2 1.0.5.RELEASE,但不适用于spring-security-oauth2 2.0.6.RELEASE。

有没有办法用spring-security-oauth2 2.0.6.RELEASE做同样的事情?

EN

回答 1

Stack Overflow用户

发布于 2015-02-18 02:56:59

下面是使用spring-security-oauth2 2.0.6.RELEASE的Rest控制器方法示例

代码语言:javascript
复制
@RequestMapping("/token")
public OAuth2AccessToken token(Principal principal) {
    Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

    Map<String, String> requestParameters = new HashMap<>();
    String clientId = "acme";
    boolean approved = true;
    Set<String> scope = new HashSet<>();
    scope.add("scope");
    Set<String> resourceIds = new HashSet<>();
    Set<String> responseTypes = new HashSet<>();
    responseTypes.add("code");
    Map<String, Serializable> extensionProperties = new HashMap<>();

    OAuth2Request oAuth2Request = new OAuth2Request(requestParameters, clientId,
            authorities, approved, scope,
            resourceIds, null, responseTypes, extensionProperties);


    User userPrincipal = new User(principal.getName(), "", true, true, true, true, authorities);

    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userPrincipal, null, authorities);
    OAuth2Authentication auth = new OAuth2Authentication(oAuth2Request, authenticationToken);
    OAuth2AccessToken token = defaultTokenServices.createAccessToken(auth);
    return token;
}

希望能有所帮助。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28473659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档