# Spring Boot JWT 认证示例项目 本示例项目中使用了 Spring Security 和 Spring Data JPA 框架。


提供 CWIKI.US 项目中使用的代码

所有的 Java 代码使用的是 JDK 11。 你可以通过单击下面连接后访问我们网站,并且访问我们提供的最新有关 Java 的开发资料。 * [概述](https://www.cwiki.us/pages/viewpage.action?pageId=37492282) * [社区和讨论](https://www.ossez.com/c/open-source/java/15) # 联系方式 请使用下面的联系方式和我们联系: | 联系方式名称 | 联系方式 | |--------|-----------------------------------------------| | 电子邮件 | [service@ossez.com](mailto:service@ossez.com) | | QQ 或微信 | 103899765 | | QQ 交流群 | 15186112 | | 社区论坛 | https://www.ossez.com/c/open-source/java/15 | # 公众平台 我们建议您通过社区论坛来和我们进行沟通,请关注我们公众平台上的账号 ## 微信公众号 ![](https://cdn.ossez.com/img/cwikius/cwikius-qr-wechat-search-w400.png) ## 头条号 我们也在头条号上创建了我们的公众号,请扫描下面的 QR 关注我们的头条号。 ![](https://cdn.ossez.com/img/cwikius/cwikus-qr-toutiao.png) ## 知乎 请关注我们的知乎:https://www.zhihu.com/people/huyuchengus ## 快速导航 在下面的表格中,我们列出了一些比较有用的 CWIKIUS 相关软件开发使用教程的导航,欢迎访问下面的链接获得更多的内容和参与讨论 | 网站名称 | URL | NOTE | |----------------|--------------------------------------------------------|----------------------------| | OSSEZ 社区 | [www.ossez.com](https://www.ossez.com/) | 开放社区,欢迎注册参与讨论 | | WIKI 维基 | [www.cwiki.us](https://www.cwiki.us/) | 使用 Confluence 部署的 WIKI 知识库 | | DOCS.OSSEZ.COM | [https://docs.ossez.com/#/](https://docs.ossez.com/#/) | 本手册的编译版本将会部署在这个链接上 | | CN 博客 | [http://www.cwikius.cn/](http://www.cwikius.cn/) | CWIKIUS.CN 一个有独立思考和温度的清新站 | ## User Registration, User Login and Authorization process. The diagram shows flow of how we implement User Registration, User Login and Authorization process. ![spring-boot-jwt-authentication-spring-security-flow](spring-boot-jwt-authentication-spring-security-flow.png) ## Spring Boot Server Architecture with Spring Security You can have an overview of our Spring Boot Server with the diagram below: ![spring-boot-jwt-authentication-spring-security-architecture](spring-boot-jwt-authentication-spring-security-architecture.png) For more detail, please visit: > [Secure Spring Boot App with Spring Security & JWT Authentication](https://bezkoder.com/spring-boot-jwt-authentication/) > [For MongoDB](https://bezkoder.com/spring-boot-jwt-auth-mongodb/) ## Refresh Token ![spring-boot-refresh-token-jwt-example-flow](spring-boot-refresh-token-jwt-example-flow.png) For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.com/spring-boot-refresh-token-jwt/) ## More Practice: > [Spring Boot File upload example with Multipart File](https://bezkoder.com/spring-boot-file-upload/) > [Exception handling: @RestControllerAdvice example in Spring Boot](https://bezkoder.com/spring-boot-restcontrolleradvice/) > [Spring Boot Repository Unit Test with @DataJpaTest](https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/) > [Spring Boot Pagination & Sorting example](https://www.bezkoder.com/spring-boot-pagination-sorting-example/) Associations: > [Spring Boot One To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-many/) > [Spring Boot Many To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-many-to-many/) > [JPA One To One example with Spring Boot](https://www.bezkoder.com/jpa-one-to-one/) Deployment: > [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/) > [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/) ## Fullstack Authentication > [Spring Boot + Vue.js JWT Authentication](https://bezkoder.com/spring-boot-vue-js-authentication-jwt-spring-security/) > [Spring Boot + Angular 8 JWT Authentication](https://bezkoder.com/angular-spring-boot-jwt-auth/) > [Spring Boot + Angular 10 JWT Authentication](https://bezkoder.com/angular-10-spring-boot-jwt-auth/) > [Spring Boot + Angular 11 JWT Authentication](https://bezkoder.com/angular-11-spring-boot-jwt-auth/) > [Spring Boot + Angular 12 JWT Authentication](https://www.bezkoder.com/angular-12-spring-boot-jwt-auth/) > [Spring Boot + Angular 13 JWT Authentication](https://www.bezkoder.com/angular-13-spring-boot-jwt-auth/) > [Spring Boot + Angular 14 JWT Authentication](https://www.bezkoder.com/angular-14-spring-boot-jwt-auth/) > [Spring Boot + React JWT Authentication](https://bezkoder.com/spring-boot-react-jwt-auth/) ## Fullstack CRUD App > [Vue.js + Spring Boot + H2 Embedded database example](https://www.bezkoder.com/spring-boot-vue-js-crud-example/) > [Vue.js + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-vue-js-mysql/) > [Vue.js + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-vue-js-postgresql/) > [Angular 8 + Spring Boot + Embedded database example](https://www.bezkoder.com/angular-spring-boot-crud/) > [Angular 8 + Spring Boot + MySQL example](https://bezkoder.com/angular-spring-boot-crud/) > [Angular 8 + Spring Boot + PostgreSQL example](https://bezkoder.com/angular-spring-boot-postgresql/) > [Angular 10 + Spring Boot + MySQL example](https://bezkoder.com/angular-10-spring-boot-crud/) > [Angular 10 + Spring Boot + PostgreSQL example](https://bezkoder.com/angular-10-spring-boot-postgresql/) > [Angular 11 + Spring Boot + MySQL example](https://bezkoder.com/angular-11-spring-boot-crud/) > [Angular 11 + Spring Boot + PostgreSQL example](https://bezkoder.com/angular-11-spring-boot-postgresql/) > [Angular 12 + Spring Boot + Embedded database example](https://www.bezkoder.com/angular-12-spring-boot-crud/) > [Angular 12 + Spring Boot + MySQL example](https://www.bezkoder.com/angular-12-spring-boot-mysql/) > [Angular 12 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/angular-12-spring-boot-postgresql/) > [Angular 13 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-13-crud/) > [Angular 13 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-13-mysql/) > [Angular 13 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-13-postgresql/) > [Angular 14 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-14-crud/) > [Angular 14 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-14-mysql/) > [Angular 14 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-14-postgresql/) > [React + Spring Boot + MySQL example](https://bezkoder.com/react-spring-boot-crud/) > [React + Spring Boot + PostgreSQL example](https://bezkoder.com/spring-boot-react-postgresql/) > [React + Spring Boot + MongoDB example](https://bezkoder.com/react-spring-boot-mongodb/) Run both Back-end & Front-end in one place: > [Integrate Angular with Spring Boot Rest API](https://bezkoder.com/integrate-angular-spring-boot/) > [Integrate React.js with Spring Boot Rest API](https://bezkoder.com/integrate-reactjs-spring-boot/) > [Integrate Vue.js with Spring Boot Rest API](https://bezkoder.com/integrate-vue-spring-boot/) ## Dependency – If you want to use PostgreSQL: ```xml org.postgresql postgresql runtime ``` – or MySQL: ```xml mysql mysql-connector-java runtime ``` ## Configure Spring Datasource, JPA, App properties Open `src/main/resources/application.properties` - For PostgreSQL: ``` spring.datasource.url= jdbc:postgresql://localhost:5432/testdb spring.datasource.username= postgres spring.datasource.password= 123 spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect # Hibernate ddl auto (create, create-drop, validate, update) spring.jpa.hibernate.ddl-auto= update # App Properties bezkoder.app.jwtSecret= bezKoderSecretKey bezkoder.app.jwtExpirationMs= 86400000 ``` - For MySQL ``` spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false spring.datasource.username= root spring.datasource.password= 123456 spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto= update # App Properties bezkoder.app.jwtSecret= bezKoderSecretKey bezkoder.app.jwtExpirationMs= 86400000 ``` ## Run Spring Boot application ``` mvn spring-boot:run ``` ## Run following SQL insert statements ``` INSERT INTO roles(name) VALUES('ROLE_USER'); INSERT INTO roles(name) VALUES('ROLE_MODERATOR'); INSERT INTO roles(name) VALUES('ROLE_ADMIN'); ```