# oauth2-jwe-examples **Repository Path**: marsdonne/oauth2-jwe-examples ## Basic Information - **Project Name**: oauth2-jwe-examples - **Description**: 基于spring-security 7.0和kotlin整理的oauth2示例代码,包括用户认证和客户端授权分离。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-28 - **Last Updated**: 2026-02-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # OAuth2 JWE 示例项目 本项目演示了如何在 Spring Security OAuth2 Authorization Server 中实现 JSON Web Encryption (JWE) 加密,以及资源服务器如何解密和验证加密的令牌。 ## 项目结构 ``` oauth2-jwe-example/ ├── build.gradle.kts # 根构建配置 ├── settings.gradle.kts # 项目设置 ├── gradle/ # Gradle 包装器 ├── simple-auth-server/ # 简单 OAuth2 授权服务器 ├── scoped-auth-server/ # 带 Redis 会话管理的授权服务器 ├── scoped-auth-client/ # 授权客户端应用 ├── res-server-api/ # OAuth2 资源服务器 └── res-server-api-test/ # 资源服务器测试配置 ``` ## 模块说明 ### simple-auth-server 基础 OAuth2 授权服务器实现,包含: - JWK (JSON Web Key) 配置 - JWE 加密/解密支持 - JWT Token 自定义 - 用户注册与管理 - 支持授权码流程和客户端凭证流程 ### scoped-auth-server 增强版授权服务器,在 simple-auth-server 基础上增加: - Redis 会话存储 - 设备注册与认证 - 跨会话的认证状态共享 - 自定义认证 Provider ### scoped-auth-client OAuth2 客户端应用,演示: - 授权码流程集成 - 设备认证 - Redis 会话管理 - 与 scoped-auth-server 集成 ### res-server-api OAuth2 资源服务器,提供: - JWE 令牌解密 - JWT 验证 - 保护 REST API - 支持 GraphQL 端点 ## 主要特性 - **JWE 加密**:使用 RSA-OAEP 算法对 JWT 进行加密 - **PKCE 支持**:授权码流程中的代码交换密钥支持 - **Testcontainers**:使用容器化 PostgreSQL 和 Redis 进行测试 - **数据库迁移**:使用 Flyway 管理数据库 schema - **全面测试**:每个模块都包含集成测试 ## 技术栈 - Kotlin - Spring Boot 3.x - Spring Security OAuth2 Authorization Server - Spring Data JPA - Redis (Lettuce) - PostgreSQL - Gradle - JUnit 5 ## 快速开始 ### 环境要求 - JDK 17 或更高版本 - Docker 和 Docker Compose (用于运行测试容器) ### 构建项目 ```bash # 克隆项目 git clone https://gitee.com/marsdonne/oauth2-jwe-example.git cd oauth2-jwe-example # 构建所有模块 ./gradlew build # 运行测试 ./gradlew test ``` ### 运行各模块 **简单授权服务器:** ```bash cd simple-auth-server ../gradlew bootRun ``` **带作用域的授权服务器:** ```bash cd scoped-auth-server ../gradlew bootRun ``` **资源服务器:** ```bash cd res-server-api ../gradlew bootRun ``` ## 配置说明 ### 授权服务器配置 (application.yaml) ```yaml server: port: 8080 spring: datasource: url: jdbc:postgresql://localhost:5432/oauth2 username: postgres password: postgres redis: host: localhost port: 6379 oauth2: authorization-server: jwk-set-uri: http://localhost:8080/oauth2/jwks jwe-decrypt-key-location: keys/private.key ``` ### 资源服务器配置 ```yaml oauth2: resource-server: enabled: true jwk-set-uri: http://localhost:8080/oauth2/jwks jwe-decrypt-key-location: keys/private.key permit-all-paths: /public/**,/health,/info ``` ## 测试 项目使用 Testcontainers 运行集成测试,测试前会自动启动 PostgreSQL 和 Redis 容器。 ```bash # 运行所有测试 ./gradlew test # 运行特定模块的测试 ./gradlew :simple-auth-server:test ./gradlew :scoped-auth-server:test ./gradlew :res-server-api:test ``` ## API 端点 ### 授权服务器端点 | 端点 | 方法 | 描述 | |------|------|------| | `/oauth2/authorize` | GET | 授权请求入口 | | `/oauth2/token` | POST | 获取访问令牌 | | `/oauth2/jwks` | GET | JSON Web Key Set | | `/oauth2/introspect` | POST | 令牌自省 | | `/oauth2/revoke` | POST | 令牌撤销 | ### 资源服务器端点 | 端点 | 方法 | 描述 | |------|------|------| | `/api/data` | GET | 受保护的 GET 端点 | | `/api/data` | POST | 受保护的 POST 端点 | | `/graphql` | POST | GraphQL 端点 | ## 许可证 本项目采用 MIT 许可证。