public static String getUsername ( String token ) { String accessTokenPayload = token . substring ( token . indexOf ( "." ) + 1 , token . lastIndexOf ( "." ) ) ; byte [ ] decoded = Base64 . getMimeDecoder ( ) . decode ( accessTokenPayload ) ; String output = new String ( decoded ) ; Map < String , Object > myMap = new HashMap < String , Object > ( ) ; String result = "unknown" ; ObjectMapper objectMapper = new ObjectMapper ( ) ; try { myMap = objectMapper . readValue ( output , HashMap . class ) ; if ( myMap . get ( "sub" ) instanceof String ) { result = ( String ) myMap . get ( "sub" ) ; } } catch ( IOException e ) { e . printStackTrace ( ) ; } return result ; }
Username from a given JWT Token