2015-10-15 57 views
0

我正在使用「Java EE 1.5,支持JAX-WS批註來創建Web服務。Java EE 1.5 Servlet - http方法OPTIONS給出了「405方法不允許」

我的網絡服務可以調用沒有肥皂用戶界面的問題。 我想從Chrome使用JavaScript和JQuery調用它。我遇到了交叉視線安全問題。我是需要添加到呼叫中的標頭的一部分,並且我添加了它們並驗證它們存在於Soap UI中。 看着鉻日誌我可以看到,它正在發送一個http調用的方法「選項」到web服務,它得到一個響應「405(方法不允許)」

從我能做些什麼在它到達我的servlet之前攔截「OPTIONS」方法。 「POST」請求無任何問題。 有誰知道如何配置Java EE 1.5 servlett以接收OPTIONS HTTP方法?我用google搜索過,但沒有運氣。

我的web.xml:

<?xml version = '1.0' encoding = 'UTF-8'?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"> 
    <servlet> 
    <servlet-name>connectionInfo</servlet-name> 
    <servlet-class>ic.ac.uk.icsoa.wladminserversensor.wsdlgen.Main_ptImpl</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>connectionInfo</servlet-name> 
    <url-pattern>/connectionInfo</url-pattern> 
    </servlet-mapping> 
</web-app> 

我的Java代碼:

package ic.ac.uk.icsoa.wladminserversensor.wsdlgen; 

import ic.ac.uk.icsoa.wladminserversensor.WeblogicSensor; 
import ic.ac.uk.icsoa.wladminserversensor.wsdlgentypes.ObjectFactory; 
import ic.ac.uk.icsoa.wladminserversensor.wsdlgentypes.RequestSOAEndpointsMessageType; 
import ic.ac.uk.icsoa.wladminserversensor.wsdlgentypes.ResponseSOAEndpointsMessageType; 

import java.util.HashMap; 
import java.util.Map; 

import javax.annotation.Resource; 

import javax.jws.WebMethod; 
import javax.jws.WebParam; 
import javax.jws.WebResult; 
import javax.jws.WebService; 

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import javax.xml.bind.annotation.XmlSeeAlso; 
import javax.xml.ws.Action; 
import javax.xml.ws.BindingType; 
import javax.xml.ws.WebServiceContext; 
import javax.xml.ws.handler.MessageContext; 
import javax.xml.ws.soap.SOAPBinding; 

@WebService(name = "main_pt", targetNamespace = "urn:WLAdminServerSensor", serviceName = "connectionInfoService", portName = "connectionInfo", wsdlLocation = "/WEB-INF/wsdl/connectionInfoService.wsdl") 
@XmlSeeAlso({ ObjectFactory.class }) 
@javax.jws.soap.SOAPBinding(style = javax.jws.soap.SOAPBinding.Style.DOCUMENT, parameterStyle = javax.jws.soap.SOAPBinding.ParameterStyle.BARE) 
@BindingType(SOAPBinding.SOAP12HTTP_BINDING) 
public class Main_ptImpl { 
    @Resource 
    WebServiceContext ctxt; 

    public Main_ptImpl() { 
    } 

