All Projects → cassiomolin → Jersey Jwt

cassiomolin / Jersey Jwt

Licence: mit
Example of REST API with JWT authentication using Jersey, Jackson, Undertow, Weld, Hibernate and Arquillian.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Jersey Jwt

Spring Petclinic Rest
REST version of the Spring Petclinic sample application
Stars: ✭ 257 (+96.18%)
Mutual labels:  rest-api, rest, hibernate, jackson
Spring Boot Postgresql Jpa Hibernate Rest Api Demo
Building RESTful APIs with Spring Boot, PostgreSQL, JPA and Hibernate
Stars: ✭ 209 (+59.54%)
Mutual labels:  rest-api, rest, hibernate, jpa
Node Express Mongoose Passport Jwt Rest Api Auth
Node, express, mongoose, passport and JWT REST API authentication example
Stars: ✭ 146 (+11.45%)
Mutual labels:  rest-api, rest, jwt, jwt-authentication
Ee7 Jaxrs Sample
Building RESTful APIs with Java EE 7 and JAXRS
Stars: ✭ 15 (-88.55%)
Mutual labels:  rest, hibernate, jpa, jwt
Node Express Mongodb Jwt Rest Api Skeleton
This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API). Demo of frontend in VueJS here: https://github.com/davellanedam/vue-skeleton-mvp
Stars: ✭ 603 (+360.31%)
Mutual labels:  rest, postman, jwt, jwt-authentication
spring-boot-jpa
A Spring Boot microservices reference application using Spring Data JPA
Stars: ✭ 25 (-80.92%)
Mutual labels:  jpa, jackson, hibernate
Spring Boot Mysql Rest Api Tutorial
Building a Restful CRUD API using Spring Boot, Mysql, JPA and Hibernate
Stars: ✭ 279 (+112.98%)
Mutual labels:  rest-api, hibernate, jpa
Angularjs Springmvc Sample Boot
A RESTful sample using Spring Boot, Spring MVC, Spring Data and Angular/Bootstrap.
Stars: ✭ 309 (+135.88%)
Mutual labels:  rest, hibernate, jpa
Spring Boot 2 Oauth2 Authorization Jwt
Spring Boot 2 OAuth2 JWT Authorization server implementation with Database for Users and Clients (JPA, Hibernate, MySQL)
Stars: ✭ 115 (-12.21%)
Mutual labels:  hibernate, jpa, jwt
Proteus
Lean, mean, and incredibly fast JVM framework for web and microservice development.
Stars: ✭ 178 (+35.88%)
Mutual labels:  rest-api, rest, jax-rs
Guns
Guns基于SpringBoot 2,致力于做更简洁的后台管理系统,完美整合springmvc + shiro + mybatis-plus + beetl!Guns项目代码简洁,注释丰富,上手容易,同时Guns包含许多基础模块(用户管理,角色管理,部门管理,字典管理等10个模块),可以直接作为一个后台管理系统的脚手架!
Stars: ✭ 3,327 (+2439.69%)
Mutual labels:  rest-api, rest, jwt
Cerberus
A demonstration of a completely stateless and RESTful token-based authorization system using JSON Web Tokens (JWT) and Spring Security.
Stars: ✭ 482 (+267.94%)
Mutual labels:  rest-api, rest, jwt
jersey-jwt-springsecurity
Example of REST API with JWT authentication using Spring Boot, Spring Security, Jersey and Jackson.
Stars: ✭ 44 (-66.41%)
Mutual labels:  jackson, postman, jwt-authentication
Crnk Framework
JSON API library for Java
Stars: ✭ 234 (+78.63%)
Mutual labels:  rest-api, jax-rs, jpa
Rest Api Nodejs Mongodb
A boilerplate for REST API Development with Node.js, Express, and MongoDB
Stars: ✭ 672 (+412.98%)
Mutual labels:  rest-api, rest, jwt-authentication
Go Book Store Api
Go Sample project to understand Mysql CRUD operation with best practises Includes logging, JWT, Swagger and Transactions
Stars: ✭ 18 (-86.26%)
Mutual labels:  rest-api, jwt, jwt-authentication
Jeddict
Jakarta EE 8 (Java EE) & MicroProfile 3.2 application generator and modeler
Stars: ✭ 358 (+173.28%)
Mutual labels:  jax-rs, hibernate, jpa
Hikaku
A library that tests if the implementation of a REST-API meets its specification.
Stars: ✭ 154 (+17.56%)
Mutual labels:  rest-api, rest, jax-rs
Kotlin Spring Boot Jpa Rest Api Demo
Build a Restful API with Kotlin, Spring Boot, Mysql, Jpa and Hibernate
Stars: ✭ 67 (-48.85%)
Mutual labels:  rest-api, hibernate, jpa
Dropwizard
A damn simple library for building production-ready RESTful web services.
Stars: ✭ 8,078 (+6066.41%)
Mutual labels:  rest, jax-rs, hibernate

REST API with JWT authentication using Jersey and CDI

Build Status MIT Licensed

