Java 跨域 CrossOrigin注解 Filter攔截 Nginx配置


說明

  • 資源請求的發起方與請求的資源不在同一個域中的;
  • 一般的,只要網站的【協議名protocol】、【主機host】、【端口號port】這三個中的任意一個不同,網站間的數據請求與傳輸便構成了跨域調用;
  • 跨域請求能夠達到服務端,但是返回結果會被瀏覽器攔截;

處理方式

1、filter處理器

一般的處理方法,在Filter中攔截請求,在請求頭中添加信息,例如:

setHeader("Access-Control-Allow-Origin","*")

2、@CrossOrigin注解

1、Spring 4.2之后提供了跨域注解 @CrossOrigin;
2、可以用在方法或Controller上;
3、Controller和方法上都有時,Spring會合並兩個注解的屬性一起使用;

[官方文檔]https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/CrossOrigin.html

@CrossOrigin
@RequestMapping("/obtainJqxxListByPage")
public Map obtainJqxxListByPage(){}

注解屬性有以下7個

String[] value() default {}
String[] origins() default {}
String[] allowedHeaders() default {}
String[] exposedHeaders() default {}
RequestMethod[] methods() default {}
String  allowCredentials() default {}
long maxAge() default -1L

3、Nginx配置解決跨域


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM