2013-08-30 12 views
8

Mam sytuację, w której sam chciałbym utworzyć token dostępu (a nie zwykły proces). Mam wymyślić coś takiego:Wiosna OAuth2 - Ręczne tworzenie tokenu dostępu w magazynie znaczników

@Inject 
private DefaultTokenServices defaultTokenServices; 

... 

OAuth2Authentication auth = xxx; 
OAuth2AccessToken token = defaultTokenServices.createAccessToken(auth); 

Jedynym problemem jest to, że nie jestem pewien, jak stworzyć OAuth2Authentication (w moim kodzie część z XXX). Mam informacje o kliencie użytkownika & i wiem, które władze chcę nadać tokenowi.

+0

Rzeczywiście zrobiłem to niedawno, daj mi znać, jeśli nadal potrzebujesz kodu, ponieważ ten post jest nieco stary. – Michael

+0

tak, proszę. jestem zainteresowany – checklist

+0

Czy to było zgodne z tym, czego szukałeś? – Michael

Odpowiedz

15

Oto, Twój przypadek użycia może się nieznacznie różnić w zależności od używanego przepływu. Działa to w przypadku przepływu przydziału haseł. Istnieje kilka niestandardowych klas, takich jak sklep tokenowy, token enhancer. ale tak naprawdę są to tylko rozszerzone wersje klas sprężynowych zmodyfikowanych dla naszych własnych potrzeb.

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

     DefaultAuthorizationRequest authorizationRequest = new DefaultAuthorizationRequest(authorizationParameters); 
     authorizationRequest.setApproved(true); 

     Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); 
     authorities.add(new SimpleGrantedAuthority("ROLE_UNTRUSTED_CLIENT")); 
     authorizationRequest.setAuthorities(authorities); 

     HashSet<String> resourceIds = new HashSet<String>(); 
     resourceIds.add("mobile-public"); 
     authorizationRequest.setResourceIds(resourceIds); 

     // Create principal and auth token 
     User userPrincipal = new User(user.getUserID(), "", true, true, true, true, authorities); 

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

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

     CustomTokenStore tokenStore = new CustomTokenStore(); 

     // Token Enhancer 
     CustomTokenEnhancer tokenEnhancer = new CustomTokenEnhancer(user.getUserID()); 

     CustomTokenServices tokenServices = new CustomTokenServices(); 
     tokenServices.setTokenEnhancer(tokenEnhancer); 
     tokenServices.setSupportRefreshToken(true); 
     tokenServices.setTokenStore(tokenStore); 

     OAuth2AccessToken accessToken = tokenServices.createAccessTokenForUser(authenticationRequest, user); 
10

Oto jak wygenerować token za pomocą interfejsu TokenEndpoint (używany narazić usługi REST):

@Inject 
private TokenEndpoint tokenEndpoint; 

public ResponseEntity<?> getToken(Principal principal) { 

     HashMap<String, String> parameters = new HashMap<String, String>(); 
     parameters.put("client_id", "appid"); 
     parameters.put("client_secret", "myOAuthSecret"); 
     parameters.put("grant_type", "password"); 
     parameters.put("password", myUser.getPassword()); 
     parameters.put("scope", "read write"); 
     parameters.put("username", myUser.getLogin()); 

     return tokenEndpoint.getAccessToken(principal, parameters); 
} 
4

inny sposób, aby ręcznie wygenerować OAuth2 Accesss Token możemy użyć instancji TokenService

@Autowired 
private AuthorizationServerEndpointsConfiguration configuration; 

@Override 
public String generateOAuth2AccessToken(User user, List<Role> roles, List<String> scopes) { 

    Map<String, String> requestParameters = new HashMap<String, String>(); 
    Map<String, Serializable> extensionProperties = new HashMap<String, Serializable>(); 

    boolean approved = true; 
    Set<String> responseTypes = new HashSet<String>(); 
    responseTypes.add("code"); 

    // Authorities 
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); 
    for(Role role: roles) 
     authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName())); 

    OAuth2Request oauth2Request = new OAuth2Request(requestParameters, "clientIdTest", authorities, approved, new HashSet<String>(scopes), new HashSet<String>(Arrays.asList("resourceIdTest")), null, responseTypes, extensionProperties); 

    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUsername(), "N/A", authorities); 

    OAuth2Authentication auth = new OAuth2Authentication(oauth2Request, authenticationToken); 

    AuthorizationServerTokenServices tokenService = configuration.getEndpointsConfigurer().getTokenServices(); 

    OAuth2AccessToken token = tokenService.createAccessToken(auth); 

    return token.getValue(); 
} 
+0

dzięki takiemu podejściu możemy uzyskać dostęp do zasobów za pomocą generowanego tokena dostępu, na wygasającym tokenie dostępu nie wydamy tokenu dostępu z odświeżającym tokenem. daje nieautoryzowanego klienta, nawet jeśli dane klienta są poprawne –

Powiązane problemy