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

Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪問(wèn)跨域問(wèn)題

 更新時(shí)間:2023年04月04日 08:53:29   作者:ForestSpringH  
這篇文章主要介紹了Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪問(wèn)跨域問(wèn)題,關(guān)于Cors跨域的問(wèn)題,前端有代理和jsonp的常用方式解決這種非同源的訪問(wèn)拒絕策略

關(guān)于Cors跨域的問(wèn)題,前端有代理和jsonp的常用方式解決這種非同源的訪問(wèn)拒絕策略,什么是同源?即域名一致端口一致但是端口下訪問(wèn)的接口api不同的兩種或者幾種的互相訪問(wèn)叫做同源訪問(wèn),但是若是接口不一致或者域名不一致(這里泛指IP不一致),那么對(duì)應(yīng)的就屬于非同源訪問(wèn),瀏覽器會(huì)拒絕發(fā)出請(qǐng)求,直接回復(fù)404,有時(shí)候我也見過(guò)恢復(fù)202的就是發(fā)出去了但是被后端的Mvc處理hander鏈給拒絕了。那么配置MVC是后端處理Cors問(wèn)題的一種解決思路。

之前學(xué)習(xí)過(guò)MVC的處理鏈路,從一次請(qǐng)求發(fā)過(guò)來(lái)到回復(fù)數(shù)據(jù)總共11次處理:

請(qǐng)求發(fā)送到服務(wù)器端時(shí)是由我們的MVC進(jìn)行處理的,而統(tǒng)一調(diào)配任務(wù)流程的則是我們的請(qǐng)求分發(fā)器,注意這里請(qǐng)求到處理器之后回去尋找處理器適配器(符合校驗(yàn)處理的請(qǐng)求才能被允許例如接口含有的合法api,以及跨域原則),之前我們的微信小程序開發(fā)過(guò)程中是沒(méi)有考慮跨域問(wèn)題的,原因是我們知道小程序的請(qǐng)求處理都是由微信后臺(tái)進(jìn)行分發(fā)處理的,也就是在微信的后臺(tái)時(shí)就做了前端的跨域處理,大概是采用動(dòng)態(tài)代理的方式解決了小程序的跨域。

那么我們先看看MVC的配置接口 WebMvcConfigurer 的源代碼:

public interface WebMvcConfigurer {
    default void configurePathMatch(PathMatchConfigurer configurer) {
    }
    default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    }
    default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
    }
    default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    }
    default void addFormatters(FormatterRegistry registry) {
    }
    default void addInterceptors(InterceptorRegistry registry) {
    }
    default void addResourceHandlers(ResourceHandlerRegistry registry) {
    }
    default void addCorsMappings(CorsRegistry registry) {
    }
    default void addViewControllers(ViewControllerRegistry registry) {
    }
    default void configureViewResolvers(ViewResolverRegistry registry) {
    }
    default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
    }
    default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
    }
    default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    }
    default void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    }
    default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
    }
    default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
    }
    @Nullable
    default Validator getValidator() {
        return null;
    }
    @Nullable
    default MessageCodesResolver getMessageCodesResolver() {
        return null;
    }
}

它的內(nèi)部是具備一些處理器解析器以及映射的添加與配置的方法的,那么我們要解決Cros跨域問(wèn)題就是要考慮addCorsMappings 配置Cros映射,所以我們點(diǎn)進(jìn)去看看這注冊(cè)Cros的 CorsRegistry 的源碼:

public class CorsRegistry {
    private final List<CorsRegistration> registrations = new ArrayList();
    public CorsRegistry() {
    }
    public CorsRegistration addMapping(String pathPattern) {
        CorsRegistration registration = new CorsRegistration(pathPattern);
        this.registrations.add(registration);
        return registration;
    }
    protected Map<String, CorsConfiguration> getCorsConfigurations() {
        Map<String, CorsConfiguration> configs = CollectionUtils.newLinkedHashMap(this.registrations.size());
        Iterator var2 = this.registrations.iterator();
        while(var2.hasNext()) {
            CorsRegistration registration = (CorsRegistration)var2.next();
            configs.put(registration.getPathPattern(), registration.getCorsConfiguration());
        }
        return configs;
    }
}

從上述代碼中不難發(fā)現(xiàn),內(nèi)部有一個(gè)不可改變的 CorsRegistration 數(shù)組鏈表,以及增加映射的方法,主要還是看看它具備的元素 CorsRegistration 含有什么配置項(xiàng):

