Auth
For the security, only authorized clients can invoke services.
Servers receive requests, first check the auth token. They reject requests with invalid tokens.
Client must set the token:
func main() {
flag.Parse()
d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
option := client.DefaultOption
option.ReadTimeout = 10 * time.Second
defer xclient.Close()
//xclient.Auth("bearer tGzv3JOkF0XG5Qx2TlKWIA")
xclient.Auth("bearer abcdefg1234567")
args := &example.Args{
A: 10,
B: 20,
}
reply := &example.Reply{}
ctx := context.WithValue(context.Background(), share.ReqMetaDataKey, make(map[string]string))
err := xclient.Call(ctx, "Mul", args, reply)
if err != nil {
log.Fatalf("failed to call: %v", err)
}
log.Printf("%d * %d = %d", args.A, args.B, reply.C)
By smallnest updated 2018-12-04 11:47:26