

? goal 的用户认证(Auth)模块完成,离生产环境又双叒叕近了一步!
source link: https://www.v2ex.com/t/831820
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Goal-web/auth
goal-web/auth
goal 框架的用户认证组件,你可以在 http
、websocket
或者其他环境使用,有上下文即可。
安装 - install
go get github.com/goal-web/auth
使用 - usage
goal 的脚手架自带了绝大多数开发一个 web 应用的所需要的功能和组件,当然这也包括了认证组件。一般情况下,我们只需要在 .env 修改自己的认证配置即可,比如 jwt
驱动的 secret 、session
驱动的 session_key 。
配置 - config
默认情况下,config/auth.go
配置文件像下面那样,默认添加了 jwt
、session
两个守卫配置
package config
import (
"github.com/goal-web/auth"
"github.com/goal-web/contracts"
"github.com/goal-web/example/models"
"github.com/golang-jwt/jwt"
)
func init() {
configs["auth"] = func(env contracts.Env) interface{} {
return auth.Config{
Defaults: struct {
Guard string
User string
}{
Guard: env.StringOption("auth.default", "jwt"), // 默认守卫
User: env.StringOption("auth.user", "db"), // 默认用户提供者
},
Guards: map[string]contracts.Fields{
"jwt": { // 守卫名称
"driver": "jwt", // 驱动,目前支持 jwt 、session
"secret": env.GetString("auth.jwt.secret"), // jwt 签名所需的 secret ,不同的守卫建议不同的 secret
"method": jwt.SigningMethodHS256, // jwt 签名方法
"lifetime": 60 * 60 * 24, // token 有效时长,单位:秒
"provider": "db", // 用户提供者名
},
"session": { // 守卫名称
"driver": "session", // 驱动名
"provider": "db", // 用户提供者名
// session 驱动所需的参数,如果应用需要配置多个 session 驱动的守卫,那么需要配置不一样的 session_key
"session_key": env.StringOption("auth.session.key", "auth_session"),
},
},
Users: map[string]contracts.Fields{ // 用户提供者,目前只支持 db
"db": { // 用户提供者名称
"driver": "db", // 驱动名称
"model": models.UserModel, // 用户模型
},
},
}
}
}
.env
的数据库相关配置
# 默认连接
auth.jwt.secret=jwt_secret
auth.default=jwt
定义模型 - define a model
app/models/user.go
文件
package models
import (
"github.com/goal-web/database/table"
"github.com/goal-web/supports/class"
)
var (
UserModel = table.NewModel(class.Make(new(User)), "users")
)
func UserQuery() *table.Table {
return table.FromModel(UserModel)
}
type User struct {
Id string `json:"id"`
NickName string `json:"name"`
}
// GetId 实现了 contracts.Authenticatable 接口,此方法必不可少
func (u User) GetId() string {
return u.Id
}
用法 - method of use
package controllers
import (
"github.com/goal-web/contracts"
"github.com/goal-web/example/models"
)
func LoginExample(guard contracts.Guard) contracts.Fields {
// 这是伪代码
user := models.UserQuery().First().(models.User)
return contracts.Fields{
"token": guard.Login(user), // jwt 返回 token ,session 返回 true
}
}
func GetCurrentUser(guard contracts.Guard) interface{} {
return contracts.Fields{
"user": guard.User(), // 已登录返回用户模型,否则返回 nil
}
}
使用中间件
package routes
import (
"github.com/goal-web/auth"
"github.com/goal-web/contracts"
"github.com/goal-web/example/app/http/controllers"
"github.com/goal-web/session"
)
func ApiRoutes(router contracts.Router) {
v1 := router.Group("", session.StartSession)
// 直接应用在路由上
v1.Get("/myself", controllers.GetCurrentUser, auth.Guard("jwt"))
// 应用在路由组上
authRouter := v1.Group("", auth.Guard("jwt"))
authRouter.Get("/myself", controllers.GetCurrentUser, auth.Guard("jwt"))
}
守卫 API - guard api
type Guard interface {
Once(user Authenticatable)
User() Authenticatable
GetId() string
Check() bool
Guest() bool
Login(user Authenticatable) interface{}
}
扩展守卫和用户提供者 - extension
这部分内容比较多,这里暂时不展开讲,后面会专门录视频介绍,欢迎大家点赞订阅
在 goal 之外的框架使用 - use in frameworks other than goal
这部分内容比较多,这里暂时不展开讲,后面会专门录视频介绍,欢迎大家点赞订阅
Recommend
-
92
生产环境中持续集成平台的实践 Original...
-
69
全球工单系统 - @gcg0036 - 前几天操作 edas 时,莫名其妙,把我们俩主机直接释放了,毛都没剩,手机验证码也没,甚至都没个弹框确认 后来发工单,客服确认是阿里云代码 bug,然后说送我 500 代金券??
-
111
redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。生产中我们主要用来存储用户的登录信息,设备的详情数据,以及会员签到点赞的信息等等。下面来记录下生产中redis的备份与恢复。提到redis备份,就不得不提及redis的...
-
41
-
8
Django基础八之认证模块---auth 1. auth介绍 Django 自带一个用户验证系统。它负责处理用户账号、组、权限和基于cookie的用户会话。 认证系统由以下部分组成: 权限:二进制(是/否)标识指...
-
5
通过软件密码模块最高认证标准!360安全浏览器喜获商用密码产品二级证书 – 智汇云技术社区 近日,360安全浏览器获得国家密码管理局商用密码检测中心颁发的《商用密码产品认证证书》,证书等级为二级。此次360安全浏览器的商用密码资质...
-
4
消息称苹果神秘网卡获认证 由比亚迪生产
-
10
Django框架 一、csrf跨站请求伪造 csrf是django默认中间件中的一道,它用于验证网页的真伪性,通过在在前端页面设置csrf_token令牌,这样转递到后端的信息将会在csrf中间...
-
2
用Abp实现双因素认证(Two-Factor Authentication, 2FA)登录(一):认证模块 ...
-
8
Joby 完成预生产 eVTOL 测试,进入生产原型机飞行认证2024/05/04 17:45|By
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK