Skip to content

jet Mac

Prayag edited this page Aug 22, 2022 · 2 revisions
implementation 'com.auth0:java-jwt:4.0.0'

    public static void main(String[] args) {
        String secretKey = "WfuLMmTNtbzztLYk";

        try {
            //client
            Algorithm algorithm = Algorithm.HMAC256(secretKey);
            String clientToken = JWT.create()
                .withIssuer("auth0")
                .withHeader(Map.of("x-date", "2022-08-08"))
                .withPayload(Map.of("username", "Prayag", "sku", "12"))
                .sign(algorithm);
            System.out.println("request token: ");
            System.out.println(clientToken);

            ///server-side
            System.out.println("-- server --");
            try {
                Algorithm serverAlgorithm = Algorithm.HMAC256(secretKey);
                JWTVerifier serverVerifier = JWT.require(serverAlgorithm)
                    .withIssuer("auth0")
                    .build();
                DecodedJWT jwt = serverVerifier.verify(clientToken);
                System.out.println(toString(jwt.getHeader()));
                System.out.println(toString(jwt.getPayload()));
                System.out.println(jwt.getSignature());
                System.out.println(jwt.getToken());
            } catch (JWTVerificationException exception){
                System.out.println("Invalid request");
                exception.printStackTrace();
            }
        } catch (JWTCreationException exception){
            exception.printStackTrace();
        }
Clone this wiki locally