如何将JWT设置为持有人授权

发布于 2025-01-29 03:56:56 字数 2286 浏览 4 评论 0原文

我必须调用一个需要承载身份验证的API端点。对于身份验证令牌,我打电话给另一个端点,该端点将返回请求主体中的JWT。我是API的新手,并且有一个问题,如何在请求正文中获得此JWT,并将其映射到Bearer

之后是我对JWT令牌的认证请求请求,

public String getAuthToken() {
   OkHttpClient authClient = new OkHttpClient().newBuilder()
       .build();
     MediaType mediaType = MediaType.parse("application/json");
     RequestBody body = RequestBody.create(mediaType, "{\r\n\"grant_type\": \"password\",\r\n\"username\": \"vmnadmin|rvbsu\",\r\n\"connection\": \"local_account\",\r\n\"password\": \"Pqltuv\",\r\n\"domain\": \"\",\r\n\"auth_domain\": \"\",\r\n\"refresh_token_revoke_unused_in\": 60\r\n  \r\n}");
     Request request = new Request.Builder()
       .url("https://smqp.vpsis.app/api/smv/auth/tokens/")
       .method("POST", body)
       .addHeader("Content-Type", "application/json")
       .build();
     Response response = null;
    try {
        response = authClient.newCall(request).execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

以下是我的补丁方法,它将

           OkHttpClient client = new OkHttpClient().newBuilder()
               .build();
             MediaType mediaType = MediaType.parse("application/json");
             RequestBody body = RequestBody.create(mediaType, "{\r\n  \r\n\"meta\": {\r\n    \"assureId\": \"rmsuv|srv.emsdv\"\r\n  }\r\n\r\n  \r\n}");
             Request request = new Request.Builder()
               .url("https://smqp.vpsis.app/api/smv/file/secure2/newsecure.rvmct")
               .method("PATCH", body)
               .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIwM2ExMjU5NC0zYzFiLTQwOGItYjY5NC01NGE0MzM1ZTEwZTIiLCJzdWIiOiJla21hZG1pbnN8YWRhc2lkYSIsImlzcyI6Imt5bG8iLCJhY2MiOiJreWxvIiwicHJlZmVycmVkX3VzZXJuYW1lIjo")
               .addHeader("Content-Type", "application/json")
               .build();
             try {
                Response response = client.newCall(request).execute();
                  System.out.println(response);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

在第1个方法中获得JWT后更改AssureId,我需要将JWT应用于承载者。我对如何在请求主体中获得此JWT和贴图的方式有一个问题。请让我知道是否有任何有效的方法来调用此请求。

I have to call an api endpoint that requires Bearer authentication. For the authentication token, I called another endpoint that will return a jwt in the request body. I'm new to api and have an issue how to get this jwt available in request body and map to Bearer

Following is my authentication call request for jwt token,

public String getAuthToken() {
   OkHttpClient authClient = new OkHttpClient().newBuilder()
       .build();
     MediaType mediaType = MediaType.parse("application/json");
     RequestBody body = RequestBody.create(mediaType, "{\r\n\"grant_type\": \"password\",\r\n\"username\": \"vmnadmin|rvbsu\",\r\n\"connection\": \"local_account\",\r\n\"password\": \"Pqltuv\",\r\n\"domain\": \"\",\r\n\"auth_domain\": \"\",\r\n\"refresh_token_revoke_unused_in\": 60\r\n  \r\n}");
     Request request = new Request.Builder()
       .url("https://smqp.vpsis.app/api/smv/auth/tokens/")
       .method("POST", body)
       .addHeader("Content-Type", "application/json")
       .build();
     Response response = null;
    try {
        response = authClient.newCall(request).execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Following is my Patch method which will change the assureId

           OkHttpClient client = new OkHttpClient().newBuilder()
               .build();
             MediaType mediaType = MediaType.parse("application/json");
             RequestBody body = RequestBody.create(mediaType, "{\r\n  \r\n\"meta\": {\r\n    \"assureId\": \"rmsuv|srv.emsdv\"\r\n  }\r\n\r\n  \r\n}");
             Request request = new Request.Builder()
               .url("https://smqp.vpsis.app/api/smv/file/secure2/newsecure.rvmct")
               .method("PATCH", body)
               .addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiIwM2ExMjU5NC0zYzFiLTQwOGItYjY5NC01NGE0MzM1ZTEwZTIiLCJzdWIiOiJla21hZG1pbnN8YWRhc2lkYSIsImlzcyI6Imt5bG8iLCJhY2MiOiJreWxvIiwicHJlZmVycmVkX3VzZXJuYW1lIjo")
               .addHeader("Content-Type", "application/json")
               .build();
             try {
                Response response = client.newCall(request).execute();
                  System.out.println(response);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

After getting JWT from 1st method, I need to apply that JWT to Bearer. I have an issue with how to get this jwt available in request body and map to Bearer. Please let me know if there any efficient way to invoke this request.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£噩梦荏苒 2025-02-05 03:56:56

通常响应。getbody是响应本身的表示。您需要获得它的字符串表示形式,并将其与Jackson或Gson的适当模型化。下一步是从模型中提取一个令牌,并将其作为授权标题传递。

Usually response.getBody is a representation of the response itself. You need get a string representation of it and to deserialize it to an appropriate model with Jackson or gson. The next step is to extract a token from the model and pass it as an Authorization header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文