在 java 中的 AWS lambda 函数中包含 Access-control-header

我一直在尝试部署一个 lambda 函数,然后通过 API 网关使其可访问。我的 java 函数在 JAVA 中,这是我在 JAVA 中创建一个简单的 AWS lambda 函数所遵循的文档:


https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html


这是我的函数处理程序的外观:


package lambda;


    import com.amazonaws.services.lambda.runtime.Context;

    import com.amazonaws.services.lambda.runtime.RequestHandler;

    import lambda.axon.Path;


    public class shortestPath implements RequestHandler<RequestClass, ResponseClass>{


        public ResponseClass handleRequest(RequestClass request, Context context){

            String inputString = request.inputString;

            int steps = Path.stepsTo(inputString);

            return new ResponseClass(steps);


        }

    }

这是这个请求类:


package lambda;


public class RequestClass {

    String inputString;


    public String getInputString() {

        return inputString;

    }


    public void setInputString(String inputString) {

        this.inputString = inputString;

    }



    public RequestClass(String inputString) {

        this.inputString = inputString;

    }


    public RequestClass() {

    }

}

这是响应类:


package lambda;


public class ResponseClass {

    int steps;


    public ResponseClass(int steps) {

        this.steps = steps;

    }


    public ResponseClass() {

    }

    public int getSteps() {

        return steps;

    }


    public void setSteps(int steps) {

        this.steps = steps;

    }



}

我在aws上部署它并配置AWS api网关来触发它,当我使用邮递员时,当我到达API网关给出的端点时一切正常(https://www.getpostman.com/)


但是通过浏览器尝试相同的操作会给我一个 CORS 错误:


Access to XMLHttpRequest at 'https://<hash>execute-api.us-east-1.amazonaws.com/dev' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

我尝试在 API 网关控制台中启用 CORS,然后重新部署它:

http://img4.mukewang.com/62c4ec9b0001d26005100461.jpg

这篇 Stackoverflow 帖子(在 AWS Lambda 上配置 CORS 响应标头?)说如果我使用 lambda-proxy,我应该在处理程序响应本身中包含标头,我不确定代理是什么,但我如何用我当前的Lambda 函数的实现,即在我的响应中包含自定义标头



沧海一幻觉
浏览 135回答 2
2回答

largeQ

要启用cors连接到 lambda 函数的 api 网关端点,您必须为 api 端点启用 cors(您已经完成)并配置您的 lambda 函数 suport cors。按照我的例子:// new respose class, replace for your class - ResponseClasspublic class Response {&nbsp; &nbsp; private int statusCode; // http status code&nbsp; &nbsp; private Map<String, String> headers; // headers&nbsp; &nbsp; private String body; // body - what you want to return to client&nbsp; &nbsp; public Response(int statusCode, Map<String, String> headers, String body) {&nbsp; &nbsp; &nbsp; &nbsp; this.statusCode = statusCode;&nbsp; &nbsp; &nbsp; &nbsp; this.headers = headers;&nbsp; &nbsp; &nbsp; &nbsp; this.body = body;&nbsp; &nbsp; }&nbsp; &nbsp; public int getStatusCode() {&nbsp; &nbsp; &nbsp; &nbsp; return statusCode;&nbsp; &nbsp; }&nbsp; &nbsp; public Map<String, String> getHeaders() {&nbsp; &nbsp; &nbsp; &nbsp; return headers;&nbsp; &nbsp; }&nbsp; &nbsp; public String getBody() {&nbsp; &nbsp; &nbsp; &nbsp; return body;&nbsp; &nbsp; }&nbsp; &nbsp; public void setStatusCode(int statusCode) {&nbsp; &nbsp; &nbsp; &nbsp; this.statusCode = statusCode;&nbsp; &nbsp; }&nbsp; &nbsp; public void setHeaders(Map<String, String> headers) {&nbsp; &nbsp; &nbsp; &nbsp; this.headers = headers;&nbsp; &nbsp; }&nbsp; &nbsp; public void setBody(String body) {&nbsp; &nbsp; &nbsp; &nbsp; this.body = body;&nbsp; &nbsp; }}/// -------------package lambda;&nbsp; &nbsp; import com.amazonaws.services.lambda.runtime.Context;&nbsp; &nbsp; import com.amazonaws.services.lambda.runtime.RequestHandler;&nbsp; &nbsp; import lambda.axon.Path;&nbsp; &nbsp; public class shortestPath implements RequestHandler<RequestClass, Response>{&nbsp; &nbsp; &nbsp; &nbsp; public Response handleRequest(RequestClass request, Context context){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String inputString = request.inputString;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int steps = Path.stepsTo(inputString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> headers = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers.put(Access-Control-Allow-Origin, "*"); // cors header, you can add another header fields&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Response(200, headers, "" + steps);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return new Response(200, headers, "{result: " + steps + "}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // simple json response. ex: {result: '3433"}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }我的方式仅在 api 网关使用LAMBDA-PROXY事件配置时生效(默认)

慕哥9229398

您可以从 api 网关启用 cors 您可以从 lambda 管理 cors 并从 lambda 管理标头。我建议从 api 网关启用 cors 并测试它是否可以工作。你可以管理access-control-origin并headers喜欢这种方式'use strict';module.exports.hello = function(event, context, callback) {const response = {&nbsp; statusCode: 200,&nbsp; headers: {&nbsp; &nbsp; "Access-Control-Allow-Origin" : "*", // Required for CORS support to work&nbsp; &nbsp; "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS&nbsp;&nbsp; },&nbsp; body: JSON.stringify({ "message": "Hello World!" })};callback(null, response);};您可以参考此文档: https ://serverless.com/framework/docs/providers/aws/events/apigateway/
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java