简体   繁体   English

AWS Appsync Graphql Java 客户端 - IAM 授权

[英]AWS Appsync Graphql Java Client - IAM authorisation

This is the schema for which I have implement business logic这是我实现业务逻辑的架构

type Query {
     getLicenseInformation(localmd5: String): License @aws_cognito_user_pools
   getUserInformation(username: String!): CognitoUser @aws_iam
    listUsers(searchString: String): [NamedResource] @aws_iam
}

I use RestTemplate as my Java client to consume graphql endpoint giving API key as authorization.我使用 RestTemplate 作为我的 Java 客户端来使用提供 API 密钥作为授权的 graphql 端点。 I ad dthe api key in the header paart as x-api-key.我在标题 paart 中将 api 密钥添加为 x-api-key。

        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders requestHeaders = new HttpHeaders();

        requestHeaders.set("x-api-key",api_key.getId());
        requestHeaders.set("Content-Type","application/graphql");

        HttpEntity entity = new HttpEntity(requestHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(URL, HttpMethod.POST, new HttpEntity(query,requestHeaders),String.class);

The above implementation retrieves the values from the backend.上面的实现从后端检索值。 But according the schema which is defined by the other team, the authorization mode is not API key rather iam.但是根据其他团队定义的模式,授权模式不是API key而是iam。 So I have to configure the rest template accordingly.所以我必须相应地配置其余模板。

Where in the Client side code in Java I can configure so that aws_iam is used as authorization method to retrieve the information from the endpoint.在 Java 客户端代码中,我可以配置 aws_iam 作为授权方法从端点检索信息。 Dynamodb is the datasource Dynamodb 是数据源

Building the request object like below helps:像下面这样构建请求对象有帮助:

private DefaultRequest prepareRequest(HttpMethodName method, InputStream content) {
        Map<String,String> headers = new HashMap<>();
        headers.put("Content-type", "application/json");
        headers.put("type", "AUTH_TYPE.AWS_IAM");
        headers.put("X-Amz-Security-Token",securityToken);
        DefaultRequest request = new DefaultRequest(API_GATEWAY_SERVICE_NAME);
        request.setHttpMethod(method);
        request.setContent(content);
        request.setEndpoint(this.endpoint);
        request.setHeaders(headers);

        return request;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM