security - GStones/moke-kit GitHub Wiki
本文档介绍了moke-kit 提供的两种安全机制:基于服务的MTls认证和基于用户的token认证机制
- 把相关的证书放到
./configs
目录下面,当然也可以通过环境变量指定证书的路径。具体参考 - 设置环境变量
TLS_ENABLED=true
来启用Grpc的TLS认证。 - 设置环境变量
MTLS_ENABLE=true
来启用Grpc的mTLS认证,注意:TLS_ENABLED=true
也需要设置。 - 设置环境变量
TCP_TLS_ENABLE=true
来启用Zinx的TLS认证。 - 证书文件如果发生变化, moke-kit 会自动重新加载证书文件,推荐用cert-manager管理证书。
基于Token的认证(Grpc)
- 可以自定义实现IAuthMiddleware
注入到对应的
Module
中,moke-kit会自动为每个rpc或者stream添加认证中间件。实现参考 - 当然你可以为指定
Service
忽略认证,在Service struct
中匿名嵌入utility.WithoutAuth即可。type Service struct { utility.WithoutAuth }
- 如果你只想忽略
Service
中指定的Handler
,只需要在Service
中注入sfx.AuthMiddlewareParams ,使用siface.IAuthMiddleware
中的AddUnAuthMethod
方法添加对应的Handler FullMethodName
即可。s.authMiddleware.AddUnAuthMethod(pb.ProfileService_GetProfileBasics_FullMethodName)