# security-demo **Repository Path**: cch_z/security-demo ## Basic Information - **Project Name**: security-demo - **Description**: security各种授权模式使用,开箱即用,完善中 - **Primary Language**: Java - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-05-26 - **Last Updated**: 2025-06-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: Security, Java, Spring ## README # Spring Security 集成示例 ![Java](https://img.shields.io/badge/Java-17-red) ![Spring Boot](https://img.shields.io/badge/Spring%20Boot-3.2.5-brightgreen) ![Spring Security](https://img.shields.io/badge/Spring%20Security-6.2.2-blue) 这是一个Spring Security集成示例项目,包含多种安全方案实现,帮助开发者快速集成安全功能到Spring Boot应用中。 ## 📦 项目结构 ``` security-all/ ├── security-basic/ -- 基础表单登录认证 ├── security-token/ -- JWT令牌前后端分离方案 └── security-oauth2/ -- OAuth2协议实现 ``` ## 🌟 功能特性 - **security-basic** - 基于Session的表单登录、手机号登录 - 角色权限控制 - CSRF防护 - 记住我功能 - 自定义登录页 - **security-token** - JWT令牌认证 - 无状态身份验证 - 令牌刷新机制 - 黑名单管理 - 前后端分离支持 - **security-oauth2** - OAuth2授权服务器 - 资源服务器配置 - 支持授权码模式、密码模式、客户端模式、手机号验证码登录 - 客户端凭证管理 - Token存储(内存/JDBC) ## 🚀 快速开始 ### 环境要求 - JDK 17+ - Maven 3.6+ - MySQL 8.0+(仅OAuth2模块需要) ### 安装步骤 ```bash git clone https://gitee.com/cch_z/security-demo.git cd security-all mvn clean install ``` ### 模块单独运行 ```bash # 运行基础模块 cd security-basic && mvn spring-boot:run # 运行JWT模块 cd security-token && mvn spring-boot:run # 运行OAuth2模块(需先配置数据库) cd security-oauth2 && mvn spring-boot:run ``` ## 🔧 配置说明 ### JWT模块配置(application.yml) ```yaml jwt: secret-key: your-256-bit-secret expiration: 3600 # 1小时 refresh-expiration: 2592000 # 30天 ``` ### OAuth2数据库配置(schema.sql) ```sql CREATE TABLE oauth2_registered_client ( id varchar(100) NOT NULL, client_id varchar(100) NOT NULL, ... ); ``` ## 📡 示例请求 ### 基础登录 ```bash curl -X POST http://localhost:8888/login \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=user&password=password" ``` ### JWT认证 ```bash # 获取令牌 curl -X POST http://localhost:8888/login/login \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":"admin"}' # 访问受保护资源 curl -H "Authorization: Bearer your-jwt-token" http://localhost:8888/api/protected ``` ### OAuth2授权码流程 1. 访问授权端点: ``` http://localhost:8888/oauth2/authorize?response_type=code&client_id=client&redirect_uri=http://localhost:8888/authorized ``` 2. 使用Token访问资源: ```bash curl -H "Authorization: Bearer your-oauth-token" http://localhost:8888/api/oauth2-resources ``` ## ⚠️ 注意事项 1. 生产环境请务必修改以下配置: - JWT签名密钥 - 数据库连接信息 - OAuth2客户端凭证 - 密码加密盐值 2. Spring Boot 3.x需要JDK17及以上版本 3. OAuth2模块需要提前创建数据库表结构 ## 🤝 贡献指南 欢迎提交Issue和PR,请遵循以下规范: 1. Fork本仓库 2. 创建特性分支(git checkout -b feature/your-feature) 3. 提交修改(git commit -am 'Add some feature') 4. 推送分支(git push origin feature/your-feature) 5. 新建Pull Request ## 📄 许可证 [MIT License](LICENSE) ```