2017-04-10 164 views
1

我想通過java SDK實現自定義授權者lambda函數。有人可以告訴我從我的lambda函數中預期的JSON響應的確切格式。另外,我應該返回輸出格式(JSON對象或策略對象)。AWS API網關自定義授權者

{ 
    "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" 
} 

這是格式我在輸出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.JsonMappingException.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

任何幫助將是巨大的。在此先感謝

回答

0

你所面臨的問題是Λ框架有關。

本質上,Lambda將調用處理函數並傳遞一個序列化的JSON。

public class LambdaCustomAuthorizer implements RequestHandler<AuthorizationRequestDO, Object> { 


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

}

當您使用自定義的授權工作,API網關通過以下JSON到您的lambda表達式:

{ 「類型」: 「令牌」, 「authorizationToken」: 「」 , 「methodArn」: 「阿爾恩:AWS:執行-API ::: ///」 }

,你應該有一個自定義DO AuthorizationRequestDO

這是一個POJO ::

公共類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() + "]"; 
} 

}

+1

問題是以什麼作爲自定義授權者lambda函數的輸出返回。我返回了一個JSON字符串IAM策略,它引發了序列化錯誤。我做了什麼創建了一個相當於IAM策略的POJO類,並返回此對象POJO類作爲輸出解決了問題。再次,這是AWS lambda中的一個錯誤。它只能序列化POJO類而不是原始數據類型和包裝類。 – dpanshu

0

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" 
} 
+0

只有它的單個字符串....反正這個問題現在已經消失。轉而成爲AWS lambda的一件事。 – dpanshu

相關問題