Skip to content

HiAuth是一个开源的基于Oauth2协议的认证、授权系统。

License

Notifications You must be signed in to change notification settings

bestaone/HiAuth

Repository files navigation

Hi Auth

HiAuth是一个开源的基于OAuth2.0协议的认证、授权系统,除了标准的OAuth2.0授权流程功能外,还提供了应用管理、用户管理、权限管理等相关功能。

Star Fork Language License Author

介绍

除了认证相关功能外,还提供了/example/demo/example/himall项目,供用户参考如何集成。

  • 参考demo实例,你可以几分钟之内快速验证如何集成HiAuth;
  • 参考himall实例,你可以快速的启动一个带页面的实例;

LIVE

目录结构

├─cicd                              持续集成
├─docs                              开发文档
├─example                           实例
│ ├─demo                            基础实例
│ ├─himall                          带有页面的实力
├─hiauth-client-spring-boot-starter 客户端SDK,如果深度的集成,可以参考此SDK,或者直接使用;
├─hiauth-front                      管理端前端项目
├─hiauth-server                     HiAuth服务端
├─hiauth-resource                   HiAuth资源服务端
├─other                             其他内容,数据库脚本等

效果图

  • 认证中心登录页

  • 管理后台登录页

  • 超级管理员-用户管理页

  • 企业管理员-部门列表页

  • 企业管理员-员工列表页

如果你觉得此项目对你有帮助,请给我点个star,谢谢!

快速尝试

环境要求

  • Git
  • JDK17+
  • Maven 3.8+

下载源码

$ git clone https://github.com/bestaone/HiAuth.git

构建、启动

# 启动himall实例
$ cd HiAuth/example/himall
$ mvn clean install
$ mvn spring-boot:run

验证

注意:127.0.0.1不能使用localhost代替,因为数据库中配置了回调地址为http://127.0.0.1:9000

认证模式

authorization_code模式:

# 最后的yourCode替换为上面步骤获取的授权码
$ curl --location 'http://auth.hiauth.cn/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Accept: application/json' \
--data 'grant_type=authorization_code&redirect_uri=http%3A%2F%2F127.0.0.1%3A9000%2Flogin%2Foauth2%2Fcode%2Fhiauth-code&client_id=himall&client_secret=secret&code=yourCode'

返回结果:

{
    "access_token": "eyJraWQiOiJkZTYxMjVmNi0wYTQ5LTQwMGYtYWMzMC02M2U2Zm",
    "refresh_token": "8WS6liiSW0gmUy8yudFAPIHGor3Hf6yBtaBTUNjj3-q9y4JXRlBZ",
    "scope": "profile",
    "token_type": "Bearer",
    "expires_in": 35999
}

client_credentials模式:

$ curl --location 'http://auth.hiauth.cn/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Accept: application/json' \
--data 'grant_type=client_credentials&client_id=himall&client_secret=secret&scope=profile'

返回结果:

{
  "access_token": "eyJraWQiOiJkZTYxMjVmNi0wYTQ5LTQwMGYtYWMzMC02M2U2Zm",
  "scope": "profile",
  "token_type": "Bearer",
  "expires_in": 35999
}

用户信息获取:

# 将accessToken替换为上面步骤获取的访问令牌
$ curl --location --request POST 'http://auth.hiauth.cn/userinfo' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer accessToken'

返回结果

{
    "sub": "corpadmin",
    "empId": 1,
    "avatarUrl": "/unpapi/image/2c924149ddfe4bd181959ee9bede10c0.jpeg",
    "appId": 91,
    "name": "企业管理员",
    "phoneNum": "13400000001",
    "userId": 11,
    "authorities": [],
    "cid": 1,
    "username": "corpadmin"
}

scop权限:

  • 在授权请求中包含所需scope
  • 获取的访问令牌将包含授予的scope
  • 资源服务器验证请求的scope是否匹配
@PreAuthorize("#oauth2.hasScope('profile')")
@GetMapping("/protected")
public String protectedResource() {
    return "Accessed protected resource";
}

其他

更多集成方式

社区与作者

如果群二维码失效了,请先添加我的微信,然我我拉你入群。

授权协议

本项目执行 MIT 协议