# spring-authorization-server-demo **Repository Path**: ftzheng/spring-authorization-server-demo ## Basic Information - **Project Name**: spring-authorization-server-demo - **Description**: 使用spring-authorization-server自定义的一个简单的授权服务 相关链接 https://spring.io/projects/spring-authorization-server - **Primary Language**: Java - **License**: MIT - **Default Branch**: 1.0.0 - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-02-08 - **Last Updated**: 2023-02-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # spring-authorization-server-demo
## 介绍 使用[Spring-Authorization-Server](https://spring.io/projects/spring-authorization-server)自定义的一个简单的授权服务。在源码基础上自定义了AuthorizationCode、AccessToken、RefreshToken以及oidc的一些额外功能,比如修改令牌的展示值为UUID、踢人下线(只签发一个可用的令牌)等。目前1.0.0版本的Spring-Authorization-Server没有Redis储存授权信息的功能,根据业务需要自定义了Redis储存授权信息功能。 > 这个项目只是个Demo > > 我已使用该项目已在另一个Nacos SpringCloud项目中做授权中心模块,使用体验不错可能还有很多坑要踩,这个项目将同步那边修改逐步完善。 ## 使用说明 ### 1. 新建数据库 新建的数据库名称为`test_oauth2` ### 2. 新建表 使用`/sql/test_oauth2.sql`的文件建表 | 表名 | 描述 | |------------------------------|-------| | authorities | 用户权限 | | oauth2_authorization | 授权信息 | | oauth2_authorization_consent | 授权同意 | | oauth2_registered_client | 客户端信息 | | users | 用户表 | ### 3. 添加测试数据的两种方法 **建议使用第一种(单元测试)** > 用户名:user 密码:password > > 客户端id:messaging-client 密码:123456 1. 运行单元测试 创建用户的单元测试: `com.maoatao.authorization.SpringAuthorizationServerDemoApplicationTests#testSaveUser()` 创建客户端的单元测试: `com.maoatao.authorization.SpringAuthorizationServerDemoApplicationTests#testSaveClient()` 2. 使用SQL导入 使用`/sql/test_data.sql`的文件导入数据 > 新增时先新增用户再新增权限 > > 删除时先删除权限再删除用户 ### 4. 生成授权码的三种方法 1. OAuth2提供的 1. 接口地址:http://127.0.0.1:8080/oauth2/authorize 2. 方法:GET 3. 权限:Spring Security 用户登录后同意授权(如果需要同意) 4. 示例:`http://127.0.0.1:8080/oauth2/authorize?response_type=code&client_id=messaging-client&scope=message.read message.write&redirect_uri=https://cn.bing.com&code_challenge_method=S256&code_challenge=3vrxycun-VbyenvO5GiFOaOBazUBX_xcFElnqbl-TXA` 2. 我自定义的 1. 接口地址:http://127.0.0.1:8080/authorization 2. 方法:POST **(application/json)** 3. 权限:无 4. 示例(Body): `{ "clientId": "messaging-client", "username": "user", "password": "password", "scopes": [ "message.read", "message.write" ], "codeChallengeMethod": "S256", "codeChallenge": "3vrxycun-VbyenvO5GiFOaOBazUBX_xcFElnqbl-TXA" }`  3. 单元测试(仅供测试) `com.maoatao.authorization.SpringAuthorizationServerDemoApplicationTests#testGenerateAuthorizationCode()` > 客户端的配置require_proof_key为true时就使用PKCE协议,额外使用三个参数。分别是请求授权码时使用的code_challenge和code_challenge_method和请求令牌时使用的code_verifier。 > > 反之就不使用这三个额外参数 ### 5. 获取令牌 1. 接口地址:http://127.0.0.1:8080/oauth2/token 2. 方法:POST **(multipart/form-data)** 3. 权限:Basic (客户端ID:客户端密码)  4. 示例(Body): `{ "grant_type": "authorization_code", "redirect_uri": "https://cn.bing.com", "code_verifier": "eT3Zhtr7Tmz20-qpTk9zs8EWhN63qdZd8GWiq5-h67TrujxzIg0p_tPUfWH1dXQg278ZEiMcq9ehYPvbBehNe8f4VP4o8EOnFoQY7wVwjUyG_l0ksZUUuPWg5dWKAEth", "code": "c360646f-4f35-44a7-bac0-05a7925d1e5a" }`  > 参数中code是第四步获取的授权码 ### 6. 刷新令牌 1. 接口地址:http://127.0.0.1:8080/oauth2/token 2. 方法:POST **(multipart/form-data)** 3. 权限:Basic (客户端ID:客户端密码)  4. 示例(Body): `{ "grant_type": "refresh_token", "refresh_token": "07236457-6fbb-4115-9402-850865440d77" }`  ### 7. 验证令牌 1. 接口地址:http://127.0.0.1:8080/oauth2/introspect 2. 方法:POST **(multipart/form-data)** 3. 权限:Basic (客户端ID:客户端密码)  4. 示例(Body): `{ "token": "fade5f4b-6817-47bd-9549-2732a83e30c7" }`  ### 8. 吊销令牌(我自定义的) 1. 接口地址:http://127.0.0.1:8080/authorization 2. 方法:DELETE **(none)** 3. 权限:Bearer (令牌) 4. 示例(Auth): `Bearer fade5f4b-6817-47bd-9549-2732a83e30c71`  ## OAuth 2.0 扩展协议之 PKCE 介绍和原理这里不赘述,如果需要时请求授权码和请求令牌时要添加额外参数,上文已讲过使用方法,这里介绍如何生成这一对参数。 1. 使用单元测试生成S256的参数 `com.maoatao.authorization.test.CodeVerifierTest#buildS256Code()` ## 帮助 联系邮箱:maoatao@outlook.com