如何在 Java 中的 JAX-WS 的服务器端添加 CORS 支持 [英] How to add CORS support on server side of JAX-WS in java
问题描述
我找不到这个问题的答案java中如何在JAX-WS的服务器端启用CORS
I can't find answer to this question How to enable CORS on server side of JAX-WS in java
作为响应我们不能使用处理程序",因为第一个错误是关于 HTTP 请求(当 Javascript 请求 WSDL 时).Javascript 说无法加载 http://XXXX/server?wsdl:没有 'Access-Control-Allow-请求的资源上存在 Origin' 标头.因此不允许访问 Origin 'http://YYYY'.".因为 CORS 标头不存在.
As Mentionned as response we cann't use the "Handlers" because the first error is about HTTP request (when Javascript request the WSDL). Javascript say "Failed to load http://XXXX/server?wsdl: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://YYYY' is therefore not allowed access.". Because CORS headers is not present.
请求头(带有 Origin)
The request header (with Origin)
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:127.0.0.1:11080
Origin:http://10.33.25.15:10080
Referer:http://10.33.25.15:10080/soap/soap.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
以及没有 CORS 标头的响应
And the response without CORS header
Content-type:text/xml;charset=utf-8
Date:Mon, 23 Oct 2017 09:03:51 GMT
Transfer-encoding:chunked
我如何配置"或自定义"Java Endpoint 来执行此操作
How can i "configure" or "customise" Java Endpoint to do this
编辑
我可能已经找到了解决方案的开头,但我不明白以下错误:
I may have found the beginning of a solution, but I do not understand the following error:
源代码ServerInfo.java:
Source code ServerInfo.java:
@WebService
public class ServerInfo {
@WebMethod
public String getServerName() {
return "Mon Serveur";
}
}
源代码 Publisher.java
Source code Publisher.java
public class Publisher {
public static void main(final String[] args) {
try {
Endpoint endpoint = Endpoint.create(new ServerInfo());
HttpServer httpServer = HttpServer.create(new InetSocketAddress(11080), 0);
HttpHandler handler = new HttpHandler() {
@Override
public void handle(final HttpExchange httpExchange) throws IOException {
// TODO CORS
}
};
HttpContext context = httpServer.createContext("/server",handler);
httpServer.start();
endpoint.publish(context);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行时我不明白这个错误
I do not understand this error when running
java.lang.IllegalArgumentException: handler already set
at sun.net.httpserver.HttpContextImpl.setHandler(HttpContextImpl.java:86)
at com.sun.xml.internal.ws.transport.http.server.HttpEndpoint.setHandler(HttpEndpoint.java:121)
at com.sun.xml.internal.ws.transport.http.server.HttpEndpoint.publish(HttpEndpoint.java:75)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:241)
at main(Publisher.java:11)
推荐答案
如果你正在使用 jersey 你可以创建一个过滤器:
If you are using jersey you can create a filter:
public class CorsResponseFilter implements ContainerResponseFilter
过滤器方法是您验证请求并发布所需标头的地方:
The filter method is where you validate the request and publish the required headers:
public void filter (ContainerRequestContext req, ContainerResponseContext containerRsp)
{
final String origin = req.getHeaderString("Origin");
if (!isValidOrigin(origin))
return;
containerRsp.getHeaders().add("Access-Control-Allow-Origin", origin);
containerRsp.getHeaders().add("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS");
String reqHead = headerValue(req, "Access-Control-Request-Headers");
if (isNotEmpty(reqHead))
containerRsp.getHeaders().add("Access-Control-Allow-Headers", reqHead);
return;
}
这篇关于如何在 Java 中的 JAX-WS 的服务器端添加 CORS 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!