在AWS Api网关自定义授权者中,如何基于对头的验证将自定义头添加到输入请求并发送到后端

时间:2019-03-01 23:36:20

标签: amazon-web-services aws-api-gateway custom-authentication lambda-authorizer

在AWS API网关中,我使用自定义lambda授权程序来验证请求标头。我需要根据验证结果更新现有的标头或添加新的标头。 以下是Java中的Lambda授权者逻辑,验证工作符合预期。不管更新标头值如何,后端lambda中接收的事件对象都是空的。

@Override
    public AuthPolicy handleRequest(APIGatewayProxyRequestEvent event, Context context) {
    LambdaLogger logger = context.getLogger();
    logger.log("Loading Java Lambda handler of Proxy");
    logger.log("Event Object  :  " + event.toString());

    Pattern requestIdpattern = Pattern.compile("[0-9a-f]{4}-[0-9A-Z]{3}");

    ProxyRequestContext reqContext = event.getRequestContext();
    boolean isValid = false;
    Map<String, String> headers = event.getHeaders();
    if (requestIdpattern.matcher(headers.get("x-request-id")).matches()) {
        isValid = true;
        headers.put("x-jid", UUID.randomUUID().toString());
    }
    if (isValid) {
        AuthPolicy authPolicy = new AuthPolicy("XXXX",
                PolicyDocument.getAllowPolicy("us-east-1", reqContext.getAccountId(), reqContext.getApiId(),
                        reqContext.getStage(), HttpMethod.getHttpMethod(reqContext.getHttpMethod()),
                        reqContext.getResourcePath()));
        return authPolicy;
    } else {
        AuthPolicy authPolicy = new AuthPolicy("XXXXXX",
                PolicyDocument.getDenyPolicy("us-east-1", reqContext.getAccountId(), reqContext.getApiId(),
                        reqContext.getStage(), HttpMethod.getHttpMethod(reqContext.getHttpMethod()),
                        reqContext.getResourcePath()));
        logger.log("Auth Policy Response Object  :  " + authPolicy.toString());
        return authPolicy;
    }
}

请让我知道如何配置授权者,以便在成功授权后将对API网关请求的输入请求发送到集成服务。

0 个答案:

没有答案