REST请求到AWS来自Quarkus后端

huangapple go评论56阅读模式
英文:

REST request to AWS from Quarkus backend

问题

我需要发送一个HTTP请求到AWS IoT核心以更新设备影子。此请求应通过Quarkus后端发送。当前问题是如何将身份验证标头附加到请求。目前在此代码中存在身份验证问题。

这是用于发送请求的类:

import io.smallrye.mutiny.Uni;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import javax.json.JsonObject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/things/abc")
@Produces(MediaType.APPLICATION_JSON)
@RegisterRestClient(baseUri = "https://a144gttuytyty10wv7-ats.iot.us-east-1.amazonaws.com")
public interface OrganizationProxy {

    @GET
    @Path("/shadow?name=shadow-version-1")
    Uni<JsonObject> getOrg();

}

这是用于测试样本GET请求的客户端类:

import io.smallrye.mutiny.Uni;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.json.JsonObject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/organizations")
@ApplicationScoped
public class OrganizationResource {

    @Inject
    @RestClient
    OrganizationProxy organizationProxy;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<JsonObject> get() {
        return organizationProxy.getOrg();
    }
}

我已经尝试附加身份验证标头,但我没有找到在程序中如何执行此操作的方法。我的最终目标是向AWS IoT核心设备API发送请求并获取响应。

英文:

I have to send a HTTP request to AWS IoT core to update device shadow. This request should send through Quarkus backend. The current problem is how to Append Authentication headers to the request. Currently there is a authentication problem with this code.

This is the Class used to send the request

import io.smallrye.mutiny.Uni;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import javax.json.JsonObject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path(&quot;/things/abc&quot;)
@Produces(MediaType.APPLICATION_JSON)
@RegisterRestClient(baseUri = &quot;https://a144gttuytyty10wv7-ats.iot.us-east-1.amazonaws.com&quot;)
public interface OrganizationProxy {

@GET
@Path(&quot;/shadow?name=shadow-version-1&quot;)
Uni&lt;JsonObject&gt; getOrg();

}

This is the client class for test sample get request

import io.smallrye.mutiny.Uni;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.json.JsonObject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path(&quot;/organizations&quot;)
@ApplicationScoped
public class OrganizationResource {


@Inject
@RestClient
OrganizationProxy organizationProxy;


@GET
@Produces(MediaType.APPLICATION_JSON)
public Uni&lt;JsonObject&gt; get() {
return organizationProxy.getOrg();
}

}

I already try to append the Authentication headers. But I didn't found a way to how to do this in the program. My final goal is to send a request to this AWS IoT core device API and get a response.

答案1

得分: 0

你打算使用基于X509证书的身份验证还是使用IAM访问密钥和密钥(参见客户端身份验证)?我建议利用现有的SDK而不是自行构建。例如,你可以将此库添加为Maven依赖项:

<dependency>
  <groupId>software.amazon.awssdk.iotdevicesdk</groupId>
  <artifactId>aws-iot-device-sdk</artifactId>
  <version>1.11.5</version>
</dependency>

然后直接使用这些类。请参阅https://github.com/aws/aws-iot-device-sdk-java-v2获取示例。

英文:

Do you plan to use X509 certificate-based authentication or leveraging IAM access key and secret (see Client authentication)?

I recommend to leverage an existing SDK instead of building on your own. E.g. you can add this library as a Maven dependency

&lt;dependency&gt;
  &lt;groupId&gt;software.amazon.awssdk.iotdevicesdk&lt;/groupId&gt;
  &lt;artifactId&gt;aws-iot-device-sdk&lt;/artifactId&gt;
  &lt;version&gt;1.11.5&lt;/version&gt;
&lt;/dependency&gt;

and directly use the classes. See https://github.com/aws/aws-iot-device-sdk-java-v2 for samples.

答案2

得分: 0

我连接到了AWS IoT Core设备影子的REST端点,使用了AWS SignV4。

https://github.com/kavishkamk/AWS-IoT-Core-REST-endpont-with-AWSV4

英文:

I connected with AWS Iot Core Device Shadow Rest end point with AWS SignV4.

https://github.com/kavishkamk/AWS-IoT-Core-REST-endpont-with-AWSV4

huangapple
  • 本文由 发表于 2023年3月31日 16:13:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定