Golang中如何实现GoBuffalo与Authboss(认证系统)的集成
Golang中如何实现GoBuffalo与Authboss(认证系统)的集成
我正在尝试将 authboss(https://github.com/volatiletech/authboss)与 GoBuffalo(https://github.com/gobuffalo/buffalo)结合使用。
我仍在学习Go语言,我认为这是一个很好的练习。
我创建了一个新的GoBuffalo应用程序,你可以在这里找到:https://github.com/frederikhors/buffalo-authboss-sample
重现步骤
git clone https://github.com/frederikhors/buffalo-authboss-sample- 进入项目根目录
go get -u -v github.com/gobuffalo/buffalo/buffalogo mod tidybuffalo dev
如你所见,新的buffalo应用程序是最简化的(所有功能都被注释掉了)。
我从这个示例仓库复制了一小部分指令:https://github.com/volatiletech/authboss-sample
应用程序启动了,但我无法打开 http://localhost:3000/auth/login(404页面未找到)。
使用 app.Mount("/auth", http.StripPrefix("/auth", ab.Config.Core.Router)) 创建了路由,你可以在仪表板(http://localhost:3000/)或下面的屏幕截图中看到。
Buffalo路由:
GET /auth/{path:.+}/ authPath github.com/gobuffalo/buffalo.WrapHandler.func1
PUT /auth/{path:.+}/ authPath github.com/gobuffalo/buffalo.WrapHandler.func1
PATCH /auth/{path:.+}/ authPath github.com/gobuffalo/buffalo.WrapHandler.func1
HEAD /auth/{path:.+}/ authPath github.com/gobuffalo/buffalo.WrapHandler.func1
OPTIONS /auth/{path:.+}/ authPath github.com/gobuffalo/buffalo.WrapHandler.func1
DELETE /auth/{path:.+}/ authPath github.com/gobuffalo/buffalo.WrapHandler.func1
POST /auth/{path:.+}/ authPath github.com/gobuffalo/buffalo.WrapHandler.func1
我无法理解如何告诉Buffalo为这些路由渲染authboss视图。
更多关于Golang中如何实现GoBuffalo与Authboss(认证系统)的集成的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于Golang中如何实现GoBuffalo与Authboss(认证系统)的集成的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
在GoBuffalo中集成Authboss时,需要确保Authboss的路由和视图正确配置。问题在于你使用了http.StripPrefix,这可能导致路径不匹配,同时Buffalo的视图渲染机制需要明确设置。以下是完整的集成示例:
首先,在actions/app.go中配置Authboss,并确保路由和视图正确挂载:
package actions
import (
"net/http"
"github.com/gobuffalo/buffalo"
"github.com/volatiletech/authboss/v3"
aboauth "github.com/volatiletech/authboss/v3/oauth2"
)
var ab *authboss.Authboss
func initAuthboss(app *buffalo.App) error {
ab = authboss.New()
ab.Config.Paths.RootURL = "http://localhost:3000"
// 配置存储(使用内存存储作为示例,生产环境需替换为数据库)
ab.Config.Storage.Server = authboss.NewMemoryStorer()
ab.Config.Storage.SessionState = authboss.NewMemoryStorer()
ab.Config.Storage.CookieState = authboss.NewMemoryStorer()
// 配置核心设置
ab.Config.Core.ViewRenderer = &BuffaloRenderer{app: app}
ab.Config.Core.MailRenderer = ab.MailRenderer{}
// 加载模块
if err := ab.LoadOAuth2(aboauth.GitHub); err != nil {
return err
}
// 初始化Authboss
if err := ab.Init(); err != nil {
return err
}
// 挂载Authboss路由到Buffalo,不使用StripPrefix
app.Any("/auth/{path:.+}", buffalo.WrapHandler(ab.Config.Core.Router))
return nil
}
// BuffaloRenderer 实现Authboss的ViewRenderer接口
type BuffaloRenderer struct {
app *buffalo.App
}
func (b *BuffaloRenderer) Render(ctx context.Context, name string, data authboss.HTMLData) ([]byte, string, error) {
// 将authboss.HTMLData转换为buffalo.Data
buffData := buffalo.Data{}
for k, v := range data {
buffData[k] = v
}
// 使用Buffalo的渲染器渲染视图
r := b.app.NewRendererWithContext(ctx)
r.Set("data", buffData)
return r.RenderHTML(name + ".html")
}
// 在App()函数中调用initAuthboss
func App() *buffalo.App {
if app != nil {
return app
}
app = buffalo.New(buffalo.Options{
Env: ENV,
SessionName: "_buffalo-authboss-sample_session",
})
// 初始化Authboss
if err := initAuthboss(app); err != nil {
app.Logger.Fatal(err)
}
app.GET("/", HomeHandler)
return app
}
创建对应的视图文件。在templates目录下添加login.html:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
{{ if .data.error }}
<div style="color: red;">{{ .data.error }}</div>
{{ end }}
<form method="POST" action="/auth/login">
<div>
<label>Email:</label>
<input type="email" name="email" required>
</div>
<div>
<label>Password:</label>
<input type="password" name="password" required>
</div>
<button type="submit">Login</button>
</form>
</body>
</html>
在actions/home.go中添加一个简单的首页处理器:
package actions
import (
"github.com/gobuffalo/buffalo"
)
func HomeHandler(c buffalo.Context) error {
return c.Render(200, r.HTML("home.html"))
}
创建templates/home.html:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome to Buffalo with Authboss</h1>
<p><a href="/auth/login">Login</a></p>
</body>
</html>
关键点:
- 使用
app.Any("/auth/{path:.+}", buffalo.WrapHandler(ab.Config.Core.Router))直接挂载Authboss路由,避免http.StripPrefix导致的路径问题。 - 实现
BuffaloRenderer来桥接Authboss的视图渲染和Buffalo的模板系统。 - 确保视图文件位于正确路径(如
templates/login.html),Authboss默认会查找这些视图。
运行buffalo dev后,访问http://localhost:3000/auth/login应能正确显示登录页面。如果仍有404错误,检查Authboss配置的根URL和路径是否匹配。

