Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

没有服务器端对客户端的鉴权 #34

Open
ztg1 opened this issue Aug 8, 2022 · 0 comments
Open

没有服务器端对客户端的鉴权 #34

ztg1 opened this issue Aug 8, 2022 · 0 comments

Comments

@ztg1
Copy link

ztg1 commented Aug 8, 2022

我想实现客户也有证书,服务器要验证客户端的证书;我在客户端加了证书 服务端做了验证 客户端访问不到 也没有错误;
下面是我的代码;我这样写有问题吗
客户端:
d, err3 := etcd_client.NewEtcdV3Discovery(*basePath1, *serverName, []string{*etcdAddr1}, false, nil)
if err3 != nil {
log.Panicln("err3:", err3)
}
//conf := &tls.Config{
// InsecureSkipVerify: true,
//}
//加入tls
cert, err := tls.LoadX509KeyPair("../tls/client.crt", "../tls/client.key")
if err != nil {
log.Fatal("err:", err)
}
certPool := x509.NewCertPool()
certBytes, eeee := ioutil.ReadFile("../tls/client.crt")
if eeee != nil {
panic("eeeee")
}
if !certPool.AppendCertsFromPEM(certBytes) {
panic("cc")
}
confls := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: certPool,
}

option := client.DefaultOption
option.TLSConfig = confls

xclient := client.NewXClient(*serverName, client.Failover, client.RoundRobin, d, option)
defer xclient.Close()

服务端:
cert, err := tls.LoadX509KeyPair("tls/server.crt", "tls/server.key")
if err != nil {
log.Panicln("tls red err:", err)
}
certPool := x509.NewCertPool()

certBytes, err := ioutil.ReadFile("tls/client.crt")
if err != nil {

	log.Panicln("errrrrrrr:", err)
}
certPool.AppendCertsFromPEM(certBytes)

//lstconfig := &tls.Config{Certificates: []tls.Certificate{cert}}

lstconfig := &tls.Config{
	ClientCAs:    certPool,
	ClientAuth:   tls.RequireAndVerifyClientCert,
	Certificates: []tls.Certificate{cert},
}

//实例化user服务的结构体
ctx := svc.NewServicContext(conf)
svr := servic.NewUserServer(ctx)

s := server.NewServer(server.WithTLSConfig(lstconfig))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant