Spring OAuth2-创建访问令牌

Spring OAuth2-创建访问令牌,spring,spring-security,oauth-2.0,access-token,Spring,Spring Security,Oauth 2.0,Access Token,我使用这里描述的方法创建OAuth2访问令牌: 此方法适用于spring-security-oauth2 1.0.5.RELEASE,但不适用于spring-security-oauth2 2.0.6.RELEASE 有没有一种方法可以让spring-security-oauth2.0.6.RELEASE实现同样的功能?下面是一个使用spring-security-oauth2.0.6.RELEASE的Rest控制器方法示例 @RequestMapping("/token") public O

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

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


有没有一种方法可以让spring-security-oauth2.0.6.RELEASE实现同样的功能?

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

@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;
}
@RequestMapping(“/token”)
公共OAuth2AccessToken令牌(主体){
Set authorities=new HashSet();
添加(新的SimpleGrantedAuthority(“角色用户”);
Map requestParameters=new HashMap();
字符串clientId=“acme”;
布尔批准=真;
Set scope=new HashSet();
范围。添加(“范围”);
Set resourceIds=newhashset();
Set responseType=new HashSet();
响应类型。添加(“代码”);
Map extensionProperties=new HashMap();
OAuth2Request OAuth2Request=新的OAuth2Request(requestParameters,clientId,
授权、批准、范围、,
ResourceId、null、ResponseType、extensionProperties);
User userPrincipal=新用户(principal.getName(),“”,true,true,true,authorities);
UsernamePasswordAuthenticationToken authenticationToken=新的UsernamePasswordAuthenticationToken(userPrincipal,null,authorities);
OAuth2Authentication auth=新的OAuth2Authentication(oAuth2Request,authenticationToken);
OAuth2AccessToken token=defaultTokenServices.createAccessToken(auth);
返回令牌;
}

希望能有所帮助。

非常感谢您对我的关注:您需要自己连接您的defaultTokenServices,否则会连接错误的实例。defaultTokenServices service=new defaultTokenServices();OAuth2AccessToken=service.createAccessToken(auth);InMemoryTokenStore InMemoryTokenStore=新建InMemoryTokenStore();service.setTokenStore(inMemoryTokenStore);/*您可能希望在MemoryTokenStore中为scope*/(添加到Kumaresan addition中)设置最终静态或类似设置