简体   繁体   中英

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

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.

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

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

How can i "configure" or "customise" Java Endpoint to do this

EDIT

I may have found the beginning of a solution, but I do not understand the following error:

Source code ServerInfo.java:

@WebService
public class ServerInfo {
    @WebMethod
    public String getServerName() {
        return "Mon Serveur";
    }
}

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)

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM