en java Comment vérifier que la chaîne que j'ai donnée est un jeton JWT sans utiliser de signature?
J'utilise p> Ceci fonctionne bien mais je veux vérifier Ceci sans Je veux juste vérifier s'il s'agit d'un jeton JWT ou non. P> P> secret_key code>. p>
3 Réponses :
Vous pouvez décoder JWT de base64 et vérifier nécessairement le champ. Ou simplement vérifier le jeton, pourquoi vous voulez vérifier cela, ce n'est pas plus rapide que Simpy Vérifiez-le. P>
Essayez celui-ci. Ensuite, vous devez obtenir de l'organisme JWT cette partie qui est importante pour vous, par exemple. P> JSONObject body = JWTUtils.getPayload(token);
String username = body.getString("username");
Voici un exemple pour vérifier la structure du JWT. Il vous suffit d'ajouter les validations des données que le JWT doit transporter
boolean isJWT(String jwt) { String[] jwtSplitted = jwt.split("\\."); if (jwtSplitted.length != 3) // The JWT is composed of three parts return false; try { String jsonFirstPart = new String(Base64.getDecoder().decode(jwtSplitted[0])); JSONObject firstPart = new JSONObject(jsonFirstPart); // The first part of the JWT is a JSON if (!firstPart.has("alg")) // The first part has the attribute "alg" return false; String jsonSecondPart = new String(Base64.getDecoder().decode(jwtSplitted[1])); JSONObject secondPart = new JSONObject(jsonSecondPart); // The first part of the JWT is a JSON //Put the validations you think are necessary for the data the JWT should take to validate }catch (JSONException err){ return false; } return true; }
Cela a fonctionné pour moi