Search code examples
javasoapcorsjax-ws

CORS problems with sending OPTIONS method - Java SOAP services


I have java application with Soap services. I can access my services through SOAP UI and everything is fine. The problem occurred when i am trying to access the services through Angluar app.

When i use:

 curl -i -X OPTIONS http://localhost:8080/test

i am receiving

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
 <soap:Fault>
  <faultcode>soap:Server</faultcode>
  <faultstring>HTTP verb was not GET or POST</faultstring>
 </soap:Fault>
</soap:Body>
</soap:Envelope>

I found some solutions but they did not helped.

This is handler class:

public boolean handleMessage(SOAPMessageContext smc) {
    System.out.println("Handling soap message...");


    Map<String, List<String>> map = (Map<String, List<String>>)smc.get(MessageContext.HTTP_REQUEST_HEADERS);        
    Boolean outboundProperty = (Boolean) smc
            .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {
        //logger.debug("\nOutbound message:");
          HttpServletResponse response = (HttpServletResponse) smc.get(MessageContext.SERVLET_RESPONSE);

            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Credentials", "false");
            response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, x-requested-with, Content-Type, SOAPAction, Access-Control-Allow-Headers, Access-Control-Response-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin");



                    FilterChain chain = new FilterChain() {

                        @Override
                        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {


                        }
                    };

                     if(!((String) smc.get(MessageContext.HTTP_REQUEST_METHOD)).equalsIgnoreCase("OPTIONS")) {
                         try {
                            chain.doFilter((ServletRequest) smc.get(MessageContext.SERVLET_REQUEST), response);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (ServletException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
            }
    }
     else {
        //logger.debug("\nInbound message:");
    }

    return true;

and here is my server class:

JaxWsServerFactoryBean serverFactory = new JaxWsServerFactoryBean();

    serverFactory.setAddress("http://localhost:8080/test");

    serverFactory.setServiceBean(new WSRepository());
    List<Handler> handlerList = new ArrayList<>();
    handlerList.add(new SOAPLoggingHandler());
    serverFactory.setHandlers(handlerList);

    serverFactory.create();

Solution

  • In Nginx you can avoid sending OPTIONS request and instead send direct POST request. You can try that.