網站首頁 編程語言 正文
其實相信很多編程的初學者可能不太理解什么是跨域問題,因為做后端項目的時候,大部分都是使用postman自測,一些老的項目都是前后端不分離,這樣不會發生跨域的問題。
跨域的問題就是請求發起方和接收方不在同一個域。
協議不同,域名不同(localhost和127.0.0.1也不同),端口不同都屬于跨域
跨源資源共享 (CORS)(或通俗地譯為跨域資源共享)是一種基于 HTTP 頭的機制,該機制通過允許服務器標示除了它自己以外的其它 origin(域,協議和端口),使得瀏覽器允許這些 origin 訪問加載自己的資源。跨源資源共享還通過一種機制來檢查服務器是否會允許要發送的真實請求,該機制通過瀏覽器發起一個到服務器托管的跨源資源的"預檢"請求。在預檢中,瀏覽器發送的頭中標示有 HTTP 方法和真實請求中會用到的頭。
但是有的時候并不會觸發跨域問題
當使用簡單請求
- GET
- HEAD
- POST
Content Type 也僅屬于 - text/plain
- multipart/form-data
- application/x-www-form-urlencoded
否則會發送預檢請求(一個options請求)
如果預檢請求沒有通過,那么后面真正的請求是不會發送的
在springboot中解決跨域問題有三種辦法
- 在controller的方法或者類加上
@CrossOrigin(origins = "*")
注解
@RestController
@CrossOrigin(origins = "*")
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello world";
}
}
- 配置跨域過濾器
@Configuration
public class FrankMallCorsConfiguration {
@Bean
public CorsWebFilter corsWebFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.addAllowedHeader("*");
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.setAllowCredentials(true);
source.registerCorsConfiguration("/**", config);
CorsWebFilter corsWebFilter = new CorsWebFilter(source);
return corsWebFilter;
}
}
- 實現WebMvcConfigurer接口,重寫addCorsMappings方法
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.maxAge(3600);
}
}
- 手動設置單次請求的響應頭
@RequestMapping("/index")
public String index(HttpServletResponse response) {
response.addHeader("Access-Allow-Control-Origin","*");
return "index";
}
- 通過過濾器,記得在主類加上
@ServletComponentScan("com.lxc.frankmall.coupon")
@WebFilter(urlPatterns = {"/*"})
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
filterChain.doFilter(servletRequest,servletResponse);
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
response.setHeader("Access-Control-Expose-Headers", "content-disposition");
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
原文鏈接:https://blog.csdn.net/weixin_42293662/article/details/125465881
- 上一篇:注冊bean有多少種方式
- 下一篇:瀏覽器解析機制和XSS向量編碼
相關推薦
- 2022-05-26 Android?Flutter實現3D動畫效果示例詳解_Android
- 2023-02-04 詳解C++中存儲類的使用_C 語言
- 2022-06-18 go語言實現Elasticsearches批量修改查詢及發送MQ操作示例_Golang
- 2022-07-08 一文詳解C++中運算符的使用_C 語言
- 2022-12-21 python中把嵌套的列表合并成一個列表方法總結_python
- 2022-05-29 簡單聊聊Golang中defer預計算參數_Golang
- 2022-05-19 python?字典常用方法超詳細梳理總結_python
- 2022-07-30 注冊中心eureka的介紹及源碼探索
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支