返回排行榜

szerhusenBC/jwt-spring-security-demo

Java

A demo for using JWT (Json Web Token) with Spring Security and Spring Boot 2

jwtjwt-authenticationspring-securityspring-bootjavademoauthenticationspringframework
Star 增长趋势
Star
3.1k
Forks
1.3k
周增长
Issues
7
1k2k3k
2023年1月2024年3月2025年5月2026年7月
制品库Mavengit clone https://github.com/szerhusenBC/jwt-spring-security-demo.git
README

JWT Spring Security Demo

Screenshot from running application

About

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.

Build Status

Requirements

This demo is build with with Maven 3.6.x and Java 11.

Usage

Just start the application with the Spring Boot maven plugin (mvn spring-boot:run). The application is running at http://localhost:8080.

You can use the H2-Console for exploring the database under http://localhost:8080/h2-console:

Screenshot from h2-console login

Backend

There are three user accounts present to demonstrate the different levels of access to the endpoints in the API and the different authorization exceptions:

Admin - admin:admin
User - user:password
Disabled - disabled:password (this user is deactivated)

There are four endpoints that are reasonable for the demo:

/api/authenticate - authentication endpoint with unrestricted access
/api/user - returns detail information for an authenticated user (a valid JWT token must be present in the request header)
/api/persons - an example endpoint that is restricted to authorized users with the authority 'ROLE_USER' (a valid JWT token must be present in the request header)
/api/hiddenmessage - an example endpoint that is restricted to authorized users with the authority 'ROLE_ADMIN' (a valid JWT token must be present in the request header)

Frontend

I've written a small Javascript client and put some comments in the code that hopefully makes this demo understandable. You can find it at /src/main/resources/static/js/client.js.

Generating password hashes for new users

I'm using bcrypt to encode passwords. Your can generate your hashes with this simple tool: Bcrypt Generator

Using another database

Actually this demo is using an embedded H2 database that is automatically configured by Spring Boot. If you want to connect to another database you have to specify the connection in the application.yml in the resource directory. Here is an example for a MySQL DB:

spring:
  jpa:
    hibernate:
      # possible values: validate | update | create | create-drop
      ddl-auto: create-drop
  datasource:
    url: jdbc:mysql://localhost/myDatabase
    username: myUser
    password: myPassword
    driver-class-name: com.mysql.jdbc.Driver

Hint: For other databases like MySQL sequences don't work for ID generation. So you have to change the GenerationType in the entity beans to 'AUTO' or 'IDENTITY'.

You can find a reference of all application properties here.

Using Flyway

https://github.com/szerhusenBC/jwt-spring-security-demo/issues/81

Docker

This project has a docker image. You can find it at https://hub.docker.com/r/hubae/jwt-spring-security-demo/.

Questions

If you have project related questions please take a look at the past questions or create a new ticket with your question.

If you have questions that are not directly related to this project (e.g. common questions to the Spring Framework or Spring Security etc.) please search the web or look at Stackoverflow.

Sorry for that but I'm very busy right now and don't have much time.

Interesting projects

Author

Stephan Zerhusen

The code is released under the MIT license.


Please feel free to send me some feedback or questions!

相关仓库
fastapi/full-stack-fastapi-template

Full stack, modern web application template. Using FastAPI, React, SQLModel, PostgreSQL, Docker, GitHub Actions, automatic HTTPS and more.

TypeScriptnpmMIT Licensepythonjson
44.4k8.8k
nextauthjs/next-auth

Authentication for the Web.

TypeScriptnpmISC Licensenodejsnextjs
authjs.dev
28.3k4k
flipped-aurora/gin-vue-admin

🚀Vite+Vue3+Gin拥有AI辅助的基础开发平台,企业级业务AI+开发解决方案,内置mcp辅助服务,内置skills管理,支持TS和JS混用。它集成了JWT鉴权、权限管理、动态路由、显隐可控组件、分页封装、多点登录拦截、资源权限、上传下载、代码生成器、表单生成器和可配置的导入导出等开发必备功能。

GoGo ModulesOthergin-vue-admingin
demo.gin-vue-admin.com
24.9k7.1k
shieldfy/API-Security-Checklist

Checklist of the most important security countermeasures when designing, testing, and releasing your API

MIT Licenseapisecurity
23.3k2.7k
elunez/eladmin

eladmin jpa 版本:项目基于 Spring Boot 2.7.18、 Jpa、 Spring Security、Redis、Vue的前后端分离的后台管理系统,项目采用分模块开发方式, 权限控制采用 RBAC,支持数据字典与数据权限管理,支持一键生成前后端代码,支持动态路由

JavaMavenApache License 2.0spring-bootspring-security
eladmin.vip/demo
21.9k7.4k
logto-io/logto

🧑‍🚀 Authentication and authorization infrastructure for SaaS and AI apps, built on OIDC and OAuth 2.1 with multi-tenancy, SSO, and RBAC.

TypeScriptnpmMozilla Public License 2.0authenticationauthorization
logto.io
14.2k1k
go-admin-team/go-admin

基于Gin + Vue + Element UI & Arco Design & Ant Design 的前后端分离权限管理系统脚手架(包含了:多租户的支持,基础用户管理功能,jwt鉴权,代码生成器,RBAC资源控制,表单构建,定时任务等)3分钟构建自己的中后台项目;项目文档》:https://www.go-admin.pro V2 Demo: https://vue2.go-admin.dev V3 Demo: https://vue3.go-admin.dev Antd PRO:https://antd.go-admin.pro

GoGo ModulesMIT Licensegincasbin
go-admin.pro
12.7k2.6k
tymondesigns/jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen

PHPPackagistMIT Licensejwtlaravel
jwt-auth.com
11.5k1.5k
jwtk/jjwt

Java JWT: JSON Web Token for Java and Android

JavaMavenApache License 2.0jwtjava
11.1k1.4k
golang-jwt/jwt

Go implementation of JSON Web Tokens (JWT).

GoGo ModulesMIT Licensegogolang
golang-jwt.github.io/jwt/
9.2k440
teamhanko/hanko

Modern authentication, on your terms. Open source alternative to Auth0, Clerk, WorkOS, Stytch.

GoGo ModulesOtherpasskeyswebauthn
hanko.io
9k1k
panva/jose

JWA, JWS, JWE, JWT, JWK, JWKS for Node.js, Browser, Cloudflare Workers, Deno, Bun, and other Web-interoperable runtimes

TypeScriptnpmMIT Licensejosejwa
7.7k375