Monkeyk Spring Oauth Server Save

Deep Integrate Spring Security & OAuth2

Project README

spring-oauth-server


java config版本

Spring与OAuth2的整合示例. OIDC1.0 + OAuth2.1

项目用Maven管理
Base on SpringBoot

使用的主要技术与版本号

  1. JDK (openjdk 17)
  2. Spring Boot(3.1.5)
  3. Spring Core(6.0.11)
  4. spring-security-oauth2-authorization-server (1.1.1)
  5. thymeleaf (3.1.1.RELEASE)

授权协议

GPL-2.0

技术视频

  1. spring-oauth-server开源项目背景 2018-09-16
  2. 如何在本地运行spring-oauth-server(传统版本) 2018-09-17
  3. 如何在本地运行spring-oauth-server(config版本) 2018-10-08
  4. 在线测试环境的使用 2018-10-14
  5. spring-oauth-server v2.0.1更新内容简介 2019-08-05

注意:以上视频主要适用于v2.x版本

http://list.youku.com/albumlist/show/id_51900110.html (持续更新...)

1000 star Gitee奖杯: sos-1000-stars.jpg [2019年]


版本分支介绍


相关项目

  1. OAuth2客户端项目请访问 spring-oauth-client

  2. 在线测试访问地址 https://andaily.com/spring-oauth-server (v1.0)

  3. Shiro与OLTU整合的OAuth2项目 https://gitee.com/mkk/oauth2-shiro (相比spring-oauth-server, 该项目入门门槛相对较低, 代码更加透明, 理解更容易,可扩展性更强, 且模块化开发)


