You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.1 KiB
40 lines
1.1 KiB
package auth_test
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/go-kratos/kratos/example/blademaster/middleware/auth"
|
|
bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
|
|
"github.com/go-kratos/kratos/pkg/net/metadata"
|
|
)
|
|
|
|
// This example create a identify middleware instance and attach to several path,
|
|
// it will validate request by specified policy and put extra information into context. e.g., `mid`.
|
|
// It provides additional handler functions to provide the identification for your business handler.
|
|
func Example() {
|
|
myHandler := func(ctx *bm.Context) {
|
|
mid := metadata.Int64(ctx, metadata.Mid)
|
|
ctx.JSON(fmt.Sprintf("%d", mid), nil)
|
|
}
|
|
|
|
authn := auth.New(&auth.Config{
|
|
DisableCSRF: false,
|
|
})
|
|
|
|
e := bm.DefaultServer(nil)
|
|
|
|
// mark `/user` path as User policy
|
|
e.GET("/user", authn.User, myHandler)
|
|
// mark `/mobile` path as UserMobile policy
|
|
e.GET("/mobile", authn.UserMobile, myHandler)
|
|
// mark `/web` path as UserWeb policy
|
|
e.GET("/web", authn.UserWeb, myHandler)
|
|
// mark `/guest` path as Guest policy
|
|
e.GET("/guest", authn.Guest, myHandler)
|
|
|
|
o := e.Group("/owner", authn.User)
|
|
o.GET("/info", myHandler)
|
|
o.POST("/modify", myHandler)
|
|
|
|
e.Start()
|
|
}
|
|
|