Spring OAuth2 - 创建访问 token

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

我正在使用此处描述的方法来创建 OAuth2 访问 token :

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

希望能帮助到你。

关于Spring OAuth2 - 创建访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28473659/

相关文章:

java - 在 Spring 中,有没有办法从 YAML 文件渲染 Swagger 文档

java - 请解释 RestTemplate

java - 有没有关于使用 spring security 实现 2-legged oauth 的引用?

带有 Keycloak 的 Angular/Spring Boot 抛出 403

c# - 通过 C# 使用 OAuth2 RESTful 服务时授权错误 500

java - Spring boot sessionRegistry 返回空的主体列表

java - Spring JDBC使用什么方法获取最新的key

grails - springsecurity successHandler deafultTargetUrl无法正常工作

node.js - 获取oauth信息后如何将数据发送回客户端

google-app-engine - GAE 不导入 gflags