Java Spring和CORS [英] Java Spring and CORS
问题描述
我需要创建一组web服务,这将由javascript从其他域请求。
我有foud,有一个很酷的包支持跨-domain: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.5xmlns =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\">
<! - 所有Servlet和过滤器共享的根弹簧容器的定义 - >
< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/spring/root-context.xml< / param-value>
< / context-param>
<! - 创建所有Servlet和过滤器共享的Spring容器 - >
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>
<! - 处理应用程序请求 - >
< 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.5xmlns =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\">
<! - 所有Servlet和过滤器共享的根弹簧容器的定义 - >
< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value> /WEB-INF/spring/root-context.xml< / param-value>
< / context-param>
<! - 创建所有Servlet和过滤器共享的Spring容器 - >
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>
<! - 处理应用程序请求 - >
< 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>
< / filter-mapping>
/ p>
mar 19,2013 2:17:08 PM org.apache.catalina.core.StandardContext startInternal
SEVERE:Error filterStart
mar 19,2013 2:17:08下午org.apache.catalina.core.StandardContext startInternal
SEVERE:上下文[]启动失败,由于以前的错误
感谢
您应该考虑使用 -framework-4-2-goes-garel =nofollow> Spring Framework 4.2 GA 。有关详情,请参见此博文。 / p>
如果您喜欢继续使用基于
Filter
的方法,例如,如果您不使用Spring MVC, ,春季CorsFilter
。I need to create pack of webservices, which will be requestet by javascript from other domains.
I've foud, that there is a cool package for support cross-domain: CORSFilter.
I saw, that the implementation of CORS is by add to 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>
But i've got my own configuration:
<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>
When i'm trying to add CORS configuration to my 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>
I get an error:
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
How i should implement it ?
Thanks
解决方案You should maybe consider using the native CORS support available as of Spring Framework 4.2 GA. See this blog post for more details.
If you prefer continuing using a
Filter
based approach, for example if you are not using Spring MVC but just Spring Web, a SpringCorsFilter
is also provided.这篇关于Java Spring和CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!