溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring4.3如何實現跨域CORS

發(fā)布時間:2021-05-21 11:43:18 來源:億速云 閱讀:147 作者:小新 欄目:編程語言

這篇文章主要介紹了spring4.3如何實現跨域CORS,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

首先交代一下,我的jdk是1.8tomcat是8.0,我的項目是ssm+maven。

spring的框架是4.3.在使用的時候,只需要在controller的@controller注解之前注解@CrossOrigin,然后在springmvc.xml的配置文件添加

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

//與此次操作無關的配置都省略了

 <mvc:cors>
  <mvc:mapping path="/**"
  allowed-origins=""
  allowed-methods="POST,GET,OPTIONS,DELETE,PUT"
//header里面userName與passWord是由于項目的需要我自定義的請求頭的內容。
  allowed-headers="Origin,X-Mx-ReqToken,x-requested-with,userName,passWord,Authorization,Accept,content-type"
  allow-credentials="true"/>
 </mvc:cors>
</beans>

這里是controller層添加注解的截圖

spring4.3如何實現跨域CORS

感謝你能夠認真閱讀完這篇文章,希望小編分享的“spring4.3如何實現跨域CORS”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI