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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

spring boot 支持跨域配置

作者:郭俊強(qiáng) 更新時(shí)間: 2023-07-16 編程語(yǔ)言

新建java類:復(fù)制下面即可、注意包名和類名

package com.example.start.CORSConfig;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CORSConfiguration implements WebMvcConfigurer {
    static final String ORIGINS[] = new String[] { "GET", "POST", "PUT", "DELETE" };

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有的當(dāng)前站點(diǎn)的請(qǐng)求地址,都支持跨域訪問。
                .allowedOriginPatterns("*") // 所有的外部域都可跨域訪問。 如果是localhost則很難配置,因?yàn)樵诳缬蛘?qǐng)求的時(shí)候,外部域的解析可能是localhost、127.0.0.1、主機(jī)名
                .allowCredentials(true) // 是否支持跨域用戶憑證
                .allowedMethods(ORIGINS) // 當(dāng)前站點(diǎn)支持的跨域請(qǐng)求類型是什么
                .maxAge(3600); // 超時(shí)時(shí)長(zhǎng)設(shè)置為1小時(shí)。 時(shí)間單位是秒。
    }
}

原文鏈接:https://blog.csdn.net/qq_37061571/article/details/121906043

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新