简体   繁体   English

AWS API网关自定义授权程序

[英]AWS API gateway Custom Authorizer

I am trying to implement custom authorizer lambda function via java SDK. 我试图通过java SDK实现自定义授权器lambda函数。 Can somebody tell me the exact format of the JSON response that is expected from my lambda function. 有人可以告诉我lambda函数预期的JSON响应的确切格式。 Also in which format i should return the output (JSON object or policy object). 另外,我应该以哪种格式返回输出(JSON对象或策略对象)。

{
    "policyDocument": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "execute-api:Invoke",
        "Resource": [
          "arn:aws:execute-api:us-east-1:1234567:myapiId/staging/POST/*"
        ],
        "Effect": "Allow"
      }
    ]
    },
    "principalId": "User123"
}

this is the format i am providing in output in JSONObject format but getting error 这是我在输出中以JSONObject格式提供的格式,但是收到错误

Mon Apr 10 09:42:35 UTC 2017 : Endpoint request body after transformations: {"type":"TOKEN","authorizationToken":"ABC123","methodArn":"arn:aws:execute-api:ap-southeast-1:007183653813:ohlqxu9p57/null/GET/"} Mon Apr 10 09:42:36 UTC 2017 : Execution failed due to configuration error: Authorizer function failed with response body: {"errorMessage":"An error occurred during JSON serialization of response","errorType":"java.lang.RuntimeException","stackTrace":[],"cause":{"errorMessage":"com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject[\\"asString\\"])","errorType":"java.io.UncheckedIOException","stackTrace":[],"cause":{"errorMessage":"JsonObject (through reference chain: com.google.gson.JsonObject[\\"asString\\"])","errorType":"com.fasterxml.jackson.databind.JsonMappingException","stackTrace":["com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)","com.fasterxml.jackson.databind.JsonMapp 星期一4月10日09:42:35 UTC 2017:转换后的端点请求体:{“type”:“TOKEN”,“authorizationToken”:“ABC123”,“methodArn”:“arn:aws:execute-api:ap-southeast -1:007183653813:ohlqxu9p57 / null / GET /“} Mon Apr 10 09:42:36 UTC 2017:由于配置错误导致执行失败:授权程序功能因响应正文失败:{”errorMessage“:”JSON序列化期间发生错误响应“,”errorType“:”java.lang.RuntimeException“,”stackTrace“:[],”cause“:{”errorMessage“:”com.fasterxml.jackson.databind.JsonMappingException:JsonObject(通过引用链:com) .google.gson.JsonObject [\\“asString \\”])“,”errorType“:”java.io.UncheckedIOException“,”stackTrace“:[],”cause“:{”errorMessage“:”JsonObject(通过参考链) :com.google.gson.JsonObject [\\“asString \\”])“,”errorType“:”com.fasterxml.jackson.databind.JsonMappingException“,”stackTrace“:[”com.fasterxml.jackson.databind.JsonMappingException。 wrapWithPath(JsonMappingException.java:210) “” com.fasterxml.jackson.databind.JsonMapp ingException.wrapWithPath(JsonMappingException.java:177)","com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:199)","com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:683)","com.f [TRUNCATED] Mon Apr 10 09:42:36 UTC 2017 : AuthorizerConfigurationException ingException.wrapWithPath(JsonMappingException.java:177) “ ”com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:199)“,” com.fasterxml.jackson.databind.ser.std。 BeanSerializerBase.serializeFields(BeanSerializerBase.java:683)“,”com.f [TRUNCATED] Mon Apr 10 09:42:36 UTC 2017:AuthorizerConfigurationException

Any help would be great. 任何帮助都会很棒。 Thanks in advance 提前致谢

AWS开发人员指南提供了lambda函数输入/输出的良好示例: http//docs.aws.amazon.com/apigateway/latest/developerguide/use-custom-authorizer.html

The issue you are facing is Lambda framework related. 您面临的问题是与Lambda框架相关。

Essentially, Lambda will invoke the handler function and pass a serialized JSON. 从本质上讲,Lambda将调用处理函数并传递序列化的JSON。

public class LambdaCustomAuthorizer implements RequestHandler<AuthorizationRequestDO, Object> {


public Object handleRequest(AuthorizationRequestDO input, Context context) { }

} }

When you work with custom authorizer, API gateway passes following JSON to your lambda function: 当您使用自定义授权程序时,API网关将以下JSON传递给您的lambda函数:

{ "type":"TOKEN", "authorizationToken":"", "methodArn":"arn:aws:execute-api:::///" } {“type”:“TOKEN”,“authorizationToken”:“”,“methodArn”:“arn:aws:execute-api ::: ///”}

you should have a custom DO AuthorizationRequestDO 你应该有一个自定义DO AuthorizationRequestDO

which is a POJO:: 这是一个POJO ::

public class AuthorizationRequestDO { public class AuthorizationRequestDO {

 String authorizationToken;
 String methodArn;      


public String getAuthorizationToken() {
    return authorizationToken;
}
public void setAuthorizationToken(String authorizationToken) {
    this.authorizationToken = authorizationToken;
}
public String getMethodArn() {
    return methodArn;
}
public void setMethodArn(String methodArn) {
    this.methodArn = methodArn;
}

@Override
public String toString() {
    return "AuthorizationRequestDO [authorizationToken=" + authorizationToken + ", methodArn=" + methodArn
            + ", getAuthorizationToken()=" + getAuthorizationToken() + ", getMethodArn()=" + getMethodArn() + "]";
}   

} }

Your Resource property should be a single string value. 您的Resource属性应该是单个string值。

{
    "policyDocument": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "execute-api:Invoke",
        "Resource": "arn:aws:execute-api:us-east-1:1234567:myapiId/staging/POST/*",
        "Effect": "Allow"
      }
    ]
    },
    "principalId": "User123"
}

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

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