【问题标题】:Spring OAuth2 - Create an access tokenSpring OAuth2 - 创建访问令牌
【发布时间】:2015-04-12 23:07:49
【问题描述】:

我正在使用此处描述的方法来创建 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 做同样的事情?

【问题讨论】:

    标签: spring spring-security oauth-2.0 access-token


    【解决方案1】:

    这里是使用 spring-security-oauth2 2.0.6.RELEASE 的示例 Rest Controller 方法

    @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;
    }
    

    希望对你有帮助。

    【讨论】:

    • 对于那些追随我的人:你需要自己连接你的 defaultTokenServices,注入它会连接错误的实例。
    • DefaultTokenServices service = new DefaultTokenServices(); OAuth2AccessToken 令牌 = service.createAccessToken(auth);
    • InMemoryTokenStore inMemoryTokenStore = new InMemoryTokenStore(); service.setTokenStore(inMemoryTokenStore); /* 你可能想让 inMemoryTokenStore 成为最终的静态或类似的作用域 */(添加到 Kumaresan 加法)
    猜你喜欢
    • 2013-09-03
    • 2018-12-01
    • 2019-03-13
    • 2018-05-03
    • 1970-01-01
    • 2015-06-19
    • 2018-05-06
    • 2017-04-11
    • 2016-01-04
    相关资源
    最近更新 更多