如何使用?

  1. 项目是Maven管理的, 需要本地安装Maven(开发用的maven版本号为3.6.0), 还有MySql(开发用的mysql版本号为5.7.22)

  2. 下载(或clone)项目源码到本地

  3. 创建MySQL数据库(默认数据库名oauth2_boot), 并运行相应的SQL脚本(脚本文件位于others/database目录), 运行脚本的顺序: initial_db.ddl -> oauth.ddl -> initial_data.ddl

  4. 修改application.properties(位于src/main/resources目录)中的数据库连接信息(包括username, password等)

  5. 将本地项目导入到IDE(如Intellij IDEA)中, 可直接运行SpringOauthServerApplication.java进行访问;或配置Tomcat(或类似的servlet运行服务器), 并启动Tomcat(默认端口为8080); 也可通过maven package命令将项目编译为jar文件(spring-oauth-server.jar), 使用命令java -jar启动访问. 若使用java -jar spring-oauth-server.jar启动, 建议使用参数spring.config.location指定配置文件, 如:java -jar spring-oauth-server.jar --spring.config.location=xxx.properties

  6. 参考oauth2.1-flow.md(位于others目录)的内容并测试之(也可在浏览器中访问相应的地址,如: http://localhost:8080/ 在界面上操作).

配置参数说明

说明配置文件application.properties中的主要变量。
参数名必须?默认值说明
spring.datasource.*-数据库连接相关配置
spring.thymeleaf.*-Spring MVC thymeleaf相关配置
server.port8080服务运行端口号
spring.security.oauth2.authorizationserver.issuerOAuth2 issuer, 生产环境配置对外访问完整地址
spring.application.name应用组件名称

grant_type 介绍

说明OAuth2.1支持的grant_type(授权方式)与功能

  1. authorization_code -- 授权码模式(即先登录获取code,再获取token)
  2. authorization_code + PKCE -- 授权码模式+PKCE (即先登录获取code, 请求时增加参数code_challengecode_challenge_method; 再获取token,增加参数code_verifier)
  3. password -- 密码模式(将用户名,密码传过去,直接获取token) OAuth2.1不推荐使用
  4. refresh_token -- 刷新access_token
  5. device_code -- 适用于各类无输入键盘的物联网智能设备进行认证授权, 通过类似'扫码登录'形式完成整个流程 OAuth2.1新增
  6. client_credentials -- 客户端模式(无用户,用户向客户端注册,然后客户端以自己的名义向'服务端'获取资源)
  7. jwt-bearer -- 增强client端请求安全性的断言(assertion)实现; 通过类似'双向SSL'的机制来让server端验证client端的签名实现强安全性.

注意:相比OAuth2.0,去掉了 implicit 模式


帮助与改进

  1. 与该项目相关的博客请访问 https://blog.csdn.net/monkeyking1987/article/details/16828059

  2. 如果在使用过程中遇到特殊的问题(如:如何将oauth_code存入数据库),请访问项目的 Wiki附件.
    我会把大家反馈的问题解决办法添加在这里.
    若在这两个地方没有找到解决办法的, 欢迎发邮件到[email protected]一起讨论.

  3. 如果在使用项目的过程中发现任何的BUG或者更好的提议, 建议将其提交到项目的 Issues 中, 我会一直关注并不断改进项目.


功能扩展

  1. oauth_code存入数据库的配置, 请下载文件 oauth_code存入数据库的配置.jpg
  2. 改变token过期的时间的配置, 请下载文件改变token过期的时间的配置.jpg
  3. 自定义 grant_type, 默认情况支持的grant_type包括 [password,authorization_code,refresh_token,device_code], 若不需要其中的某些grant_type, 则可以修改 oauth_client_details 表中的 authorized_grant_types 字段的值;
  4. 如何刷新access_token(refresh_token), 在通过客户端(如移动设备)登录成功后返回的数据如下
    {"access_token":"eyJraWQiOiJteW9pZGMta2V5aWQiLCJhbGciOiJSUzI1...","token_type":"bearer","refresh_token":"UCFNxUj4ytr241KzwJJgnMno1RfmoLs0GKVxNWPjW5VZ7d4U4YsDM7...","expires_in":43199,"scope":"openid"}

    若需要刷新获取新的token(一般在 expires_in 有效期时间快到时), 请求的URL类似如下
    http://localhost:8080/oauth2/token?client_id=mobile-client&client_secret=mobile&grant_type=refresh_token&refresh_token=UCFNxUj4ytr241KzwJJgnMno1RfmoLs0GKVxNWPjW5VZ7

    注意: refresh_token 参数值必须与登录成功后获取的 refresh_token 一致, 且grant_type = refresh_token
    另: 刷新token 需要 ClientDetails 支持 refresh_token 类型的 grant_type (默认是支持的)

开发计划

从 0.3版本开始将项目的所有计划的开发内容列出来, 方便大家跟进, 也欢迎你加入.
项目的开发管理使用开源项目 andaily-developer.

  • Version: 3.0.0 [finished]
    Date: 2023-10-10 / 2023-10-31

    1. 底层安全架构升级:jdk升级17, spring6.x, springboot3.x, thymeleaf替换servlet/jsp

    2. 全面升级支持 OAuth2.1协议与 OIDC1.0协议

    3. 构建包由war换成jar, SQL相应调整

    4. 用spring-security-oauth2-authorization-server升级替换spring-security-oauth2, 详见背景说明

    5. 界面使用说明按OAuth2.1进行友好设计并更新各提示语句

    6. 增加spring-restdocs文档支持, 自动生成API相关文档

  • Version: 2.1.1 [canceled]
    Date: 2022-05-05 / ---

    1. 尝试升级替换spring-security-oauth2, 详见背景说明

  • Version: 2.1.0 [finished]
    Date: 2020-06-05 / 2022-05-01

    1. 升级access_token, refresh_token为JWT(Json Web Token), 提高性能

    2. 增加灵活性配置参数sos.reuse.refresh-token可实时延长token时效(类似session机制)

    3. 升级spring-security oauth2版本为2.3.8.RELEASE, 增加spring-security-jwt版本1.1.1.RELEASE


  • Version: 2.0.2 [finished]
    Date: 2019-08-05 / 2020-06-04

    1. Fix CVE-2019-3778, use spring-security-oauth 2.3.5.RELEASE

    2. 解决数据库多条 access_token问题,增加唯一约束

    3. 升级Spring-Boot版本为2.1.4.RELEASE


  • Version: 2.0.1 [finished]
    Date: 2018-05-01 / 2019-08-04

    1. 增加使用代码生成AccessToken功能

    2. 增加将AccessToken存入Redis的配置参考

    3. 升级Spring Security OAuth版本为2.3.4.RELEASE

    4. 修改ROLE的错误配置

    5. Use spring-boot 2.0.2.RELEASE

    6. Fix issue #IJO9R /oauth/rest_token 接口 client_secret字段没有校验

    7. 将项目用视频方式展现出来,更直观


  • Version: 2.0.0 [finished]
    Date: 2018-04-09 / 2018-04-21

    1. 更新UI,为了更易理解与使用,场景化

    2. 使用 spring-boot 重构
  • Version: 1.1 [cancel]
    Date: 2018-10-14 / ---

    1. ---

  • Version: 1.0 [finished]
    Date: 2017-03-30 / 2018-04-04

    1. implicit测试时 取消掉 client secret

    2. 更新UI,为了更易理解与使用,场景化

    3. 增加删除access_token API

    4. 增加删除 refresh_token API

    5. 增加校验 access_token API: /oauth/check_token

    6. Fix ISSUE #IGNQ9 CustomJdbcTokenStore中的CacheEvict不起作用

    7. ---

  • Version: 0.6 [finished]
    Date: 2016-07-07 / 2016-10-13

    1. (150) - 修改OAUTH错误时返回JSON数据

    2. (151) - 数据添加Ehcache缓存支持

    3. (158) - 对配置,代码必要的地方添加注释,方便理解

    4. 添加OIDC协议文档

  • Version: 0.5 [finished]
    Date: 2016-02-19 / 2016-05-24

    1. (118) - Add java-config(零配置) 的支持, 以及启用 新的注解, branch: config

    2. (138) - OAuth 'token' Restful API

    3. (139) - User Overview/ user add/archive

    4. (143) - Add project API document

    5. (144) - Add MongoDB branch


  • Version: 0.4 [finished]
    Date: 2015-11-09 / 2015-11-30

    1. (97) - Fix custom access_token_validity,refresh_token_validity issue(#5)

    2. (109) - 升级 spring-security-oauth2 的版本到 2.0.6以上, 目前是1.0.5

    3. (113) - Upgrade spring, spring security version to > 4.0

    4. 将项目添加到在线测试服务器

    5. (115) - Sync update spring-oauth-client version with spring-oauth-server

    6. (116) - Remove mybatis dependency

    7. Upgrade JAVA to 1.8; Servlet 3.0

    8. Oauth table add index

  • Version: 0.3 [finished]
    Date: 2015-05-14 / 2015-06-07

    1. #73 - Upgrade 'spring-security-oauth2' version to '2.0.6.RELEASE' (current: 1.0.5.RELEASE) [CANCELED]

    2. #74 - oauth mysql ddl add create_time, default is now()

    3. #75 - Add user information API, for spring-oauth-client project use
                          URL: /unity/user_info
                          Login: Yes (ROLE_UNITY)
                          Data Format: JSON
                          URL: /m/user_info
                          Login: Yes (ROLE_MOBILE)
                          Data Format: JSON
                          
    4. #77 - User add Privilege domain. Addition initial two user: unityuser(ROLE_UNITY),mobileuser("ROLE_MOBILE). If default user, return all privilegs, otherwise return specify privilege(s)

    5. #78 - Initial 'sprint-oauth-client' project(maven), add sub-modules

    6. #91 - User log4j replace logback dependency

    7. #92 - Add database table column description. (添加数据库表的字段说明)

    8. #93 - 将默认的 oauth_code存入数据库(当前是存入内存)

    9. spring-oauth-server project add Bootstrap CSS

    10. #95 - Add 'client-details' management; create/delete, show testing links



数据库表字段说明

在0.3版本中添加了db_table_description.html文件(位于/others目录, 3.0.0版本db_table_description_3.0.0.html), 用来说明数据库脚本文件oauth.ddl中各表,各字段的用途及使用场合.
也可在线访问db_table_description_3.0.0.html.


Project Log

  1. 2013-11-19 Initial project, start push code

  2. 2013-11-20 发布0.1版本

  3. 2015-05-06 发布0.2版本

  4. 2015-05-27 创建项目博客,访问地址 http://andaily.com/blog/?cat=19

  5. 2015-06-07 发布0.3版本

  6. 2015-06-16 添加github访问: https://github.com/monkeyk/spring-oauth-server, 以后的更新将同步github与gitosc.

  7. 2015-11-09 开始开发 0.4-beta 版本

  8. 2015-11-18 发布 0.4-beta 版本

  9. 2016-01-02 发布 0.4 版本

  10. 2016-02-19 Add 0.5 version development planning

  11. 2016-04-03 Add config branch

  12. 2016-04-14 Add mongodb branch

  13. 2018-04-21 使用spring-boot重构,增加2.0.0实现 2.0.0

  14. 2018-05-01 开始 2.0.1 分支开发

  15. 2019-07-04 Add Redis config branch

  16. 2019-08-04 发布 2.0.1 版本

  17. 2020-06-04 发布 2.0.2 版本

  18. 2022-05-01 发布 2.1.0 版本

  19. 2023-10-10 开始全新大版本 3.0.0 开发

  20. 2023-10-31 发布 3.0.0 全新版本


更多资源

以下是在学习工作中收集的更多关于OAuth2的资源,对深入理解与运用OAuth2有帮助


与项目相关的技术文章请访问 http://andaily.com/blog/?cat=19 (不断更新与OAuth2相关的文章)
问答与讨论
与项目相关的,与OAuth2相关的问题与回答,以及各类讨论请访问
http://andaily.com/blog/?dwqa-question_category=oauth

使用案例

以下是已知的使用(或基于) spring-oauth-server 开源项目的各类商业项目(排名不分先后), 若你有案例希望添加, 请联系作者.

  • Hongkong Parkway Online (在线医疗服务系统)

  • 海尔日日平台 (B2B电商平台)

  • wdcy-game (手机游戏服务端)

  • Honyee Management System (企业管理系统)

  • AoLin Open Platform (国际物流开发平台)

  • IDS (移动安全产品)

  • ......


捐助

支付宝: [email protected] (**钊)
  • 快意江湖 -- 100元
  • yufan -- 100元

其他...

关注更多开源项目请访问 https://andaily.com/my_projects.html

欢迎联系作者 [email protected] 进行探讨

Open Source Agenda is not affiliated with "Monkeyk Spring Oauth Server" Project. README Source: monkeyk/spring-oauth-server
Stars
82
Open Issues
1
Last Commit
2 months ago
License

Open Source Agenda Badge

Open Source Agenda Rating