SpringMVC解决跨域问题,以及跨服务器调用时候session丢失的问题总结!!!

  • Post author:
  • Post category:其他


SpringMVC解决跨域问题,以及跨服务器调用时候session丢失的问题总结!!!

从2个方面解决

1:web.xml配置中加上

<filter>



<filter-name>CorsFilter</filter-name>



<filter-class>org.apache.catalina.filters.CorsFilter</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</param-value>



</init-param>



<init-param>



<param-name>cors.allowed.headers</param-name>



<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>



</init-param>



<init-param>



<param-name>cors.exposed.headers</param-name>



<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>



</init-param>




<!– 此处一定要这样设置,而且对应的客户端也要设置xhrFields:{withCredentials:true},否则session将失去 –>





<init-param>



<param-name>cors.support.credentials</param-name>



<param-value>true</param-value>



</init-param>




<init-param>



<param-name>cors.preflight.maxage</param-name>



<param-value>10</param-value>



</init-param>



</filter>



<filter-mapping>



<filter-name>CorsFilter</filter-name>



<url-pattern>/*</url-pattern>



</filter-mapping>

“`


注意:最主要的是cors.support.credentials设置为true

2:jquey在使用ajax的时候一定要设置


xhrFields:{withCredentials:true}


然后其他的都是一样



版权声明:本文为cow5287687原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。