public class CorsRegistration {
    private final String pathPattern;
    private CorsConfiguration config;
    public CorsRegistration(String pathPattern) {
        this.pathPattern = pathPattern;
        this.config = (new CorsConfiguration()).applyPermitDefaultValues();
    }
    public CorsRegistration allowedOrigins(String... origins) {
        this.config.setAllowedOrigins(Arrays.asList(origins));
        return this;
    }
    public CorsRegistration allowedOriginPatterns(String... patterns) {
        this.config.setAllowedOriginPatterns(Arrays.asList(patterns));
        return this;
    }
    public CorsRegistration allowedMethods(String... methods) {
        this.config.setAllowedMethods(Arrays.asList(methods));
        return this;
    }
    public CorsRegistration allowedHeaders(String... headers) {
        this.config.setAllowedHeaders(Arrays.asList(headers));
        return this;
    }
    public CorsRegistration exposedHeaders(String... headers) {
        this.config.setExposedHeaders(Arrays.asList(headers));
        return this;
    }
    public CorsRegistration allowCredentials(boolean allowCredentials) {
        this.config.setAllowCredentials(allowCredentials);
        return this;
    }
    public CorsRegistration maxAge(long maxAge) {
        this.config.setMaxAge(maxAge);
        return this;
    }
    public CorsRegistration combine(CorsConfiguration other) {
        this.config = this.config.combine(other);
        return this;
    }
    protected String getPathPattern() {
        return this.pathPattern;
    }
    protected CorsConfiguration getCorsConfiguration() {
        return this.config;
    }
}

我們可以發(fā)現(xiàn)內(nèi)部是具備允許放行:請(qǐng)求頭,請(qǐng)求路徑,請(qǐng)求方法,請(qǐng)求源策略的方法的,所以我們?cè)谶@里的 重寫addCorsMappings方法配置一個(gè) CorsRegistry 添加相應(yīng)的路徑方法與請(qǐng)求策略放行不就可以解決跨域的問(wèn)題了?

我們寫一個(gè)WebMvcConfig配置類實(shí)現(xiàn)剛剛研究的WebMvcConfigurer接口重寫addCrosMappings配置CrosRegistry即可(或者在api與Controller控制類上打上@CrossOrigin注解也可以解決問(wèn)題(注解默認(rèn)放行所有來(lái)源的請(qǐng)求)):

/**
 * 配置前端跨域訪問(wèn)請(qǐng)求
 */
@Configuration
public class WbMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
       registry.addMapping("/**")
               .allowedHeaders("Content-Type","X-Request-With","Access-Control-Request-Method","Access-Control-Request-Headers","token")
               .allowedMethods("*")
               .allowedOriginPatterns("*")
               /*注意當(dāng)這個(gè)配置為真是我們不能將允許源設(shè)置為*而是將源路徑設(shè)置為*即可*/
               .allowCredentials(true);
    }
    @Bean
    public FormContentFilter httpPutFormContentFilter(){
        return new FormContentFilter();
    }
}

我們利用axios寫一個(gè)簡(jiǎn)單的請(qǐng)求發(fā)送按鈕:

    <input type="button" value="get" class="get">
    <script>
        document.querySelector(".get").onclick = function () {
            // 跨域一般是是后端解決的事情
            axios.get("http://127.0.0.1:8080/all").then(
                function (response) {
                    console.log(response)
                }
            )
        }
    </script>

再用SpringBoot寫一個(gè)簡(jiǎn)單的controller的api:

@RestController
public class testController {
    @Autowired
    private ProductServiceImpl productService;
    @GetMapping("/all")
    @ResponseBody
    public List<Product> all() {
        Page<Product> page = productService.page(1L);
        List<Product> productList = new LinkedList<>();
        productList.add(page.getRecords().iterator().next());
        return productList;
    }
}

這里我們?cè)跒g覽器打開5050端口下的這個(gè)html文件就可以點(diǎn)擊按鈕訪問(wèn)接口了:

這里可以看到請(qǐng)求訪問(wèn)數(shù)據(jù)成功了!

