zoukankan      html  css  js  c++  java
  • SpringBoot跨域问题

    对于前后端分离的项目,跨域问题十分明显。同一协议,同一ip,同一端口,三同中有一不同就产生了跨域。解决办法很简单,在后台配置允许跨域即可:

    新建一个配置类CorsConfig,设置其允许跨域,并注入到spring中。

    package com.zys.springbootjwt.config;
    
    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 CorsConfig {
        private CorsConfiguration buildConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            // 1允许服务端访问
            corsConfiguration.addAllowedOrigin("*");
            // 1.1允许本地访问
            corsConfiguration.addAllowedOrigin("*");
            // 2允许任何头
            corsConfiguration.addAllowedHeader("*");
            // 3允许任何方法(post、get等)
            corsConfiguration.addAllowedMethod("*");
            // 4 允许withCredentials报文头
            corsConfiguration.setAllowCredentials(true);
            return corsConfiguration;
        }
    
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", buildConfig());
            return new CorsFilter(source);
        }
    }
  • 相关阅读:
    jquery设置多个css样式
    html中设置透明遮罩层的兼容性代码
    在html中显示Flash的代码
    js setTimeout()
    jquery live hover
    leetcode第16题--3Sum Closest
    leetcode第15题--3Sum
    leetcode第14题--Longest Common Prefix
    leetcode第13题--Roman to Integer
    leetcode第12题--Integer to Roman
  • 原文地址:https://www.cnblogs.com/zys2019/p/14252923.html
Copyright © 2011-2022 走看看