5

Yay güvenliği ile oAuth2 uyguladım ve benim için iyi çalışıyor. Ama şimdi arka uçtan kullanıcı şifresini parola olmadan oluşturmak istiyorum. Çünkü sadece kullanıcı adıma sahibim.OAuth2 jetonu parola olmadan manüel olarak yaratmanız gerekiyor

Herhangi biri bana yardımcı olabilir.

+0

Şimdiye kadar ne yaptın? İlk önce kendin yapmayı denedin mi? – aribeiro

+0

Normal kullanıcı kullanıcı/parola ile giriş yapmıştır ve oAuth2 jetonu başarıyla oluşturulmuştur. Ama şifre olmadan arka ucunu kullanarak başka kullanıcı belirteci oluşturmanız gerekir. –

cevap

13

Cevabınız evet!

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 istediğiniz hangi simge olduğunu.

Teşekkürler

+0

Harika, teşekkürler! –

+0

Teşekkürler Çok Fazla @ChristiaanJanssen. Ayrıca sizin için değerli olup olmadığını lütfen kabul edin. –

İlgili konular