2017-09-21 2 views
1

Je passe par this tutorial sur la façon d'installer spring boot oauth avec jwt. Il couvre le décodage du jeton JWT en utilisant Angular, mais comment pouvons-nous le décoder et avoir accès aux revendications personnalisées dans le contrôleur de serveur de ressources?Accès à une charge JWT Spring OAuth 2 dans le contrôleur de serveur de ressources?

Par exemple avec JJWT il peut être fait comme celui-ci (Based on this article):

String subject = "HACKER"; 
    try { 
     Jws jwtClaims = 
      Jwts.parser().setSigningKey(key).parseClaimsJws(jwt); 

     subject = claims.getBody().getSubject(); 

     //OK, we can trust this JWT 

    } catch (SignatureException e) { 

     //don't trust the JWT! 
    } 

et printemps a une méthode JWTAccessTokenConverter.decode(), mais le javadoc manque, et il est protégé.

Répondre

4

Voici comment j'accède demandes JWT personnalisées au printemps Boot:

1) Obtenez printemps pour copier le contenu JWT en Authentication:

@Configuration 
@EnableResourceServer 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfig extends ResourceServerConfigurerAdapter{ 

    @Override 
    public void configure(ResourceServerSecurityConfigurer config) { 
     config.tokenServices(createTokenServices()); 
    } 

    @Bean 
    public DefaultTokenServices createTokenServices() { 
     DefaultTokenServices defaultTokenServices = new DefaultTokenServices(); 
     defaultTokenServices.setTokenStore(createTokenStore()); 
     return defaultTokenServices; 
    } 

    @Bean 
    public TokenStore createTokenStore() {    
     return new JwtTokenStore(createJwtAccessTokenConverter()); 
    } 

    @Bean 
    public JwtAccessTokenConverter createJwtAccessTokenConverter() { 
     JwtAccessTokenConverter converter = new JwtAccessTokenConverter();  
     converter.setAccessTokenConverter(new JwtConverter()); 
     return converter; 
    } 

    public static class JwtConverter extends DefaultAccessTokenConverter implements JwtAccessTokenConverterConfigurer { 

     @Override 
     public void configure(JwtAccessTokenConverter converter) { 
      converter.setAccessTokenConverter(this); 
     } 

     @Override 
     public OAuth2Authentication extractAuthentication(Map<String, ?> map) { 
      OAuth2Authentication auth = super.extractAuthentication(map); 
      auth.setDetails(map); //this will get spring to copy JWT content into Authentication 
      return auth; 
     } 
    } 
} 

2) contenu jeton d'accès partout dans votre code:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();   
Object details = authentication.getDetails();   
if (details instanceof OAuth2AuthenticationDetails){ 
    OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails)details; 

    Map<String, Object> decodedDetails = (Map<String, Object>)oAuth2AuthenticationDetails.getDecodedDetails(); 

    System.out.println("My custom claim value: " + decodedDetails.get("MyClaim")); 
}