想知道是否有辦法訪問底層 net.Conn 以使用 SO_PEERCRED 檢索用戶憑據并在服務器處理請求之前驗證請求。
從https://blog.jbowen.dev/2019/09/using-so_peercred-in-go/,需要 net.UnixConn 回傳用于驗證的 unix.Ucred。因此,如果服務器請求處理程式有某種方法可以訪問 net.Conn,這應該很容易
我查看了一個 UnaryServerInterceptor,但 UnaryServerInterceptor 中提供的任何內容似乎都沒有包含 net.Conn
func 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)
}
uj5u.com熱心網友回復:
介面方法TransportCredentials.ServerHandshake是您需要的接縫。您的實作可以從輸入中讀取net.Conn并將憑證作為AuthInfo. 然后在您的處理程式代碼中,您可以通過peer.FromContext. 或者,如果您希望在到達處理程式代碼之前進行身份驗證,則可以直接在TransportCredentials.ServerHandshake攔截器中或通過攔截器執行此操作。
另見:https : //groups.google.com/g/grpc-io/c/FeQV7NXpeqA
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/373115.html
