Auth

For the security, only authorized clients can invoke services.

Clients must set an authorization token which can gets from OAuth/OAuth2 or granted access token.

Server must define a AuthFunc to validate auth token. In the above example, only requests with token are valid.

Client must set the token:

  1. func main() {
  2. flag.Parse()
  3. d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
  4. option := client.DefaultOption
  5. option.ReadTimeout = 10 * time.Second
  6. //xclient.Auth("bearer tGzv3JOkF0XG5Qx2TlKWIA")
  7. xclient.Auth("bearer abcdefg1234567")
  8. args := &example.Args{
  9. A: 10,
  10. B: 20,
  11. }
  12. ctx := context.WithValue(context.Background(), share.ReqMetaDataKey, make(map[string]string))
  13. err := xclient.Call(ctx, "Mul", args, reply)
  14. if err != nil {
  15. log.Fatalf("failed to call: %v", err)
  16. }
  17. log.Printf("%d * %d = %d", args.A, args.B, reply.C)

By smallnest updated 2018-03-27 11:12:10

原文: