Merge branch 'develop' of git.ailuowan.com:zzw/lone-services into develop
This commit is contained in:
@@ -50,6 +50,8 @@ func main() {
|
||||
|
||||
gw := gateway.MustNewServer(c.GatewayConf,
|
||||
gateway.WithDialer(dialer.Nacos),
|
||||
// 权鉴
|
||||
gateway.WithMiddleware(response.UserReadMiddleware),
|
||||
gateway.WithMiddleware(response.Wrap),
|
||||
)
|
||||
defer gw.Stop()
|
||||
|
||||
Binary file not shown.
@@ -1,6 +1,7 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"bff/internal/response"
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -25,6 +26,7 @@ func Nacos(conf zrpc.RpcClientConf) zrpc.Client {
|
||||
|
||||
return zrpc.MustNewClient(cliConf,
|
||||
zrpc.WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
|
||||
zrpc.WithDialOption(grpc.WithUnaryInterceptor(response.UserClientInterceptor)),
|
||||
zrpc.WithDialOption(grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
|
||||
inst, err := discovery.Pick(serviceName)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
type ctxKey string
|
||||
|
||||
const (
|
||||
CtxUserId ctxKey = "X-User-Id"
|
||||
CtxUserName ctxKey = "X-User-Name"
|
||||
CtxRefresh ctxKey = "X-Refresh"
|
||||
CtxServiceCode ctxKey = "X-Service-Code"
|
||||
)
|
||||
|
||||
type UserInfo struct {
|
||||
ID int64
|
||||
Name string
|
||||
RawUID string
|
||||
Refresh string
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func GetUserInfo(ctx context.Context) UserInfo {
|
||||
rawUID, _ := ctx.Value(CtxUserId).(string)
|
||||
name, _ := ctx.Value(CtxUserName).(string)
|
||||
refresh, _ := ctx.Value(CtxRefresh).(string)
|
||||
|
||||
info := UserInfo{
|
||||
RawUID: rawUID,
|
||||
Name: name,
|
||||
Refresh: refresh,
|
||||
}
|
||||
if rawUID == "" {
|
||||
return info
|
||||
}
|
||||
uid, err := strconv.ParseInt(rawUID, 10, 64)
|
||||
if err != nil {
|
||||
info.Valid = false
|
||||
return info
|
||||
}
|
||||
info.ID = uid
|
||||
info.Valid = true
|
||||
return info
|
||||
}
|
||||
|
||||
func UserReadMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
newCtx := r.Context()
|
||||
newCtx = context.WithValue(newCtx, CtxUserId, r.Header.Get("X-User-Id"))
|
||||
newCtx = context.WithValue(newCtx, CtxUserName, r.Header.Get("X-User-Name"))
|
||||
newCtx = context.WithValue(newCtx, CtxRefresh, r.Header.Get("X-Refresh"))
|
||||
next(w, r.WithContext(newCtx))
|
||||
}
|
||||
}
|
||||
|
||||
func UserClientInterceptor(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||||
user := GetUserInfo(ctx)
|
||||
md := metadata.New(map[string]string{})
|
||||
|
||||
if user.Valid {
|
||||
md.Set("x-user-id", user.RawUID)
|
||||
md.Set("x-user-name", url.QueryEscape(user.Name))
|
||||
}
|
||||
|
||||
if user.Refresh != "" {
|
||||
md.Set("x-refresh", user.Refresh)
|
||||
}
|
||||
|
||||
ctx = metadata.NewOutgoingContext(ctx, md)
|
||||
return invoker(ctx, method, req, reply, cc, opts...)
|
||||
}
|
||||
Reference in New Issue
Block a user