Need to create oAuth2 token manually without password

前端 未结 3 498
花落未央
花落未央 2021-02-03 13:12

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 us

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-03 13:29

    Got Answer!!!

        HashMap authorizationParameters = new HashMap();
        authorizationParameters.put("scope", "read");
        authorizationParameters.put("username", "user");
        authorizationParameters.put("client_id", "client_id");
        authorizationParameters.put("grant", "password");
    
        Set authorities = new HashSet();
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    
        Set responseType = new HashSet();
        responseType.add("password");
    
        Set scopes = new HashSet();
       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 is token which you want.

    Thanks

提交回复
热议问题