Need to create oAuth2 token manually without password

前端 未结 3 495
花落未央
花落未央 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:40

    Assign access token while signup process, Spring boot. Call getAccessToken(user) from anywhere in your app code.

    public OAuth2AccessToken getAccessToken(User user) {
        HashMap authorizationParameters = new HashMap();
        authorizationParameters.put("scope", "read");
        authorizationParameters.put("username", user.getEmail());
        authorizationParameters.put("client_id", clientId);
        authorizationParameters.put("grant", "password");
    
        Set authorities = new HashSet();
        user.getRoles().forEach((role) -> {
            Role rol = roleRepository.findByName(role.getName());
            authorities.add(new SimpleGrantedAuthority(rol.getName()));
        });
    
        Set responseType = new HashSet();
        responseType.add("password");
    
        Set scopes = new HashSet();
        scopes.add("read");
        scopes.add("write");
    
        OAuth2Request authorizationRequest = new OAuth2Request(authorizationParameters, clientId, authorities, true,
                scopes, null, "", responseType, null);
    
        org.springframework.security.core.userdetails.User userPrincipal = new org.springframework.security.core.userdetails.User(
                user.getEmail(), user.getPassword(), authorities);
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userPrincipal,
                null, authorities);
    
        OAuth2Authentication authenticationRequest = new OAuth2Authentication(authorizationRequest,
                authenticationToken);
        authenticationRequest.setAuthenticated(true);
        OAuth2AccessToken accessToken = tokenServices().createAccessToken(authenticationRequest);
    
        return accessToken;
    }
    
    @Bean
    TokenEnhancerChain enhancerChain() {
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        enhancerChain.setTokenEnhancers(Arrays.asList(customTokenEnhancer, accessTokenConverter()));
        return enhancerChain;
    }
    
    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey(signingKey);
        return converter;
    }
    
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }
    
    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        defaultTokenServices.setTokenEnhancer(enhancerChain());
        return defaultTokenServices;
    }
    

提交回复
热议问题