gpt4 book ai didi

java - 如何在 Undertow 中启用 Access-Control-Allow-Origin?

转载 作者:行者123 更新时间:2023-11-30 02:39:58 25 4
gpt4 key购买 nike

尝试1

builder.setHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(new HttpString("Access-Control-Allow-Origin"), "*");
exchange.getResponseHeaders().put(new HttpString("Access-Control-Allow-Methods"),
"GET, POST, PUT, DELETE, OPTIONS");
String ss = exchange.getResponseHeaders().get(new HttpString("Access-Control-Allow-Headers"))
.getFirst();
System.out.println(ss);
exchange.getResponseHeaders().put(new HttpString("Access-Control-Allow-Headers"), ss);
}
});

尝试2

HttpHandler enHandler = new SetHeaderHandler("Access-Control-Allow-Origin", "*");
builder.setHandler(enHandler);

我尝试了上述设置以在我的 undertow 服务中启用 CORS,但它不适用于我的 Angular UI。在 postman 中工作正常。

最佳答案

添加这些行后它就可以工作

        ResteasyDeployment rd = new ResteasyDeployment();
CorsFilter filter = new CorsFilter();
filter.setAllowedMethods("GET,POST,PUT,DELETE,OPTIONS");
filter.getAllowedOrigins().add("*");
rd.setProviderFactory(new ResteasyProviderFactory());
rd.getProviderFactory().register(filter);

关于java - 如何在 Undertow 中启用 Access-Control-Allow-Origin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42066845/

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