1

他のドメインから JavaScript によって要求される Web サービスのパックを作成する必要があります。

クロスドメインをサポートするためのクールなパッケージがあることを発見しました: CORSFilter.

CORS の実装は web.xml への追加によるものであることがわかりました。

<web-app>
<filter>
    <filter-name>CORSFilter</filter-name>
    <filter-class>CORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CORSFilter</filter-name>
    <url-pattern>/api/*</url-pattern>
</filter-mapping>

しかし、私は自分の構成を持っています:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    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">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>



</web-app>

CORS 構成を web.xml に追加しようとしているとき:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
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">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>corsFilter</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>corsFilter</filter-name>
    <servlet-name>/*</servlet-name>
</filter-mapping>

エラーが発生します:

    mar 19, 2013 2:17:08 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error filterStart
mar 19, 2013 2:17:08 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [] startup failed due to previous errors

どのように実装すればよいですか?

ありがとう

4

2 に答える 2

1

Spring Framework 4.2 GAの時点で利用可能なネイティブ CORS サポートの使用を検討する必要があるかもしれません。詳細については、このブログ投稿を参照してください。

Filterたとえば、Spring MVC を使用せずに Spring Web のみを使用している場合など、ベースのアプローチを引き続き使用したい場合は、SpringCorsFilterも提供されます。

于 2015-07-21T12:58:16.343 に答える
1

「filter-mapping」セクションには「servlet-name」タグがありますが、実際にはその中にあるのはサーブレット名ではなく URL パターンです。だからあなたはどちらかを持つべきです

<servlet-name>appServlet</servlet-name>

また

<url-pattern>/*</url-pattern>
于 2013-04-04T09:28:35.970 に答える