需要在没有密码的情况下手动创建 oAuth2 令牌 [英] Need to create oAuth2 token manually without password

查看:108
本文介绍了需要在没有密码的情况下手动创建 oAuth2 令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 spring 安全性实现了 oAuth2,它对我来说工作正常.但是现在我想在没有密码的情况下从后端手动创建用户令牌.因为我只有用户名.

I have implemented oAuth2 with spring security and it is working fine for me. But Now I want to create user token from back-end manually without password. Because I have only username of user.

谁能帮帮我.

推荐答案

得到答案!!!

    HashMap<String, String> authorizationParameters = new HashMap<String, String>();
    authorizationParameters.put("scope", "read");
    authorizationParameters.put("username", "user");
    authorizationParameters.put("client_id", "client_id");
    authorizationParameters.put("grant", "password");

    Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

    Set<String> responseType = new HashSet<String>();
    responseType.add("password");

    Set<String> scopes = new HashSet<String>();
   scopes.add("read");
   scopes.add("write");

    OAuth2Request authorizationRequest = new OAuth2Request(
            authorizationParameters, "Client_Id",
            authorities, true,scopes, null, "",
            responseType, null);

    User userPrincipal = new User("user", "", true, true, true, true, authorities);

    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
            userPrincipal, null, authorities);

    OAuth2Authentication authenticationRequest = new OAuth2Authentication(
            authorizationRequest, authenticationToken);
    authenticationRequest.setAuthenticated(true);

    OAuth2AccessToken accessToken = tokenService
            .createAccessToken(authenticationRequest);

accessToken 是你想要的令牌.

accessToken is token which you want.

谢谢

这篇关于需要在没有密码的情况下手动创建 oAuth2 令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