在任意可在web.xml中加载的xml 配置bean
<!--跨域设置--> <bean class="org.springframework.web.filter.CorsFilter"> <constructor-arg name="configSource"> <bean id="MyCors" class="org.springframework.web.cors.UrlBasedCorsConfigurationSource"> <property name="corsConfigurations"> <map> <entry key="/**"> <bean class="org.springframework.web.cors.CorsConfiguration"> <property name="allowCredentials" value="true"/> <property name="allowedHeaders" value="*"/> <property name="allowedOrigins" value="*"/> <property name="allowedMethods"> <list> <value>GET</value> <value>PUT</value> <value>POST</value> <value>DELETE</value> <value>OPTIONS</value> <value>HEAD</value> </list> </property> </bean> </entry> </map> </property> </bean> </constructor-arg> </bean>在web.xml中加载:
<!--加载跨域配置--> <filter> <filter-name>myCorsFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetBeanName</param-name> <param-value>MyCors</param-value> </init-param> </filter> <filter-mapping> <filter-name>myCorsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
options是预检请求,在真正的请求发送出去之前,浏览器会先发送一个options请求向服务询问此接口是否允许我访问 浏览器在当前真实请求是非简单请求且跨域的情况下会发起options预检请求
1.请求方法为get,post,head 2.Content-Type限于(application/x-www-form-urlencoded,mutipart/form-data,text/plain) 3.不可以有自定义请求头如xxx-token等
非简单请求即为复杂请求 1.常见请求方法为 put delete 2.Content-Type为application/json 3.添加自定义http header
注意 对于预检请求,请设置放行