2017-05-23 7 views
2

Je veux les utilisateurs de whitelist se connecter à mon client OAuth2, et je ne peux pas comprendre comment obtenir le nom d'utilisateur (en particulier l'adresse e-mail Google).Comment obtenir email utilisateur de Google aux utilisateurs une liste blanche lors de l'authentification avec Spring Boot OAuth2 contre Google

J'ai créé une application Spring Boot OAuth2 basé sur un ressort Tutorial

https://spring.io/guides/tutorials/spring-boot-oauth2/#_social_login_manual

J'authentification contre Google (avec succès). Je souhaite déterminer l'adresse e-mail de l'utilisateur afin que je puisse ajouter des utilisateurs authentifiés à la liste blanche.

Ce site,

http://www.baeldung.com/spring-security-openid-connect#filter

suggère que je peux déballer le "id_token" Je reviens de Google, quelque chose comme ceci:

/** 
* logic to unpack a ConnectID id_token like what we get from Google - 
* see "Spring Security and OpenID Connect" - heading '4. Custom OpenID Connect Filter': 
*   http://www.baeldung.com/spring-security-openid-connect#filter 
* 
* @param oa2token 
* @return 
*/ 
private static UsernamePasswordAuthenticationToken getOpenIDDataForToken(OAuth2AccessToken oa2token) 
{ 
     try { 
      String idToken = oa2token.getAdditionalInformation().get("id_token").toString(); 
      Jwt tokenDecoded = JwtHelper.decode(idToken); 
      Map<String, String> authInfo = new ObjectMapper().readValue(tokenDecoded.getClaims(), Map.class); 

      OpenIdConnectUserDetails user = new OpenIdConnectUserDetails(authInfo, oa2token); 
      return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities()); 
     } catch (InvalidTokenException e) { 
      throw new BadCredentialsException("Could not obtain user details from token", e); 
     } 
} 

mais je ne peux pas obtenir ce code compiler - Je ne peux pas comprendre comment obtenir la classeJtwHelper!

J'ai cherché autour et les suivantes pourraient être la bonne dépendance Maven:

 <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-jwt --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-jwt</artifactId> 
     </dependency> 

mais en ajoutant à mon pom.xml ne contribue pas - je ne suis pas un vrai fichier Jar dans mon. Référentiel m2 - Je reçois un fichier texte !!! et la ligne du bas, Eclipse ne résout pas le type JwtHelper.

aide? Je ne sais pas où je me suis trompé.

+0

double possible de [Comment obtenir les informations d'utilisateur personnalisé à partir de terminal serveur/utilisateur d'autorisation OAuth2] (https://stackoverflow.com/questions/35056169/how-to-get-custom-user-info-from -oauth2-authorization-server-user-endpoint) – dur

Répondre

0

On dirait une réponse sur cette SO la page avait ma réponse (merci @ user2802927):

How to get custom user info from OAuth2 authorization server /user endpoint

Voici le code:

Principal principal = servlet_request.getUserPrincipal(); 
    try { 
     if (principal != null) { 
       OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) principal; 
       Authentication authentication = oAuth2Authentication.getUserAuthentication(); 
       Map<String, String> details = new LinkedHashMap<>(); 
       details = (Map<String, String>) authentication.getDetails(); 
       Map<String, String> map = new LinkedHashMap<>(); 
       map.put("email", details.get("email")); 
       logger.debug("details map is: {}", map); 
      } 
    } catch (Exception e) { 
     logger.error("dumping principal " + principal + "failed, exception: ", e); 
    } 

La sortie a montré que j'ai trouvé le succès - l'adresse email de l'utilisateur !!!

2017-05-23 11:48:26.751 DEBUG 7687 --- [nio-8443-exec-1] ication$$EnhancerBySpringCGLIB$$91415b85 : 
details map is: {[email protected]}