做venn图网站,广元做网站,大气公司网站源码,wordpress怎么设置邮箱Hi Auth
HiAuth是一个开源的基于Oauth2协议的认证、授权系统#xff0c;除了标准的Oauth2授权流程功能外#xff0c;还提供了应用管理、用户管理、权限管理等相关功能。
在这个项目中你能够了解到如何基于spring-security-oauth2-authorization-server实现自己的Authorizat…
Hi Auth
HiAuth是一个开源的基于Oauth2协议的认证、授权系统除了标准的Oauth2授权流程功能外还提供了应用管理、用户管理、权限管理等相关功能。
在这个项目中你能够了解到如何基于spring-security-oauth2-authorization-server实现自己的Authorization Server 认证服务、资源服务器以及如何第三方集成本项目基于SpringBoot 3.0 版本开发。 项目源码地址https://github.com/bestaone/HiAuth 介绍
除了认证相关功能外还提供了hiauth-mall、hiauth-mgr-svc项目供用户参考如何集成。
参考hiauth-mall你可以了解如何在第三方应用中集成hiauth授权服务参考hiauth-mgr-svc项目你可以快速的启动一个微服务项目的框架搭建亦可以在这里找到一些技术的最佳实践为你的项目开发提供参考
HiAuth 2.0 升级到 3.0 的升级内容
SpringBoot 3.0.2spring-security-oauth2-authorization-server 1.0.0mybatis-plus 3.5.3.1
调整比较大的地方
SpringSecurity升级到6.0版本后用法有不少改动SpringSecurity5以后已不再支持Authorization Server取而代之的事spring-security-oauth2-authorization-server 项目前端从vue-element-admin换成了AntDesignPro
如果你觉得此项目有价值请给我点个star谢谢 项目地址https://github.com/bestaone/HiAuth 目录结构
├─doc 文档目录架构设计、数据库设计...
├─cicd 持续集成相关脚本
├─hiauth-parent 统一管理依赖必选
├─hiauth-server hiauth认证服务必选
├─hiauth-resource hiauth资源管理服务参考如何集成资源服务参考
├─hiauth-himall himall是一个demo参考此项目了解如何集成hiauth参考
├─hiauth-mgr-svc hiauth管理端后台服务基于SpringBoot可选
├─hiauth-mgr-fornt hiauth管理端前端代码基于AntDesignPro可选功能
这个项目可以帮你实现基于Oauth2协议的统一认证、授权系统这个项目可以帮助你快速的启动一个基于SpringBoot技术栈的微服务框架搭建如果你仅仅使用到了Oauth2协议的统一认证、授权系统功能那么你用技术可以是除java外的技术栈himall演示了authorization_code模式HiAuth支持了用户名密码、手机号短信两种认证方式发送短信、登录接口实现了图形验证码防刷功能HiAuth是一个前后端分离项目前端使用了React、AntDesign技术参考这个项目你可以自定义开发管理后端基于SpringBoot项目更容易集成到多个平台SpringCloud、K8S、Istio演示了如何统一控制接口规范演示了如何规范异常处理演示了如何规范使用MyBaits-Plus、分页演示了单元测试、mock测试、测试数据回滚包括对controller、service的测试
截图
HiMall UMC 前端从vue-element-admin换成了AntDesignPro截图还没来得及做新的 LIVE DEMO
Hiauth HiMallhttp://himall.hiauth.cnHiauth Mangerhttp://mgr.hiauth.cnHiauth Serverhttp://auth.hiauth.cn
快速启动
环境需求
JDK17Maven 3.8.6MySQL 8RedisNodeJS v16(v18)Yarn 1.22.4
下载源码
git clone https://github.com/bestaone/HiAuth.git创建数据库
在你的mysql数据库中创建库hiauth并执行下面脚本: HiAuth\doc\hiauth.sql# 也可以从JAR自己提取相关表结构
# org.springframework.security.oauth2.server.authorization.client/oauth2-registered-client-schema.sql
# org.springframework.security.oauth2.server.authorization/oauth2-authorization-consent-schema.sql
# org.springframework.security.oauth2.server.authorization/oauth2-authorization-schema.sql调整配置
需要调整的配置有数据库、redis默认会使用native.properties配置如果和你的环境不一致。
请修改:
# 需要将如下两个文件中的mysql、redis的配置改成自己的
# HiAuth\hiauth-server\src\main\properties\native.properties
# HiAuth\hiauth-mgr-svc\src\main\properties\native.propertiesapp.hosthttp://127.0.0.1:8080database.urljdbc:mysql://mysql-server:3306/hiauth3?useUnicodetruecharacterEncodingutf8zeroDateTimeBehaviorconvertToNulluseSSLtrueserverTimezoneGMT%2B8
database.usernamedev
database.password123456redis.hostredis-server
redis.port6379
redis.database8
redis.password添加host
# IP 换成你自己的
127.0.0.1 redis-server
127.0.0.1 mysql-server构建、启动
# 编译后台会执行单元测试需要正确配置数据库和redis
cd HiAuth
mvn clean install# 构建前端并启动
cd HiAuth\hiauth-mgr-front
yarn install
yarn start# 启动hiauth授权服务端
cd HiAuth\hiauth-server
mvn spring-boot:run# 启动hiauth资源服务端
cd HiAuth\hiauth-resource
mvn spring-boot:run# 启动himall
cd HiAuth\hiauth-himall
mvn spring-boot:run# 启动hiauth管理后端
cd HiAuth\hiauth-mgr-svc
mvn spring-boot:run验证
验证登录
访问HiMallhttp://127.0.0.1:8081访问Serverhttp://127.0.0.1:8080访问Managerhttp://127.0.0.1:8080 (具体端口看前端项目启动后的控制台输出)
验证Swagger
访问HiAuth的Swagger地址http://127.0.0.1:8080/swagger-ui.html直接测试接口显示未认证
{error: unauthorized,error_description: Full authentication is required to access this resource
}点击认证按钮会被重定向到登录输入账号登录进行认证认证成功后被重定向回swagger页面再次测试接口获取正确数据
验证Oauth2流程
authorization_code 认证流程
在浏览器中输入如下地址会调到登录页登录完成后会跳转到百度在浏览器url中把code码复制出来
http://127.0.0.1:8080/oauth2/authorize?client_iddemo-client-idresponse_typecodescopeuser_inforedirect_urihttp://www.baidu.com使用上面获取的code换取accessToken
curl --location --request POST http://127.0.0.1:8080/oauth2/token?grant_typeauthorization_codecodecoderedirect_urihttp://www.baidu.com \--header Authorization: Basic ZGVtby1jbGllbnQtaWQ6ZGVtby1jbGllbnQtc2VjcmV0{access_token: xxxxxx,refresh_token: yyyyy,scope: user_info,token_type: Bearer,expires_in: 7199
}Authorization Basic base64.encode(client_id:client_secret)可以在网上找个在线工具生成或者直接用postman测试Authorization选择Basic Auth填入对应值即可 访问受控接口不带accessToken返回401未授权
curl --location --request POST http://127.0.0.1:8082/user/info{error: unauthorized,error_description: Full authentication is required to access this resource
}访问受控接口携带accessToken返回数据
curl --location --request POST http://127.0.0.1:8082/user/info --header Authorization: Bearer token{name:Resource
}refresh_token
curl --location --request POST http://127.0.0.1:8080/oauth2/token?grant_typerefresh_tokenrefresh_tokenrefresh_token \--header Authorization: Basic ZGVtby1jbGllbnQtaWQ6ZGVtby1jbGllbnQtc2VjcmV0{access_token: xxxxxx,refresh_token: yyyyy,scope: user_info,token_type: Bearer,expires_in: 6152
}client_credentials 认证流程
使用POST访问获取access_token接口设置grant_typeclient_credentials
curl --location --request POST http://127.0.0.1:8080/oauth2/token?grant_typeclient_credentialsscopeuser_info \--header Authorization: Basic ZGVtby1jbGllbnQtaWQ6ZGVtby1jbGllbnQtc2VjcmV0{access_token: xxxxxx,scope: user_info,token_type: Bearer,expires_in: 7199
}scop权限范围验证
使用POST访问获取access_token接口,设置grant_typeclient_credentialsscopemessage.read curl --location --request POST http://127.0.0.1:8080/oauth2/token?grant_typeclient_credentialsscopemessage.read --header Authorization: Basic ZGVtby1jbGllbnQtaWQ6ZGVtby1jbGllbnQtc2VjcmV0# 返回的 token 的权限范围是 message.read
{access_token: xxxxxx,scope: message.read,token_type: Bearer,expires_in: 7199
}使用拥有message.read权限的toke访问用户接口被拒绝提示无权限
curl --location --request POST http://127.0.0.1:8082/user/info --header Authorization: Bearer token{error: insufficient_scope,error_description: Insufficient scope for this resource,scope: user_info
}所有的127.0.0.1不能使用localhost代替因为auth会检查域名的合法性数据库中登记的是127.0.0.1 集成认证、授权服务
这里为了演示如何集成HiAuth、提供了一个Demo项目HiMall。
HiMall
HiMall是基于SpringBoot技术的微服务项目其集成了HiAuth的认证、授权。
环境需求
JDK17HiAuth3
安装、启动
#编译、构建项目
cd HiAuth\hiaut-himall
mvn clean install
mvn spring-boot:run验证authorization_code模式认证
访问地址http://127.0.0.1:8081点击Login会被重定向到HiAuth系统进行认证认证通过后会被重定向回HiMall此时HiMall也将持有登录状态
授权协议
本项目执行 MIT 协议
社区与作者 如果群二维码失效了请先添加我的微信然我我拉你入群。