GoWind 开源生态GoWind 开源生态
首页
框架
GoWind Admin
GoWind CMS
GoWind IM
GoWind UBA
GoWind IoT
GoWind Toolkit
GoWind Quant
GitHub
首页
框架
GoWind Admin
GoWind CMS
GoWind IM
GoWind UBA
GoWind IoT
GoWind Toolkit
GoWind Quant
GitHub
  • 介绍

    • GoWind 框架
    • 框架整体架构
  • go-wind 核心

    • go-wind 核心框架
    • App 生命周期管理
    • Context 传播
    • Transport 抽象
    • Log 门面接口
  • go-wind-plugins 插件

    • go-wind-plugins 插件总览
    • 插件配置系统
    • 插件注册机制(SPI)
    • 日志适配插件
    • 传输协议插件
    • 消息中间件插件
    • 编码解码插件
    • 安全与认证插件
    • 链路追踪插件
    • 缓存插件
    • 对象存储插件(OSS)
    • 限流插件
    • 指标监控插件
    • AI 插件
    • 工作流插件
    • 数据库与缓存插件
    • Bootstrap 集成与网关插件
    • 压缩与序列化插件
    • 模板渲染与验证插件
  • go-wind-bootstrap 启动器

    • go-wind-bootstrap 声明式启动器
    • Bootstrap 配置系统
    • Bootstrap SPI 机制
    • 声明式中间件编排
    • Bootstrap CLI 工具
    • Bootstrap 实战示例
  • 教程

    • 快速入门教程
    • 自定义插件开发教程
    • 多协议同时监听教程
    • 框架迁移指南

安全与认证插件

go-wind-plugins 提供认证、授权和安全相关的适配器,支持 JWT、OAuth2、Casbin 等。

一、认证适配器

1.1 JWT

import jwtPlugin "github.com/tx7do/go-wind-plugins/auth/jwt"

authenticator := jwtPlugin.New(
    jwtPlugin.WithSigningMethod("HS256"),
    jwtPlugin.WithKey("my-secret-key"),
    jwtPlugin.WithExpiry(24*time.Hour),
    jwtPlugin.WithIssuer("my-service"),
)

签发 Token

claims := map[string]interface{}{
    "user_id":  "123",
    "username": "alice",
    "roles":    []string{"admin"},
}

token, _ := authenticator.Generate(claims)

验证 Token

claims, err := authenticator.Verify(tokenString)
if err != nil {
    // 无效或过期的 token
}

YAML 配置

auth:
  jwt:
    signing_method: HS256        # HS256 | RS256 | ES256
    key: ${JWT_SECRET}
    public_key_file: certs/public.pem   # RS256/ES256
    private_key_file: certs/private.pem
    expiry: 24h
    refresh_expiry: 168h         # 7 days
    issuer: my-service

1.2 OAuth2

import oauth2Plugin "github.com/tx7do/go-wind-plugins/auth/oauth2"

oauth2Server := oauth2Plugin.New(
    oauth2Plugin.WithClientStore(clientStore),
    oauth2Plugin.WithTokenStore(tokenStore),
    oauth2Plugin.WithAccessExp(3600),     // 1 hour
    oauth2Plugin.WithRefreshExp(604800),  // 7 days
)

支持的 Grant Type

Grant Type说明
authorization_code授权码模式
client_credentials客户端凭证
password密码模式
refresh_token刷新令牌

二、授权适配器

2.1 Casbin RBAC

import casbinPlugin "github.com/tx7do/go-wind-plugins/auth/casbin"

enforcer, _ := casbinPlugin.New(
    casbinPlugin.WithModelFile("rbac_model.conf"),
    casbinPlugin.WithPolicyAdapter(policyAdapter),
    casbinPlugin.WithAutoSave(true),
)

权限模型

# rbac_model.conf
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where, p.eft == allow)

[matchers]
m = g(r.sub, p.sub) && keyMatch2(r.obj, p.obj) && r.act == p.act

权限检查

// 检查用户是否有权限
allowed, _ := enforcer.Enforce("alice", "/api/users/:id", "GET")

// 添加角色
enforcer.AddRoleForUser("alice", "admin")

// 添加策略
enforcer.AddPolicy("admin", "/api/users/*", "GET")
enforcer.AddPolicy("admin", "/api/users/*", "POST")

YAML 配置

auth:
  casbin:
    model_file: configs/rbac_model.conf
    policy:
      driver: mysql          # file | mysql | postgres | redis
      dsn: "user:pass@tcp(localhost:3306)/casbin"
    auto_save: true
    auto_build: true

2.2 中间件集成

// JWT 认证中间件
authMiddleware := jwtPlugin.Middleware(
    jwtPlugin.WithSkipper(func(r *http.Request) bool {
        return r.URL.Path == "/login" || r.URL.Path == "/register"
    }),
)

// Casbin 授权中间件
authzMiddleware := casbinPlugin.Middleware(enforcer)

// 组合使用
router.Use(authMiddleware, authzMiddleware)

三、密码安全

3.1 BCrypt

import bcryptPlugin "github.com/tx7do/go-wind-plugins/auth/bcrypt"

// 哈希密码
hashed, _ := bcryptPlugin.Hash("mypassword", 10)  // cost = 10

// 验证密码
err := bcryptPlugin.Compare(hashed, "mypassword")  // nil = 匹配

3.2 Argon2

import argon2Plugin "github.com/tx7do/go-wind-plugins/auth/argon2"

hashed, _ := argon2Plugin.Hash("mypassword", argon2Plugin.WithTime(3),
    argon2Plugin.WithMemory(64*1024), argon2Plugin.WithParallelism(2))

四、TLS/SSL

// HTTP TLS
httpServer := httpPlugin.NewServer(
    httpPlugin.WithAddr(":8443"),
    httpPlugin.WithTLS("cert.pem", "key.pem"),
)

// gRPC TLS
grpcServer := grpcPlugin.NewServer(
    grpcPlugin.WithAddr(":9443"),
    grpcPlugin.WithTLS("cert.pem", "key.pem"),
)

五、CORS

httpServer := httpPlugin.NewServer(
    httpPlugin.WithAddr(":8080"),
    httpPlugin.WithCORS(true, []string{"https://app.example.com"}),
)
server:
  http:
    cors:
      enabled: true
      origins:
        - https://app.example.com
        - https://admin.example.com
      methods: [GET, POST, PUT, DELETE, OPTIONS]
      headers: [Authorization, Content-Type, X-Trace-ID]
      credentials: true
      max_age: 86400

六、限流安全

import ratelimitPlugin "github.com/tx7do/go-wind-plugins/ratelimit/tokenbucket"

limiter := ratelimitPlugin.New(
    ratelimitPlugin.WithRate(100),       // 100 requests
    ratelimitPlugin.WithBurst(50),       // burst 50
    ratelimitPlugin.WithPer("minute"),   // per minute
)

相关文档

  • 插件配置系统
  • 限流插件
  • Bootstrap 中间件编排
Edit this page
Last Updated:: 6/21/26, 9:28 PM
Contributors: Bobo
Prev
编码解码插件
Next
链路追踪插件