5

Ich habe oAuth2 mit Federsicherheit implementiert und es funktioniert gut für mich. Aber jetzt möchte ich Benutzer Token von Back-End manuell ohne Passwort erstellen. Weil ich nur den Benutzernamen des Benutzers habe.Müssen oAuth2 Token manuell ohne Passwort erstellen

Kann mir jemand helfen.

+0

Was hast du bisher gemacht? Hast du es zuerst selbst versucht? – aribeiro

+0

Normaler Benutzer ist Login mit Benutzer/Passwort und das oAuth2 Token wurde erfolgreich erstellt. Aber ich muss andere Benutzer-Token mit Back-End ohne Passwort erstellen. –

Antwort

13

Antwort erhalten !!!

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 ist Token, die Sie möchten.

Danke

+0

Super, danke! –

+0

Vielen Dank @ChristiaanJanssen. Auch bitte upvote wenn es für Sie wertvoll ist. –

Verwandte Themen