Gri*_*lik 1 ssl-certificate go mutual-authentication grpc tls1.2
我正在使用带有相互 tls 的 Golang gRPC。是否可以从 rpc 方法获取客户端的证书主题 DN?
// ...
func main() {
// ...
creds := credentials.NewTLS(&tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: []tls.Certificate{certificate},
ClientCAs: certPool,
MinVersion: tsl.VersionTLS12,
})
s := NewMyService()
gs := grpc.NewServer(grpc.Creds(creds))
RegisterGRPCZmqProxyServer(gs, s)
er := gs.Serve(lis)
// ...
}
// ...
func (s *myService) Foo(ctx context.Context, req *FooRequest) (*FooResonse, error) {
$dn := // What should be here?
// ...
}
Run Code Online (Sandbox Code Playgroud)
是否可以?
小智 5
您可以使用peer.Peerfromctx context.Context来访问 中的 OID 注册表x509.Certificate。
func (s *myService) Foo(ctx context.Context, req *FooRequest) (*FooResonse, error) {
p, ok := peer.FromContext(ctx)
if ok {
tlsInfo := p.AuthInfo.(credentials.TLSInfo)
subject := tlsInfo.State.VerifiedChains[0][0].Subject
// do something ...
}
}
Run Code Online (Sandbox Code Playgroud)
主题是pkix.Name并在文档中写道:
名称代表 X.509 专有名称
我使用了这个答案中的代码并且效果很好。
| 归档时间: |
|
| 查看次数: |
1450 次 |
| 最近记录: |