jwt 的基本使用
参考资料
JWT最详细教程以及整合SpringBoot的使用(简洁易上手)
各类JWT库(java)的对比
认证授权服务实战
jwt 官网: https://jwt.io/
简单使用 spring boot + jwt
使用 interceptor 简单的实现了登录以及校验 token
commit id
关于 secret、token 的续签、与防止令牌盗用
参考
JWT常见面试总结
springboot中如何获取真实ip地址
springboot~容器化环境获取真实IP地址
springboot获取请求ip、系统、浏览器等信息
commit id
- jwt 的生成与校验
- secret 与用户属性相关
- 可注销
- token 保存在 redis 中
- 每次访问都会刷新 token
- 简单实现登录日志
- 并没有处理密码的匹配,只匹配了用户名
关于防止令牌盗用,暂时没有写那么复杂,只是简单对比了请求头与 redis 中的 uuid
测试
将如下代码保存到一个 html 文件,打开后依次点击界面上的几个按钮即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id='login'>login</button>
<br /><br /><br />
<button id='dict'>dict detail</button>
<br /><br /><br />
<button id='logout'>logout</button>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script>
$(()=>{
let uuid = undefined
let token = undefined
$('#login').on('click',()=>{
$.ajax({
url: 'http://localhost:8092/auth/login',
method: 'POST',
headers:{'Content-Type':'application/json;charset=utf8'},
dataType: "json",
data:JSON.stringify({
"username": "superAdmin",
"password": "123456"
}),
success: (res)=>{
console.log(res)
uuid = res.body.uuid
token = res.body.token
},
error: (err) => {
console.log(err)
}
})
})
$('#dict').on('click',()=>{
$.ajax({
url: 'http://localhost:8092/system/dict/detail',
method: 'GET',
headers:{
'Content-Type':'application/json;charset=utf8',
'uuid':uuid,
'Authorization': 'Bearer ' + token,
},
dataType: "json",
success: (res)=>{
console.log(res)
},
error: (err) => {
console.log(err)
}
})
})
$('#logout').on('click',()=>{
$.ajax({
url: 'http://localhost:8092/auth/logout',
method: 'GET',
headers:{
'Content-Type':'application/json;charset=utf8',
'uuid':uuid,
'Authorization': 'Bearer ' + token,
},
dataType: "json",
success: (res)=>{
console.log(res)
},
error: (err) => {
console.log(err)
}
})
})
})
</script>
</body>
</html>
关于 RBAC 权限管理模型与 Spring Security 的参考资料
RBAC权限管理模型:基本模型及角色模型解析及举例
RBAC权限模型——理论篇
万字长文:深入浅出RBAC权限设计
security 整合 jwt 实现 RBAC0
commit id
api 测试
具体可参考 security-rbac0/dox/test.http
文件, 使用不同用户登录,测试不同的 api 即可
@hostname = localhost
@port = 8092
@contentType = application/json
### 登录
# @name login
POST http://{{hostname}}:{{port}}/auth/login HTTP/1.1
Content-Type: {{contentType}}
{
// "username":"superAdmin",
"username":"eolinkerAdmin",
// "username":"mallAdmin",
"password": "12345678"
}
### 保存 token
@token=Bearer {{login.response.body.$.body.token}}
@uuid={{login.response.body.$.body.uuid}}
### 登录即可访问
GET http://{{hostname}}:{{port}}/system/dict/detail HTTP/1.1
Authorization: {{token}}
uuid: {{uuid}}
Content-Type: {{contentType}}
### 超级管理员
GET http://{{hostname}}:{{port}}/system/menu/list HTTP/1.1
Authorization: {{token}}
uuid: {{uuid}}
Content-Type: {{contentType}}
### eolinkerAdmin 角色
GET http://{{hostname}}:{{port}}/eolinker/apiProject/list HTTP/1.1
Authorization: {{token}}
uuid: {{uuid}}
Content-Type: {{contentType}}
### mallAdmin 角色
GET http://{{hostname}}:{{port}}/mall/product/list HTTP/1.1
Authorization: {{token}}
uuid: {{uuid}}
Content-Type: {{contentType}}
数据解释
数据库脚本在 security-rbac0/dox/thresh_reference.sql
,详细的数据内容可自行查看数据库脚本
共有三个用户:
superAdmin
: 超级管理员, 拥有所有角色,和权限字符串 *:*:*
eolinkerAdmin
: eolinker 模块管理员,目前只有一个 eolinker:apiProject:list
权限
mallAdmin
: mall 模块管理员, 目前只有一个 mall:product:list
效果
superAdmin
可访问所有资源
eolinkerAdmin
可访问 /system/dict/detail
与 /eolinker/apiProject/list
mallAdmin
可访问 /system/dict/detail
与 /mall/product/list
关于若依的鉴权与数据权限过滤分析
参考资料
若依源码解析:RuoYi-Vue登录和鉴权的实现
深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
Mybatis-Plus通过注解形式实现数据权限过滤
从零搭建开发脚手架 基于Mybatis-Plus的数据权限实现