最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

最新springboot解決跨域的幾種方式小結(jié)

 更新時間:2022年05月15日 15:08:41   作者:myli92  
跨域指的是瀏覽器不能執(zhí)?其他?站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對javascript施加的安全限制,這篇文章主要介紹了springboot解決跨域的幾種方式,需要的朋友可以參考下

什么是跨域

跨域:指的是瀏覽器不能執(zhí)?其他?站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對javascript施加的安全限制。
例如:a頁?想獲取b頁?資源,如果a、b頁?的協(xié)議、域名、端?、?域名不同,所進?的訪問?動都是跨域的,?瀏覽器
為了安全問題?般都限制了跨域訪問,也就是不允許跨域請求資源。注意:跨域限制訪問,其實是瀏覽器的限制。理解這?點
很重要
同源策略:是指協(xié)議,域名,端?都要相同,其中有?個不同都會產(chǎn)?跨域;

springboot解決跨域的幾種方式

方法一、SpringBoot的注解@CrossOrigin

直接在Controller方法或者類上增加@CrossOrigin注解,SpringMVC使用@CrossOrigin使用場景要求 jdk1.8+ Spring4.2+

@GetMapping("/hello")
@CrossOrigin
public String hello() {
        return "hello:" + simpleDateFormat.format(new Date());
}

方式二:使用CorsFilter

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class ConfigConfiguration {
    @Bean
    public CorsFilter CorsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource ub = new UrlBasedCorsConfigurationSource();
        ub.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(ub);
    }
}

方式三:自定義過濾(web  filter)的方式

@Component
public class CustomFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) servletResponse;
        // 設置允許Cookie
        res.addHeader("Access-Control-Allow-Credentials", "true");
        // 允許http://www.xxx.com域(自行設置,這里只做示例)發(fā)起跨域請求
        res.addHeader("Access-Control-Allow-Origin", "*");
        // 設置允許跨域請求的方法
        res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
        // 允許跨域請求包含content-type
        res.addHeader("Access-Control-Allow-Headers", "Content-Type,X-CAF-Authorization-Token,sessionToken,X-TOKEN");
        if (((HttpServletRequest) servletRequest).getMethod().equals("OPTIONS")) {
            servletResponse.getWriter().println("ok");
            return;
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

 方式四:實現(xiàn)WebMvcConfigurer中addCorsMappings方法

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Component
public class MyWebMvcConfigurer implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 匹配所有的路徑
                .allowCredentials(true) // 設置允許憑證
                .allowedHeaders("*")   // 設置請求頭
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 設置允許的方式
                .allowedOriginPatterns("*");
    }
}

 方法五:采用nginx做動態(tài)代理

到此這篇關于springboot解決跨域的幾種方式的文章就介紹到這了,更多相關springboot解決跨域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

昌江| 长武县| 宝鸡市| 昌宁县| 象山县| 陆良县| 会宁县| 大安市| 宜黄县| 彭山县| 通江县| 长宁县| 蓝山县| 辉县市| 东兰县| 商丘市| 社旗县| 瑞昌市| 凌云县| 玉环县| 沙河市| 观塘区| 疏勒县| 天峨县| 依安县| 枣强县| 丰都县| 淅川县| 大宁县| 黄龙县| 从化市| 瓦房店市| 铁力市| 阿克苏市| 遵义县| 泗洪县| 同仁县| 沈丘县| 玛曲县| 遂平县| 蓝田县|