我已经有了JWT令牌和URL,如下所示:
。
URL:
我想将JWT令牌与上述URL一起用于Java中的后续请求,以获取相应的响应。
答案 0 :(得分:1)
希望您能提供更多详细信息,以便我提出确切的实现建议。
您必须在请求标头中添加JWT才能在所有后续请求中获得响应。获得JWT后,您可以将其存储在会话中,并在随后的所有请求中使用它。
我正在使用rest模板,您可以在客户端中遵循相同的模板
String url ="https://196.168.2.85:8443/api/v1/nodes";
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON}));
headers.set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKK");
HttpEntity<String> httpEntity = new HttpEntity<>(requestBody,headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
答案 1 :(得分:0)
这是一个粗略的示例(假设您使用的是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();
)
{{1}}
但这实际上取决于Web实施