[英]How to add CORS support on server side of JAX-WS in java
I can't find answer to this question How to enable CORS on server side of JAX-WS in java 我找不到此问题的答案如何在Java中的JAX-WS的服务器端启用CORS
As Mentionned as response we cann't use the "Handlers" because the first error is about HTTP request (when Javascript request the WSDL). 正如提到的那样,我们不能使用“处理程序”,因为第一个错误是关于HTTP请求的(当Javascript请求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.".
Javascript说:“无法加载http:// XXXX / server?wsdl :所请求的资源上没有'Access-Control-Allow-Origin'标头。因此,不允许访问源' http:// YYYY '。”。 Because CORS headers is not present.
因为不存在CORS标头。
The request header (with Origin) 请求标头(带有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
And the response without CORS header 而且没有CORS标头的响应
Content-type:text/xml;charset=utf-8
Date:Mon, 23 Oct 2017 09:03:51 GMT
Transfer-encoding:chunked
How can i "configure" or "customise" Java Endpoint to do this 我如何“配置”或“定制” Java Endpoint来做到这一点
EDIT 编辑
I may have found the beginning of a solution, but I do not understand the following error: 我可能已经找到解决方案的开始,但是我不理解以下错误:
Source code ServerInfo.java: 源代码ServerInfo.java:
@WebService
public class ServerInfo {
@WebMethod
public String getServerName() {
return "Mon Serveur";
}
}
Source code Publisher.java 源代码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)
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: 您可以在filter方法中验证请求并发布所需的标头:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.