docs.rs failed to build hiver-security-0.1.0-alpha.6
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
hiver-security
Security framework for Hiver applications
Hiver应用程序的安全框架
📋 Overview / 概述
hiver-security provides comprehensive security features for Hiver applications, including authentication, authorization, and method-level security, similar to Spring Security.
hiver-security 为Hiver应用程序提供全面的安全功能,包括身份验证、授权和方法级安全,类似于Spring Security。
Key Features / 核心特性:
- ✅ Authentication / 身份验证 - User authentication with JWT
- ✅ Authorization / 授权 - Role-based access control
- ✅ Method Security / 方法安全 -
@PreAuthorize,@Secured - ✅ JWT Support / JWT 支持 - JWT token generation and verification
- ✅ Password Encoding / 密码编码 - BCrypt, Argon2
- ✅ Security Context / 安全上下文 - Thread-local security
✨ Features / 特性
| Feature | Spring Equivalent | Description | Status |
|---|---|---|---|
| @PreAuthorize | @PreAuthorize |
Method-level authorization | ✅ |
| @Secured | @Secured |
Role-based security | ✅ |
| JWT | JwtUtil |
JWT token generation and verification | ✅ |
| JwtTokenProvider | JwtTokenProvider |
JWT token provider | ✅ |
| User | UserDetails |
User representation | ✅ |
| Role | GrantedAuthority |
Role/permission | ✅ |
| PasswordEncoder | PasswordEncoder |
Password hashing | ✅ |
| SecurityContext | SecurityContext |
Security context | ✅ |
🚀 Quick Start / 快速开始
Installation / 安装
[]
= "0.1.0-alpha"
= "0.1.0-alpha"
Basic Usage / 基本用法
use ;
use ;
;
JWT Authentication / JWT 认证
use ;
// Create JWT token / 创建 JWT token
let authorities = vec!;
let token = create_token?;
// Verify JWT token / 验证 JWT token
let claims = verify_token?;
// Check expiration / 检查过期
if !claims.is_expired
// Use JwtTokenProvider / 使用 JwtTokenProvider
let provider = new;
let token = provider.generate_token?;
let is_valid = provider.validate_token?;
📖 Security Features / 安全功能
Authentication / 身份验证
use ;
// Authenticate user / 验证用户
let auth_manager = new;
let auth = auth_manager.authenticate.await?;
// Get authenticated user / 获取已认证用户
let user = auth.principal;
Authorization / 授权
use ;
// Expression-based authorization / 基于表达式的授权
async
// Role-based authorization / 基于角色的授权
async
Password Encoding / 密码编码
use PasswordEncoder;
let encoder = bcrypt; // BCrypt with cost 10
// Encode password / 编码密码
let encoded = encoder.encode?;
// Verify password / 验证密码
let is_valid = encoder.matches?;
JWT Authentication Flow / JWT 认证流程
use ;
// 1. User login / 用户登录
let auth_manager = new;
let auth = auth_manager.authenticate.await?;
// 2. Generate JWT token / 生成 JWT token
let token = create_token?;
// 3. Return token to client / 将 token 返回给客户端
println!;
// 4. Client includes token in subsequent requests / 客户端在后续请求中包含 token
// Authorization: Bearer <token>
// 5. Verify token on subsequent requests / 在后续请求中验证 token
let claims = verify_token?;
if claims.has_role
🚦 Roadmap / 路线图
Phase 3: Core Security ✅ (Completed / 已完成)
- Authentication
- Authorization
- Method security
- Password encoding
Phase 4: JWT & Advanced Features ✅ (Completed / 已完成)
- JWT support (token generation, verification, refresh)
- JWT authentication middleware
- JWT claims and authorities
- OAuth2 (planned)
- Session management (planned)
- CSRF protection (planned)
📚 Documentation / 文档
- API Documentation: docs.rs/hiver-security
- Book: Security Guide
Built with ❤️ for application security
为应用程序安全构建 ❤️