    @javax.jws.soap.SOAPBinding(parameterStyle = javax.jws.soap.SOAPBinding.ParameterStyle.BARE) 
    @Action(input = "urn:WLAdminServerSensor/querySOAEndpoints", output = "urn:WLAdminServerSensor/main_pt/querySOAEndpointsResponse") 
    @WebMethod(action = "urn:WLAdminServerSensor/querySOAEndpoints") 
    @WebResult(name = "responseSOAEndpointsMessage", targetNamespace = "http://ic.ac.uk.icdev.rjmtest.wladminserversensor.types", partName = "requestSOAEndpointsOutputPart") 
    public ResponseSOAEndpointsMessageType querySOAEndpoints(@WebParam(name = "requestSOAEndpointsMessage", partName = "requestSOAEndpointsInputPart", targetNamespace = "http://ic.ac.uk.icdev.rjmtest.wladminserversensor.types") 
     RequestSOAEndpointsMessageType requestSOAEndpointsInputPart) { 

     MessageContext mc = ctxt.getMessageContext(); 

     HttpServletResponse response = (HttpServletResponse) mc.get(MessageContext.SERVLET_RESPONSE);   
     HttpServletRequest request = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST); 
     response.addHeader("Access-Control-Allow-Origin", "*"); 
     if (request.getMethod().equals("OPTIONS")) { 
      response.addHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); 
      response.addHeader("Access-Control-Allow-Methods","GET, POST, PUT"); 
      return null; 
     }; 

     return WeblogicSensor.execute(requestSOAEndpointsInputPart); 
    } 
} 

的Javascript運行在Chrome調用servlet(使用JQuery):

soapRequest = "<soap:Envelopexmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"xmlns:ic=\"http://ic.ac.uk.icdev.rjmtest.wladminserversensor.types\"><soap:Header/><soap:Body><ic:requestSOAEndpointsMessage><ic:Input>CallviaJQuery</ic:Input><ic:Credentials><ic:Username>weblogic</ic:Username><ic:Password>*****</ic:Password></ic:Credentials></ic:requestSOAEndpointsMessage></soap:Body></soap:Envelope>"; 

var jqxhr = $.ajax({ 
     type: 'POST', 
     url: param.endpoint, 
     data: {soapRequest}, 
     dataType: 'xml', 
     contentType: 'application/soap+xml;charset=UTF-8;action="urn:WLAdminServerSensor/querySOAEndpoints"' 
}).done(function(data) { 
    alert("TODO"); 
}).fail(function(data) { 
    alert("TODO - FAIL"); 
}); 

回答

0

我找到了解決方案,以便我發佈它爲了他人的利益。我不得不創建我自己的CORS過濾器。我通過創建一個實現「javax.servlet.Filter」的類來實現這一點。我通過逆向工程構建了thie,其中包括tomcat-catalina-8.0.0-rc1.jar.zip。我不得不創建自己的版本,而不是僅僅包含apache版本,因爲我從我的機器上的本地文件而不是從另一個網站運行我的javascript。這使得網站來源被報告爲空。即使過濾器被配置爲允許起源爲*,本地文件給出的來源(null,我認爲)被拒絕。

在我自己的版本(改變checkRequestType函數)調整代碼後,我得到了它的工作。

我然後有一個web.xml如下:

<?xml version = '1.0' encoding = 'UTF-8'?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
     version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"> 
    <servlet> 
    <servlet-name>connectionInfo</servlet-name> 
    <servlet-class>ic.ac.uk.icsoa.wladminserversensor.wsdlgen.Main_ptImpl</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>connectionInfo</servlet-name> 
    <url-pattern>/connectionInfo</url-pattern> 
    </servlet-mapping> 
<filter> 
    <filter-name>CorsFilter</filter-name> 
    <filter-class> ic.ac.uk.icsoa.wladminserversensor.RJMCorsFilter</filter-class> 
    <init-param> 
     <param-name>cors.allowed.origins</param-name> 
     <param-value>*</param-value> 
    </init-param> 
    <init-param> 
     <param-name>cors.allowed.methods</param-name> 
     <param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE,PATCH</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>CorsFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
</web-app> 

和簡化我的servlet:

public ResponseSOAEndpointsMessageType querySOAEndpoints(@WebParam(name = "requestSOAEndpointsMessage", partName = "requestSOAEndpointsInputPart", targetNamespace = "http://ic.ac.uk.icdev.rjmtest.wladminserversensor.types") 
    RequestSOAEndpointsMessageType requestSOAEndpointsInputPart) { 
    return WeblogicSensor.execute(requestSOAEndpointsInputPart); 
} 

而且它的所有工作。