Wednesday 23 August 2017

Validate JWT token and Secret Key

What is JSON Web Token?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA.

When should you use JSON Web Tokens?

Here are some scenarios where JSON Web Tokens are useful:
  • Authentication: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.
  • Information Exchange: JSON Web Tokens are a good way of securely transmitting information between parties. Because JWTs can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.
Click here to learn more about JWT.

Sample Java code to validate JWT token and secret key.

Add below maven dependency to your pom.xml

<dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.6.0</version>
</dependency>


Create GwtTokenValidate.java and add below code.

package com.kishore.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

import java.util.Base64;

public class GwtTokenValidate {
    public static void main(String[] args) {
        String gwtToken = "<<sample gwt token>>";
        String secretCode = "<<secret code>>";
        JwtUser ju = JwtService.getUser(secretCode, gwtToken);
        System.out.println(ju);
    }

}

class JwtService {

    protected static String generateEncodedSecret(String plainSecret) {
        return Base64.getEncoder().encodeToString(plainSecret.getBytes());
    }

    public static JwtUser getUser(String encodedSecret, String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(generateEncodedSecret(encodedSecret))
                .parseClaimsJws(token).getBody();
        String userName = claims.getSubject();
        String role = (String) claims.get("sub_email");
        JwtUser securityUser = new JwtUser();
        securityUser.setUserName(userName);
        securityUser.setRole(role);
        return securityUser;
    }

}

class JwtUser {
    private String userName;
    private String email;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setRole(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "JwtUser [userName=" + userName + ", email=" + email + "]";
    }

}


Output:
JwtUser [userName=900d55ba-55ff-4e4f-baf8-1a8c53204d24, email=kishore.polsani@gmail.com]
--

No comments :

Post a Comment