All Projects → Snailclimb → Spring Security Jwt Guide

Snailclimb / Spring Security Jwt Guide

从零入门 !Spring Security With JWT(含权限验证)后端部分代码。

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Spring Security Jwt Guide

Eladmin
项目基于 Spring Boot 2.1.0 、 Jpa、 Spring Security、redis、Vue的前后端分离的后台管理系统,项目采用分模块开发方式, 权限控制采用 RBAC,支持数据字典与数据权限管理,支持一键生成前后端代码,支持动态路由
Stars: ✭ 16,943 (+1958.69%)
Mutual labels:  jpa, spring-security
Springboot Registration Login Theperfectexample
Login & Signup tutorial for every website ,mixes a lot of microservices together with the latest spring framework api in combined with full security
Stars: ✭ 89 (-89.19%)
Mutual labels:  jpa, spring-security
spring-boot-elk
An sample todo app demonstrating centralised logging using ELK stack
Stars: ✭ 20 (-97.57%)
Mutual labels:  jpa, spring-security
Sample Boot Hibernate
Spring Boot + JPA ( Hibernate ) + Java8 [ DDD Sample ]
Stars: ✭ 97 (-88.21%)
Mutual labels:  jpa, spring-security
springboot-rest-api-angularjs-https
REST API https with Spring Boot and Angular JS. Use MySQL, Hibernate and Spring Security.
Stars: ✭ 38 (-95.38%)
Mutual labels:  jpa, spring-security
Spring-Boot-2
Spring Boot 2.x examples
Stars: ✭ 33 (-95.99%)
Mutual labels:  jpa, spring-security
Xboot
基于Spring Boot 2.x的一站式前后端分离快速开发平台XBoot 微信小程序+Uniapp 前端:Vue+iView Admin 后端:Spring Boot 2.x/Spring Security/JWT/JPA+Mybatis-Plus/Redis/Elasticsearch/Activiti 分布式限流/同步锁/验证码/SnowFlake雪花算法ID 动态权限 数据权限 工作流 代码生成 定时任务 社交账号 短信登录 单点登录 OAuth2开放平台 客服机器人 数据大屏 暗黑模式
Stars: ✭ 3,432 (+317.01%)
Mutual labels:  jpa, spring-security
Springboot Jwt Starter
A Spring Boot JWT starter kit for stateless and token-based authentication apps.
Stars: ✭ 538 (-34.63%)
Mutual labels:  spring-security
Spring Boot React Oauth2 Social Login Demo
Spring Boot React OAuth2 Social Login with Google, Facebook, and Github
Stars: ✭ 676 (-17.86%)
Mutual labels:  spring-security
Hibernate Orm
Hibernate's core Object/Relational Mapping functionality
Stars: ✭ 4,806 (+483.96%)
Mutual labels:  jpa
Cerberus
A demonstration of a completely stateless and RESTful token-based authorization system using JSON Web Tokens (JWT) and Spring Security.
Stars: ✭ 482 (-41.43%)
Mutual labels:  spring-security
Taroco
整合Nacos、Spring Cloud Alibaba,提供了一系列starter组件, 同时提供服务治理、服务监控、OAuth2权限认证,支持服务降级/熔断、服务权重,前端采用vue+elementUI+webpack,可以很好的解决转向Spring Cloud的一系列问题。
Stars: ✭ 545 (-33.78%)
Mutual labels:  spring-security
Simplemall
基于SpringCloud的微服务架构实战案例项目,以一个简单的购物流程为示例,融合spring cloud 相关组件,如spring-cloud-netflix、swagger等
Stars: ✭ 687 (-16.52%)
Mutual labels:  spring-security
Spring Lemon
Helper library for Spring Boot web applications
Stars: ✭ 529 (-35.72%)
Mutual labels:  spring-security
Febs Security
Spring Boot 2.0.4 & Spring Security 5.0.7 权限管理系统。(精力有限,停止维护)
Stars: ✭ 721 (-12.39%)
Mutual labels:  spring-security
Hellokoding Courses
HelloKoding provides practical coding guides series of Spring Boot, Java, Algorithms, and other topics on software engineering
Stars: ✭ 490 (-40.46%)
Mutual labels:  spring-security
Logback
💡 SpringBoot+Spring Security基本配置
Stars: ✭ 776 (-5.71%)
Mutual labels:  spring-security
Fw Cloud Framework
基于springcloud全家桶开发分布式框架(支持oauth2认证授权、SSO登录、统一下单、微信公众号服务、Shardingdbc分库分表、常见服务监控、链路监控、异步日志、redis缓存等功能),实现基于Vue全家桶等前后端分离项目工程
Stars: ✭ 717 (-12.88%)
Mutual labels:  jpa
Jpa Spec
A JPA Query By Specification framework.
Stars: ✭ 620 (-24.67%)
Mutual labels:  jpa
Angular Springboot Rest Jwt
Springboot, Angular and JWT security - Example Project based on Northwind Order Processing
Stars: ✭ 603 (-26.73%)
Mutual labels:  spring-security

