想知道是否有一種方法可以訪問底層 net.Conn 以使用 SO_PEERCRED 檢索用戶憑據并在服務器處理請求之前對其進行驗證。從https://blog.jbowen.dev/2019/09/using-so_peercred-in-go/,需要 net.UnixConn 返回用于驗證的 unix.Ucred。因此,如果服務器請求處理程序有某種方法可以訪問 net.Conn,這應該很容易我查看了 UnaryServerInterceptor,但 UnaryServerInterceptor 中提供的任何內容似乎都沒有包含 net.Connfunc interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { log.Printf("Intercepted: %+v %+v", info.Server, req) // anything here? return handler(ctx, req)}
1 回答

小怪獸愛吃肉
TA貢獻1852條經驗 獲得超1個贊
接口方法TransportCredentials.ServerHandshake
是您需要的接縫。您的實現可以從輸入中讀取net.Conn
并將憑證作為AuthInfo
. 然后在您的處理程序代碼中,您可以通過peer.FromContext
. 或者,如果您希望在到達處理程序代碼之前進行身份驗證,您可以直接在攔截器中TransportCredentials.ServerHandshake
或通過攔截器進行驗證。
另見:https ://groups.google.com/g/grpc-io/c/FeQV7NXpeqA
- 1 回答
- 0 關注
- 151 瀏覽
添加回答
舉報
0/150
提交
取消