All Projects → cassiomolin → jersey-jwt-springsecurity

cassiomolin / jersey-jwt-springsecurity

Licence: MIT license
Example of REST API with JWT authentication using Spring Boot, Spring Security, Jersey and Jackson.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to jersey-jwt-springsecurity

Jersey Jwt
Example of REST API with JWT authentication using Jersey, Jackson, Undertow, Weld, Hibernate and Arquillian.
Stars: ✭ 131 (+197.73%)
Mutual labels:  jackson, postman, jwt-authentication
Webfluxtemplate
Spring Webflux template application with working Spring Security, Web-sockets, Rest, Web MVC, and Authentication with JWT.
Stars: ✭ 107 (+143.18%)
Mutual labels:  spring-security, jwt-authentication
Simplemall
基于SpringCloud的微服务架构实战案例项目,以一个简单的购物流程为示例,融合spring cloud 相关组件,如spring-cloud-netflix、swagger等
Stars: ✭ 687 (+1461.36%)
Mutual labels:  spring-security, jwt-authentication
Spring Webflux Security Jwt
A JWT authorization and authentication implementation with Spring Reactive Webflux, Spring Boot 2 and Spring Security 5
Stars: ✭ 190 (+331.82%)
Mutual labels:  spring-security, jwt-authentication
Zuul Auth Example
Use Zuul and Spring Security for a global authentication.
Stars: ✭ 271 (+515.91%)
Mutual labels:  spring-security, jwt-authentication
Spring Boot Spring Security Jwt Authentication
Spring Boot + Security: Token Based Authentication example with JWT, Authorization, Spring Data & MySQL
Stars: ✭ 292 (+563.64%)
Mutual labels:  spring-security, jwt-authentication
zainabed-spring-security-jwt
Authentication & Authorization module for standalone Spring Boot app or Spring Cloud applications
Stars: ✭ 24 (-45.45%)
Mutual labels:  spring-security, jwt-authentication
TASK-Management-System
Spring Boot and Angular 7 web application for task management .
Stars: ✭ 34 (-22.73%)
Mutual labels:  spring-security, jwt-authentication
angular-11-spring-boot-jwt-authentication
Angular 11 Spring Boot JWT Authentication example with Authorization | User Registration & Login
Stars: ✭ 62 (+40.91%)
Mutual labels:  spring-security, jwt-authentication
spring-boot-jwt-auth
🔑 Sample Spring boot application secured using JWT auth in custom header(X-Auth-Token).
Stars: ✭ 57 (+29.55%)
Mutual labels:  spring-security, jwt-tokens
Jwt Spring Security Demo
This is a demo for using JWT (JSON Web Token) with Spring Security and Spring Boot. I completely rewrote my first version. Now this solution is based on the code base from the JHipster Project. I tried to extract the minimal configuration and classes that are needed for JWT-Authentication and did some changes.
Stars: ✭ 2,843 (+6361.36%)
Mutual labels:  spring-security, jwt-authentication
spring-boot-login-example
Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example
Stars: ✭ 50 (+13.64%)
Mutual labels:  spring-security, jwt-authentication
spring-security-jwt-csrf
A demonstration of stateless JWT authentication with Spring Security, Spring Boot and Vue js
Stars: ✭ 62 (+40.91%)
Mutual labels:  spring-security, jwt-authentication
Springboot Jwt Starter
A Spring Boot JWT starter kit for stateless and token-based authentication apps.
Stars: ✭ 538 (+1122.73%)
Mutual labels:  spring-security, jwt-authentication
spring-boot-security-postgresql
Spring Boot, Spring Security, PostgreSQL: JWT Authentication & Authorization example
Stars: ✭ 65 (+47.73%)
Mutual labels:  spring-security, jwt-authentication
Clean Architecture Delivery Example
A example of clean architecture in Java 8 and Spring Boot 2.0
Stars: ✭ 140 (+218.18%)
Mutual labels:  spring-security, jwt-authentication
pern-stack-auth
📋 Repair. PERN stack todo app with jwt user authentication
Stars: ✭ 17 (-61.36%)
Mutual labels:  postman, jwt-authentication
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 (+1270.45%)
Mutual labels:  postman, jwt-authentication
Jwt Spring Security Jpa
Backend MVP showcasing JWT (Json Web Token) authentication with multiple login, timeout / refresh / logout (with in memory invalidation) using Spring Security & MySQL JPA.
Stars: ✭ 202 (+359.09%)
Mutual labels:  spring-security, jwt-authentication
spring-boot-refresh-token-jwt
Spring Boot Refresh Token using JWT example - Expire and Renew JWT Token
Stars: ✭ 156 (+254.55%)
Mutual labels:  spring-security, jwt-authentication

REST API with JWT authentication using Jersey and Spring Security

Build Status MIT Licensed

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

  • Spring Boot: Framework for creating standalone Java applications.
  • Spring Security: Framework for authentication and authorization.
  • Spring Data JPA: Framework for implementing JPA repositories.
  • H2: In memory relational database.
  • Jersey: JAX-RS reference implementation for creating RESTful web services in Java.
  • Jackson: JSON parser for Java.
  • JJWT: Library for creating and parsing JSON Web Tokens (JWTs) in Java.

Note: For a CDI and JAX-RS approach without Spring (Boot, Data and Security), have a look at the jersey-jwt 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-springsecurity-1.0.jar.
  7. Execute the JAR: java -jar jersey-jwt-springsecurity-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

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].