日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Springboot 解決跨域問題

作者:Franklin_xc 更新時間: 2022-07-26 編程語言

其實相信很多編程的初學者可能不太理解什么是跨域問題,因為做后端項目的時候,大部分都是使用postman自測,一些老的項目都是前后端不分離,這樣不會發生跨域的問題。
跨域的問題就是請求發起方和接收方不在同一個域。
協議不同,域名不同(localhost和127.0.0.1也不同),端口不同都屬于跨域
在這里插入圖片描述

跨源資源共享 (CORS)(或通俗地譯為跨域資源共享)是一種基于 HTTP 頭的機制,該機制通過允許服務器標示除了它自己以外的其它 origin(域,協議和端口),使得瀏覽器允許這些 origin 訪問加載自己的資源。跨源資源共享還通過一種機制來檢查服務器是否會允許要發送的真實請求,該機制通過瀏覽器發起一個到服務器托管的跨源資源的"預檢"請求。在預檢中,瀏覽器發送的頭中標示有 HTTP 方法和真實請求中會用到的頭。

但是有的時候并不會觸發跨域問題
當使用簡單請求

  1. GET
  2. HEAD
  3. POST
    Content Type 也僅屬于
  4. text/plain
  5. multipart/form-data
  6. application/x-www-form-urlencoded

否則會發送預檢請求(一個options請求)
如果預檢請求沒有通過,那么后面真正的請求是不會發送的

在springboot中解決跨域問題有三種辦法

  1. 在controller的方法或者類加上 @CrossOrigin(origins = "*")注解
@RestController
@CrossOrigin(origins = "*")
public class HelloController {
 
    @RequestMapping("/hello")
    public String hello() {
        return "hello world";
    }
}
  1. 配置跨域過濾器
@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;
    }
}
  1. 實現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);
   }
}
  1. 手動設置單次請求的響應頭

@RequestMapping("/index")
public String index(HttpServletResponse response) {
 
    response.addHeader("Access-Allow-Control-Origin","*");
    return "index";
}
  1. 通過過濾器,記得在主類加上@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

欄目分類
最近更新