This example application demonstrates how to perform token-based authentication using:

  • Jersey: JAX-RS reference implementation for creating RESTful web services in Java.
  • Jackson: JSON parser for Java.
  • Undertow: Servlet container.
  • Weld: CDI reference implementation.
  • Hibernate ORM: Persistence framework for relational databases (JPA implementation).
  • Arquillian: Testing framework.
  • JJWT: Library for creating and parsing JSON Web Tokens (JWTs) in Java.
  • jBCrypt: Implementation for the bcrypt password hashing function in Java.
  • H2: In memory relational database.

This example is inspired in my Stack Overflow best answer about token-based authentication in Jersey.

Note: For an implementation using Spring Security, have a look at the jersey-jwt-springsecurity project.

How token-based authentication works?

In a token-based authentication, the client exchanges hard credentials (such as username and password) for a piece of data called token. Instead of sending the hard credentials in every request, the client will send the token to the server to perform authentication and authorisation.

In a few words, an authentication scheme based on tokens follow these steps:

  1. The client sends their credentials (username and password) to the server.
  2. The server authenticates the credentials and issues a token.
  3. The server can store the previously generated token in some storage along with the user identifier.
  4. The server sends the generated token in the response.
  5. In each request, the client sends the token to the server.
  6. The server, in each request, extracts the token from the incoming request. With the token, the server looks up the user details to perform authentication and authorisation.
    1. If the token is valid, the server accepts the request.
    2. If the token is invalid, the server refuses the request.
  7. The server can provide an endpoint to refresh tokens.

What tokens can be like?

A token can be opaque which reveals no details other than the value itself (like a random string) or can be self-contained (like JWT, which is used in this example).

JWT stands for JSON Web Token. It's a standard method for representing claims securely between two parties, defined in the RFC 7519. JWT is a self-contained token and enables you to store a user identifier, an expiration date and whatever you want (but don't store passwords) in a payload, which is a JSON encoded as Base64. The payload can be read by the client and the integrity of the token can be easily checked by verifying its signature on the server.

To find some great resources to work with JWT, have a look at http://jwt.io.

JWT allows you to perform stateless authentication, that is, you won't need to persist JWT tokens if you don't need to track them. Although, by persisting the tokens, you will have the possibility of invalidating and revoking the access of them. To keep the track of JWT tokens, instead of persisting the whole token, you could persist the token identifier (the jti claim) and some metadata (the user you issued the token for, the expiration date, etc) if you need.

Your application can provide some functionality to revoke the tokens, but always consider revoking the tokens when the users change their password. When persisting tokens, consider removing the old ones in order to prevent your database from growing indefinitely.

Building and running this application

To build and run this application, follow these steps:

  1. Open a command line window or terminal.
  2. Navigate to the root directory of the project, where the pom.xml resides.
  3. Compile the project: mvn clean compile.
  4. Package the application: mvn package.
  5. Change into the target directory: cd target
  6. You should see a file with the following or a similar name: jersey-jwt-1.0.jar.
  7. Execute the JAR: java -jar jersey-jwt-1.0.jar.
  8. The application should be available at http://localhost:8080/api.

When the application starts up, the database will be populated with the following users:

ID Username Password Active Roles
1 admin password true ADMIN, USER
2 user password true USER
3 disabled password false USER

Quick words on Undertow and uber-jars

This application is packed as an uber-jar, making it easy to run, so you don't need to be bothered by installing a servlet container such as Tomcat and then deploy the application on it. Just execute java -jar <jar-file> and the application will be up and running.

This application uses Undertow, a lighweight Servlet container designed to be fully embeddable. It's used as the default web server in the WildFly Application Server.

The uber-jar is created with the Apache Maven Shade Plugin, that provides the capability to create an executable jar including its dependencies.

Application overview

Find below a quick description of the most relevant classes of this application:

REST API overview

See the curl scripts below with the REST API supported operations:

Exchange hard credentials for an authentication token

Valid credentials must be sent in the request payload to be exchanged for a token.

curl -X POST \
  'http://localhost:8080/api/auth' \
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "username": "<username>",
  "password": "<password>"
}'

Get a public greeting

No authentication is required to perform this operation.

curl -X GET \
  'http://localhost:8080/api/greetings/public' \
  -H 'Accept: text/plain'

Get a greeting for the user

Authentication and USER role are required to perform this operation.

curl -X GET \
  'http://localhost:8080/api/greetings/protected' \
  -H 'Accept: text/plain' \
  -H 'Authorization: Bearer <authentication-token>'

Get all users

Authentication and ADMIN role are required to perform this operation.

curl -X GET \
  'http://localhost:8080/api/users' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer <authentication-token>'

Get a user by id

Authentication and ADMIN role are required to perform this operation.

curl -X GET \
  'http://localhost:8080/api/users/<user-id>' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer <authentication-token>'

Get the current user

No authentication is required to perform this operation. However, if the request is performed with a valid token, the server will return details for the current user.

curl -X GET \
  'http://localhost:8080/api/users/me' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer <authentication-token>'

Targeting the REST API with Postman

Alternatively to curl, you can use Postman to target the REST API. The Postman collection files are available in the src/main/postman directory.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].