spring-security-jwt-guide

如果国内访问缓慢的话,可以通过码云查看: https://gitee.com/SnailClimb/spring-security-jwt-guide

前言

Spring Security 是 Spring 全家桶中非常强大的一个用来做身份验证以及权限控制的框架,我们可以轻松地扩展它来满足我们当前系统安全性这方面的需求。

但是 Spring Security 相比于其他一些技术比如 JPA 来说更难上手,很多人初学的时候很难通过看视频或者文档发就很快能独立写一个 Demo 出来,于是后面可能就放弃了学习这个东西。

刚来公司的时候的入职培训实战项目以及现在正在做的项目都用到了 Spring Security 这个强大的安全验证框架,可以看出这个框架在身份验证以及权限验证领域可以说应该是比较不错的选择。由于之前经历项目的这部分模块都不是自己做的,所以对于 Spring Security 并不是太熟悉。于是自/己抽时间对这部分知识学习了一下,并实现了一个简单的 Demo 。这个 Demo 主要用到了 Spring SecuritySpring Boot 这两门技术,并且所有的依赖采用的都是最新的稳定版本。初次之外,这个项目还用到了 JPA 这门技术。

由于自己的能力以及时间有限,所以一定还有很多可以优化的地方,有兴趣的朋友可以一起完善,期待你的 PR。

介绍

项目用到的一些框架/服务:

你能从这个项目中学习到什么?

  1. Spring Security +JWT 实现登入登出以及权限校验
  2. JPA 实现审计功能、多对多的映射关系如何通过关联表实现

教程

  1. 项目讲解/分析 (内容待重构)
  2. swagger3.0整合

代办

  • [x] 增加H2内存数据库支持,无须MySQL,一键启动项目启动后访问 http://localhost:9333/api/h2-console (用户名:root,密码:123456)
  • [x] 增加Swagger,方便调用接口
  • [x] 异常处理部分代码重构,优化返回结构
  • [x] 新建一个role表,然后通过新建一个role_user表的形式,将用户与角色关联起来
  • [x] 文件结构重构
  • [x] 增加jpa审计功能
  • [x] login(登录)接口在controller层暴露出来
  • [x] 登出功能:redis保存token信息(key->user id,value->token),登出后将 redis中的token信息删除
  • [x] 重新登录将上一次登录生成的token弄失效(解决未过期的token还是可以用的问题):重新登录会将 redis 中保存的 token 信息进行更新
  • [ ] 重构详解文章

项目概览

为了区分,我把 Spring Security相关的都单独放在了一个文件夹下面。

如何运行项目

  1. git clone https://github.com/Snailclimb/spring-security-jwt-guide.git
  2. 打开项目并且等待 Maven 下载好相关依赖。建议使用 Intellij IDEA 打开,并确保你的 Intellij IDEA 下载了 lombok 插件。
  3. 下载 redis 并application.yaml中redis的配置
  4. 运行项目(相关数据表会被自动创建,不了解的看一下 JPA)

示例

1.注册一个账号

URL:

POST http://localhost:9333/api/users/sign-up

RequestBody:

{"userName":"123456","fullName":"shuangkou","password":"123456"}

注册

新注册的用户默认绑定的角色为:用户(USER)和管理者(MANAGER)。

2.登录

URL:

POST http://localhost:9333/api/auth/login

RequestBody:

{"username": "123456", "password": "123456","rememberMe":true}

登录

3.使用正确 Token 访问需要进行身份验证的资源

我们使用 GET 请求访问 /api/users,这个接口的访问权限是

@PreAuthorize("hasAnyRole('ROLE_USER','ROLE_MANAGER','ROLE_ADMIN')")

Access resources that require authentication

4.不带 Token 或者使用无效 Token 访问

我们使用 GET 请求访问 /api/users,但是不带token或者带上无效token。

Access resources that require authentication without token or with invalid token

5.带了正确Token但是访问权限

我们使用 DELETE 请求访问 /api/users?username=xxx,携带有效的 token ,但是 token 的访问权限不够。

参考

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