欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

什么是跨域

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

springboot解決跨域的幾種方式

方法一、SpringBoot的注解@CrossOrigin

直接在Controller方法或者類上增加@CrossOrigin注解,SpringMVC使用@CrossOrigin使用場(chǎng)景要求 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;
        // 設(shè)置允許Cookie
        res.addHeader("Access-Control-Allow-Credentials", "true");
        // 允許http://www.xxx.com域(自行設(shè)置,這里只做示例)發(fā)起跨域請(qǐng)求
        res.addHeader("Access-Control-Allow-Origin", "*");
        // 設(shè)置允許跨域請(qǐng)求的方法
        res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
        // 允許跨域請(qǐng)求包含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);
    }
}

 方式四:實(shí)現(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) // 設(shè)置允許憑證
                .allowedHeaders("*")   // 設(shè)置請(qǐng)求頭
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 設(shè)置允許的方式
                .allowedOriginPatterns("*");
    }
}

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

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

相關(guān)文章

最新評(píng)論