2021-08-20 14:59:13 +08:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2021-08-22 17:23:04 +08:00
|
|
|
gs "gitee.ltd/go-oauth2/gin-server"
|
2021-08-20 14:59:13 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-08-22 00:13:00 +08:00
|
|
|
"goweb/core"
|
|
|
|
"goweb/middleware"
|
2021-08-22 17:23:04 +08:00
|
|
|
"net/http"
|
2021-08-20 14:59:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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("已登录且有权限")
|
|
|
|
})
|
2021-08-22 17:23:04 +08:00
|
|
|
|
|
|
|
// 鉴权失败处理函数
|
|
|
|
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]已登录且有权限")
|
|
|
|
})
|
2021-08-20 14:59:13 +08:00
|
|
|
}
|