Explorar o código

完成基本鉴权领域导入

yjp hai 1 ano
pai
achega
b7424c456e

+ 1 - 0
go.mod

@@ -25,6 +25,7 @@ require (
 	github.com/cloudwego/base64x v0.1.4 // indirect
 	github.com/cloudwego/iasm v0.2.0 // indirect
 	github.com/davecgh/go-spew v1.1.1 // indirect
+	github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
 	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
 	github.com/gabriel-vasile/mimetype v1.4.3 // indirect
 	github.com/gin-contrib/sse v0.1.0 // indirect

+ 2 - 0
go.sum

@@ -35,6 +35,8 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
+github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
 github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
 github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
 github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=

+ 3 - 1
project/mono/application/application.go

@@ -19,7 +19,9 @@ func NewApp() {
 	appInstance = baize.NewApplication(config.GetConfig().ApplicationConfig)
 
 	// 注册Router
-	appInstance.Api().PrefixRouter().RegisterVersionedRouter("v1")
+	appInstance.Api().PrefixRouter().RegisterVersionedRouter("v1").
+		AddMiddlewares(service.AuthMiddleware(appInstance.Infrastructure()))
+
 }
 
 func DestroyApp() {

+ 43 - 0
project/mono/application/service/auth.go

@@ -0,0 +1,43 @@
+package service
+
+import (
+	"git.sxidc.com/go-framework/baize/convenient/domain/auth"
+	"git.sxidc.com/go-framework/baize/convenient/domain/auth/middlewares"
+	"git.sxidc.com/go-framework/baize/framework/binding"
+	"git.sxidc.com/go-framework/baize/framework/core/api"
+	"git.sxidc.com/go-framework/baize/framework/core/application"
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
+)
+
+func AuthMiddleware(i *infrastructure.Infrastructure) api.Handler {
+	return func(c *api.Context) {
+		middlewares.Authentication(dbSchema, jwtSecretKey)(c, i)
+	}
+}
+
+var authService = &AuthService{}
+
+type AuthService struct{}
+
+func (svc *AuthService) Init(appInstance *application.App) error {
+	// TODO 初始化超管用户
+
+	svc.prefixRoot(appInstance)
+	return nil
+}
+
+func (svc *AuthService) Destroy() error {
+	return nil
+}
+
+func (svc *AuthService) prefixRoot(appInstance *application.App) {
+	prefixRootBinder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, ""), appInstance.Infrastructure())
+
+	auth.BindAuth(prefixRootBinder, &auth.Simple{
+		Schema:         dbSchema,
+		AESKey:         aesKey,
+		JWTSecretKey:   jwtSecretKey,
+		JWTExpiredSec:  24 * 3600,
+		AuthMiddleware: middlewares.Authentication(dbSchema, jwtSecretKey),
+	})
+}

+ 9 - 0
project/mono/application/service/service.go

@@ -6,6 +6,14 @@ const (
 	dbSchema = "test"
 )
 
+const (
+	jwtSecretKey = "f0s351lanchdtx"
+)
+
+const (
+	aesKey = "@MKU^*HF%p&KIGT5<TAHCVD#$XZSWQ@L"
+)
+
 type Service interface {
 	Init(appInstance *application.App) error
 	Destroy() error
@@ -13,5 +21,6 @@ type Service interface {
 
 var RegisteredServices = []Service{
 	versionService,
+	authService,
 	classService,
 }