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

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

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

spring-boot設(shè)置跨域訪問方式

作者:小黑孩666 更新時間: 2022-07-22 編程語言

一、創(chuàng)建一個配置類。實現(xiàn)WebMvcConfigurer接口

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}
二、還可以繼承WebSecurityConfigurerAdapter類,然后重寫configure方法
 String[] url={
          "/admins/login","/doc.html","/**/*.css","/**/*.js","/swagger-resources","                /v2/api-docs"
        };

        http.cors();

        http.csrf().disable();//禁止防止跨域請求,如果無此配置,白名單路徑的異步訪問也會出現(xiàn)403錯誤
        http.authorizeRequests() //請求需要被授權(quán)才可以訪問
                .antMatchers(url)//匹配某些路徑
                .permitAll()//允許直接訪問
                .anyRequest()//
                .authenticated();

                

原文鏈接:https://blog.csdn.net/xiaoheihai666/article/details/125684854

欄目分類
最近更新