gpt4 book ai didi

java - 如何在java中为每个请求使用JSON Web Token (JWT)?

转载 作者:行者123 更新时间:2023-12-02 09:16:03 26 4
gpt4 key购买 nike

我已经有了 JWT token 和 URL,如下所示:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

网址:

https://196.168.2.85:8443/api/v1/nodes

我想在 Java 中使用带有上述 URL 的 JWT token 来进行后续请求,以获得相应的响应。

最佳答案

这是一个粗略的示例(假设您使用的是 OkHttpClient)

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "message=somemessage");
Request request = new Request.Builder()
.url("https://196.168.2.85:8443/api/v1/nodes")
.post(body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c,Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")
.addHeader("cache-control", "no-cache")
.build();

Response response = client.newCall(request).execute();

但是这实际上取决于网络实现

关于java - 如何在java中为每个请求使用JSON Web Token (JWT)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58979481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com