在spring.xml中配置corsFilter对象
<bean id="corsFilter" class="org.springframework.web.filter.CorsFilter">
<constructor-arg name="configSource">
<bean class="org.springframework.web.cors.UrlBasedCorsConfigurationSource">
<property name="corsConfigurations">
<map>
<entry key="/**">
<bean class="org.springframework.web.cors.CorsConfiguration">
<property name="allowedOrigins" value="http://localhost:8080"></property>
<property name="allowCredentials" value="true"></property>
<property name="allowedHeaders" value="*"></property>
<property name="allowedMethods" value="*"></property>
<property name="maxAge" value="1800"></property>
</bean>
</entry>
</map>
</property>
</bean>
</constructor-arg>
</bean>
在web.xml配置CorsFilter,以及生效范围
<filter>
<filter-name>delegatingFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<!-- 来自xml文件中bean对象的id-->
<param-value>corsFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>delegatingFilter</filter-name>
<!-- 全路径过滤-->
<url-pattern>/*</url-pattern>
</filter-mapping>