JWTUtils { 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; }  static String generateAccessToken(String username); static String generateRefreshToken(String username); static Tokens generateTokens(String username); static String validateTokenAndGetSubj(String jwt, TokenType tokenType); static int getTokenExpiry(String token); static String getUsername(String token);  }
@Test public void testJWTUsername() throws OAuthException { String accessToken = "eyJhbGciOiJIUzI1NiJf.eyJpc3MiOiJhcmd1cy1hcGkiLCJpYXQiOjE1MzA3NTQzNzcsImV4cCI6MTUzMDc1Nzk3Nywic3ViIjoiZ2F1cmF2Lmt1bWFyIiwidHlwZSI6IkFDQ0VTUyJ9.Vo0DaXePFZ3rbHVyXoNy_oOJ31bNeDpkzC"; Assert.assertEquals("gaurav.kumar", JWTUtils.getUsername(accessToken)); }