gpt4 book ai didi

java - 在java中的AWS lambda函数中包含访问控制 header

转载 作者:行者123 更新时间:2023-11-30 01:54:49 26 4
gpt4 key购买 nike

我一直在尝试部署 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网关来触发它,当我使用postman( https://www.getpostman.com/ )到达API网关给出的端点时,一切正常

但是通过浏览器尝试相同的操作会出现 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,然后重新部署它: enter image description here

这篇 Stackoverflow 帖子( Configure CORS response headers on AWS Lambda? )说,如果我使用 lambda 代理,我应该在处理程序响应本身中包含 header ,我不确定代理是什么,但我如何使用当前的实现来做到这一点Lambda 函数,即在我的响应中包含自定义 header

最佳答案

启用cors对于连接到 lambda 函数的 api 网关端点,您必须为 api 端点启用 cors(您已经完成)并配置您的 lambda 函数支持 cors

按照我的例子:

// new respose class, replace for your class - ResponseClass
public class Response {

private int statusCode; // http status code

private Map<String, String> headers; // headers

private String body; // body - what you want to return to client

public Response(int statusCode, Map<String, String> headers, String body) {
this.statusCode = statusCode;
this.headers = headers;
this.body = body;
}

public int getStatusCode() {
return statusCode;
}

public Map<String, String> getHeaders() {
return headers;
}

public String getBody() {
return body;
}

public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}

public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}

public void setBody(String body) {
this.body = body;
}

}

/// -------------

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, Response>{

public Response handleRequest(RequestClass request, Context context){
String inputString = request.inputString;
int steps = Path.stepsTo(inputString);

Map<String, String> headers = new HashMap<>();
headers.put(Access-Control-Allow-Origin, "*"); // cors header, you can add another header fields

return new Response(200, headers, "" + steps);
// return new Response(200, headers, "{result: " + steps + "}");
// simple json response. ex: {result: '3433"}
}
}

我的方式只在api gateway使用时有效LAMBDA-PROXY事件配置(默认)

enter image description here

关于java - 在java中的AWS lambda函数中包含访问控制 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54856423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com