Giter VIP home page Giter VIP logo

jwt4j's Introduction

jwt4j

Java JWT Authentication

This library includes everything you need to both create and verify JWTs in Java!

It assumes that you have already figured out how to get a X509 Signing Certificate and it's Private Key loaded into your application.

How to use

Creating a JwtVerifier

You can use JwtVerifiers to verify that a JWT has a certain signing pattern, as well as whether it is valid or not. A Jwt's validity could be based on things like if it is being used before it's issue date, or if it is being used after it's expiration date.

// Create a JWT Verfier with a Signing Pattern. This pattern will be run against all
String signingPattern = "^.*\\bCN=test-cert\\.swiftengineer\\.com\\b(?:,.*|\\s*)$";
JwtVerifier jwtVerifier = new JwtVerifier(signingPattern);

Creating a JwtIssuer

You can use JwtIssuers to sign and issue JWTs.

// Create a JWT Issuer
JwtIssuer jwtIssuer = new JwtIssuer(privateKey, signingCertificate);

Creating a JWT

// create a jwt issuer
JwtIssuer jwtIssuer = new JwtIssuer(privateKey, signingCertificate);

// create a valid set of JWT claims that expires 30 minutes from now
JwtClaims claims = JwtClaims.JwtClaimsBuilder.newInstance()
        .addIssuedAt(Date.from(Instant.now()) // issued now
        .addExpirationTime(Date.from(Instant.now().plusSeconds(30L * 60))) // expires 30 minutes from now
        .build();

// issue the Jwt with some claims
String serializedJwt = jwtIssuer.issueToken(claims);

// attach the serializedJwt to an authentication header or pass it from Service to Service.

jwt4j's People

Contributors

swiftengineer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.