goweb/route/test.go
2021-08-22 17:23:04 +08:00

41 lines
1.1 KiB
Go

package route
import (
gs "gitee.ltd/go-oauth2/gin-server"
"github.com/gin-gonic/gin"
"goweb/core"
"goweb/middleware"
"net/http"
)
func initTestRoute(r *gin.RouterGroup) {
group := r.Group("/test", middleware.AuthorizeJWT())
group.GET("/pub", func(context *gin.Context) {
core.R(context).OkWithMessage("已登录且无需权限可访问")
})
group.GET("/hello", middleware.AuthorityVerify(), func(context *gin.Context) {
core.R(context).OkWithMessage("已登录且有权限")
})
// 鉴权失败处理函数
errorHandle := func(ctx *gin.Context, err error) {
core.R(ctx).FailWithMessageAndCode("请先登录", http.StatusUnauthorized)
ctx.Abort()
return
}
group1 := r.Group("/test1", gs.HandleTokenVerify(gs.Config{ErrorHandleFunc: errorHandle}))
group1.GET("/pub", func(context *gin.Context) {
ti, _ := context.Get(gs.DefaultConfig.TokenKey)
core.R(context).OkDetailed(ti, "[oauth2]已登录且无需权限可访问")
})
group1.GET("/hello", middleware.AuthorityVerify(), func(context *gin.Context) {
ti, _ := context.Get(gs.DefaultConfig.TokenKey)
core.R(context).OkDetailed(ti, "[oauth2]已登录且有权限")
})
}