I tried using JWT in Java

Overview

Make a note of the introduction of JWT using the Java library.

What is JWT

--The abbreviation for Web Token is "URL Safe Token including JSON that can be signed". --JWT is composed of Header, Payload, and Signature, and Header and Payload are Base64-encoded information of Json, so it seems better not to put anything that you do not want to disclose to the outside, such as user information and password. --Since it is signed, you can check it at the time of verification even if you tamper with the Json part.

Token example

――The whole token looks like this

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.izVguZPRsBQ5Rqw6dhMvcIwy8_9lQnrO3vpxGwPCuzs

--Header part

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9

--When you decode the Header

{"typ":"JWT","alg":"HS256"}

--Payload part

eyJpc3MiOiJhdXRoMCJ9

--When you decode Payload

{"iss":"auth0"}

I actually wrote it in Java

--Environment - java8 --java-jwt (java library that handles jwt that is also published on jwt.io) --This time, I made a JWT with Issuer and Expire Time and confirmed the operation. --The algorithm uses HS256 --Token generation

try {
    Date expireTime = new Date();
    expireTime.setTime(expireTime.getTime() + 600000l);

    Algorithm algorithm = Algorithm.HMAC256("secret");
    String token = JWT.create()
            .withIssuer("auth0")
            .withExpiresAt(expireTime)
            .sign(algorithm);
} catch (JWTCreationException exception){
    //Invalid Signing configuration / Couldn't convert Claims.
}

--Token verification

String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.izVguZPRsBQ5Rqw6dhMvcIwy8_9lQnrO3vpxGwPCuzs";
try {
    Algorithm algorithm = Algorithm.HMAC256("secret");
    JWTVerifier verifier = JWT.require(algorithm)
            .withIssuer("auth0")
            .build(); //Reusable verifier instance
    DecodedJWT jwt = verifier.verify(token);
} catch (JWTVerificationException exception){
    //Invalid signature/claims
}

Feeling used

--If you borrow the code written on the official page of java-jwt, you can easily check the operation. --When the Json part (Header and Payload part) is tampered with, JWTVerificationException is thrown and checked.

Recommended Posts

I tried using JWT in Java
I tried using Elasticsearch API in Java
I tried using Java REPL
I tried metaprogramming in Java
I tried using Google Cloud Vision API in Java
I tried using Java8 Stream API
I tried using an extended for statement in Java
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
I tried using Dapr in Java to facilitate microservice development
I tried the new era in Java
I tried using OpenCV with Java + Tomcat
I tried using Gson
I tried using TestNG
I tried using Galasa
[For beginners] I tried using DBUnit in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
I tried to implement deep learning in Java
I tried to output multiplication table in Java
I tried to create Alexa skill in Java
I tried to make a talk application in Java using AI "A3RT"
Try using RocksDB in Java
I made roulette in Java.
I tried using azure cloud-init
I tried Drools (Java, InputStream)
I tried using Apache Wicket
I tried Mastodon's Toot and Streaming API in Java
I tried to implement Firebase push notification in Java
I tried to operate SQS using AWS Java SDK
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried to make a login function in Java
I tried using Log4j2 on a Java EE server
I tried passing Java Silver in 2 weeks without knowing Java
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I tried scraping a stock chart using Java (Jsoup)
I tried to find out what changed in Java 9
I tried using anakia + Jing now
I sent an email in Java
I created a PDF in Java.
Object-oriented child !? I tried Deep Learning in Java (trial edition)
I tried using JOOQ with Gradle
Encrypt using RSA cryptography in Java
I tried to interact with Java
I tried UDP communication with Java
I wrote Goldbach's theorem in java
I tried putting Domino11 in CentOS7
I tried the Java framework "Quarkus"
I tried to convert a string to a LocalDate type in Java
I made an annotation in Java.
HTTPS connection using tls1.2 in Java 6
I tried to summarize Java learning (1)
I tried to make a client of RESAS-API in Java
[Android] I tried using Coordinator Layout.
I tried using Pari gp container
I tried using WebAssembly Stadio (2018/4/17 version)
I tried to summarize Java 8 now
I tried using the CameraX library with Android Java Fragment
I tried to display the calendar on the Eclipse console using Java.
I tried a calendar problem in Ruby
I tried using Realm with Swift UI