回调
This commit is contained in:
+3
-2
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"lone-services/bff/internal/config"
|
||||
"lone-services/bff/internal/dialer"
|
||||
"lone-services/bff/internal/request"
|
||||
"lone-services/bff/internal/response"
|
||||
"lone-services/pkg/discovery"
|
||||
"lone-services/pkg/utils"
|
||||
@@ -55,8 +56,8 @@ func main() {
|
||||
|
||||
gw := gateway.MustNewServer(c.GatewayConf,
|
||||
gateway.WithDialer(dialer.Nacos),
|
||||
// 权鉴
|
||||
gateway.WithMiddleware(response.UserReadMiddleware),
|
||||
// 权鉴+穿透
|
||||
gateway.WithMiddleware(request.UserReadMiddleware),
|
||||
gateway.WithMiddleware(response.Wrap),
|
||||
)
|
||||
defer gw.Stop()
|
||||
|
||||
@@ -3,7 +3,7 @@ package dialer
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"lone-services/bff/internal/response"
|
||||
"lone-services/bff/internal/request"
|
||||
"lone-services/pkg/discovery"
|
||||
"net"
|
||||
"time"
|
||||
@@ -25,7 +25,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.WithUnaryInterceptor(request.UserClientInterceptor)),
|
||||
zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.WaitForReady(true))),
|
||||
zrpc.WithDialOption(grpc.WithConnectParams(grpc.ConnectParams{
|
||||
MinConnectTimeout: 2 * time.Second,
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package response
|
||||
package request
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"lone-services/pkg/utils"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
@@ -14,23 +17,32 @@ import (
|
||||
type ctxKey string
|
||||
|
||||
const (
|
||||
CtxUserId ctxKey = "X-User-Id"
|
||||
CtxUserName ctxKey = "X-User-Name"
|
||||
CtxRefresh ctxKey = "X-Refresh"
|
||||
CtxClientIP ctxKey = "X-Client-Ip"
|
||||
CtxUserAgent ctxKey = "X-User-Agent"
|
||||
CtxClientCode ctxKey = "X-Client-Code"
|
||||
CtxSaleId ctxKey = "X-Sale-Id"
|
||||
CtxSaleName ctxKey = "X-Sale-Name"
|
||||
CtxSaleMobile ctxKey = "X-Sale-Mobile"
|
||||
CtxSaleProvince ctxKey = "X-Sale-Province"
|
||||
CtxStoreId ctxKey = "X-Store-Id"
|
||||
CtxStoreName ctxKey = "X-Store-Name"
|
||||
CtxGroupId ctxKey = "X-Group-Id"
|
||||
CtxGroupName ctxKey = "X-Group-Name"
|
||||
CtxUserType ctxKey = "X-User-Type"
|
||||
CtxUserId ctxKey = "X-User-Id"
|
||||
CtxUserName ctxKey = "X-User-Name"
|
||||
CtxRefresh ctxKey = "X-Refresh"
|
||||
CtxClientIP ctxKey = "X-Client-Ip"
|
||||
CtxUserAgent ctxKey = "X-User-Agent"
|
||||
CtxClientCode ctxKey = "X-Client-Code"
|
||||
CtxSaleId ctxKey = "X-Sale-Id"
|
||||
CtxSaleName ctxKey = "X-Sale-Name"
|
||||
CtxSaleMobile ctxKey = "X-Sale-Mobile"
|
||||
CtxSaleProvince ctxKey = "X-Sale-Province"
|
||||
CtxStoreId ctxKey = "X-Store-Id"
|
||||
CtxStoreName ctxKey = "X-Store-Name"
|
||||
CtxGroupId ctxKey = "X-Group-Id"
|
||||
CtxGroupName ctxKey = "X-Group-Name"
|
||||
CtxUserType ctxKey = "X-User-Type"
|
||||
CtxCallbackRawBody ctxKey = "callback_raw_body"
|
||||
CtxCallbackHeaders ctxKey = "callback_headers"
|
||||
CtxCallbackErr ctxKey = "callback_read_err"
|
||||
)
|
||||
|
||||
var callbackPrefixes = []string{
|
||||
"/api/v3/express/",
|
||||
"/api/v3/other1/",
|
||||
"/api/v3/other2/",
|
||||
}
|
||||
|
||||
type UserInfo struct {
|
||||
ID int64
|
||||
Name string
|
||||
@@ -113,6 +125,26 @@ func getRealClientIP(r *http.Request) string {
|
||||
func UserReadMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
newCtx := r.Context()
|
||||
if isCallbackPath(r.URL.Path) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
newCtx = context.WithValue(newCtx, CtxCallbackRawBody, []byte(nil))
|
||||
newCtx = context.WithValue(newCtx, CtxCallbackHeaders, http.Header(nil))
|
||||
newCtx = context.WithValue(newCtx, CtxCallbackErr, err)
|
||||
} else {
|
||||
r.Body = io.NopCloser(bytes.NewBuffer(body))
|
||||
header := make(http.Header)
|
||||
for k, vv := range r.Header {
|
||||
header[k] = vv
|
||||
}
|
||||
newCtx = context.WithValue(newCtx, CtxCallbackRawBody, body)
|
||||
newCtx = context.WithValue(newCtx, CtxCallbackHeaders, header)
|
||||
newCtx = context.WithValue(newCtx, CtxCallbackErr, error(nil))
|
||||
}
|
||||
next(w, r.WithContext(newCtx))
|
||||
return
|
||||
}
|
||||
|
||||
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"))
|
||||
@@ -128,10 +160,20 @@ func UserReadMiddleware(next http.HandlerFunc) http.HandlerFunc {
|
||||
newCtx = context.WithValue(newCtx, CtxStoreName, r.Header.Get("X-Store-Name"))
|
||||
newCtx = context.WithValue(newCtx, CtxGroupId, r.Header.Get("X-Group-Id"))
|
||||
newCtx = context.WithValue(newCtx, CtxGroupName, r.Header.Get("X-Group-Name"))
|
||||
|
||||
next(w, r.WithContext(newCtx))
|
||||
}
|
||||
}
|
||||
|
||||
func isCallbackPath(path string) bool {
|
||||
for _, prefix := range callbackPrefixes {
|
||||
if strings.HasPrefix(path, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func setMDIfNotEmpty(md metadata.MD, key, value string) {
|
||||
if value != utils.StringEmpty {
|
||||
md.Set(key, value)
|
||||
Reference in New Issue
Block a user