到此這篇關(guān)于Springboot通過(guò)配置WebMvcConfig處理Cors非同源訪問(wèn)跨域問(wèn)題的文章就介紹到這了,更多相關(guān)Springboot Cors非同源訪問(wèn)跨域內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 以武俠形式理解Java LinkedList源碼

    以武俠形式理解Java LinkedList源碼

    鏈表(Linked list)是一種常見的基礎(chǔ)數(shù)據(jù)結(jié)構(gòu),是一種線性表,但是并不會(huì)按線性的順序存儲(chǔ)數(shù)據(jù),而是在每一個(gè)節(jié)點(diǎn)里存到下一個(gè)節(jié)點(diǎn)的地址
    2021-11-11
  • SpringCloud與Consul集成實(shí)現(xiàn)負(fù)載均衡功能

    SpringCloud與Consul集成實(shí)現(xiàn)負(fù)載均衡功能

    負(fù)載均衡基本概念有:實(shí)服務(wù)、實(shí)服務(wù)組、虛服務(wù)、調(diào)度算法、持續(xù)性等,其常用應(yīng)用場(chǎng)景主要是服務(wù)器負(fù)載均衡,鏈路負(fù)載均衡。這篇文章主要介紹了SpringCloud與Consul集成實(shí)現(xiàn)負(fù)載均衡 ,需要的朋友可以參考下
    2018-09-09
  • Java調(diào)用CXF WebService接口的兩種方式實(shí)例

    Java調(diào)用CXF WebService接口的兩種方式實(shí)例

    今天小編就為大家分享一篇關(guān)于Java調(diào)用CXF WebService接口的兩種方式實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • 使用Java代碼實(shí)現(xiàn)RocketMQ的生產(chǎn)與消費(fèi)消息

    使用Java代碼實(shí)現(xiàn)RocketMQ的生產(chǎn)與消費(fèi)消息

    這篇文章介紹一下其他的小組件以及使用Java代碼實(shí)現(xiàn)生產(chǎn)者對(duì)消息的生成,消費(fèi)者消費(fèi)消息等知識(shí)點(diǎn),并通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • Java中的對(duì)象和對(duì)象引用實(shí)例淺析

    Java中的對(duì)象和對(duì)象引用實(shí)例淺析

    這篇文章主要介紹了Java中的對(duì)象和對(duì)象引用,實(shí)例分析了對(duì)象與對(duì)象引用的概念與相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • dockerfile-maven-plugin極簡(jiǎn)教程(推薦)

    dockerfile-maven-plugin極簡(jiǎn)教程(推薦)

    這篇文章主要介紹了dockerfile-maven-plugin極簡(jiǎn)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • JDK 14的新特性:文本塊Text Blocks的使用

    JDK 14的新特性:文本塊Text Blocks的使用

    這篇文章主要介紹了JDK 14的新特性:文本塊Text Blocks的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Java二叉樹中LCA問(wèn)題解決方法兩則

    Java二叉樹中LCA問(wèn)題解決方法兩則

    這篇文章主要介紹了Java二叉樹中LCA問(wèn)題解決方法,總的來(lái)說(shuō)這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過(guò)程。希望通過(guò)這道題能給你帶來(lái)一種解題優(yōu)化的思路
    2022-12-12
  • Springboot處理CORS跨域請(qǐng)求的三種方法

    Springboot處理CORS跨域請(qǐng)求的三種方法

    這篇文章主要介紹了Springboot處理CORS跨域請(qǐng)求的三種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java設(shè)計(jì)模式之單例模式簡(jiǎn)單解析

    Java設(shè)計(jì)模式之單例模式簡(jiǎn)單解析

    這篇文章主要介紹了Java設(shè)計(jì)模式之單例模式簡(jiǎn)單解析,單例模式的優(yōu)點(diǎn)在于在內(nèi)存中某個(gè)類只有一個(gè)實(shí)例,減少了內(nèi)存的開銷,尤其是頻繁的創(chuàng)建和銷毀實(shí)例,避免對(duì)資源的多重暫用,需要的朋友可以參考下
    2023-12-12

最新評(píng)論

青海省| 名山县| 五莲县| 莫力| 页游| 河南省| 徐水县| 田林县| 澜沧| 四会市| 阳信县| 库尔勒市| 万年县| 兴安盟| 郓城县| 凭祥市| 凉城县| 黄石市| 淅川县| 青神县| 雷波县| 荣昌县| 浑源县| 抚松县| 敖汉旗| 遵化市| 丰宁| 北川| 朝阳区| 泰州市| 柏乡县| 个旧市| 垦利县| 安图县| 海晏县| 临湘市| 北宁市| 万荣县| 浮山县| 郁南县| 苍梧县|