• Java后台解决跨域问题


    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 {
    
        public CorsConfig() {
        }
    
        @Bean
        public CorsFilter corsFilter(){
            //添加cors配置信息
            CorsConfiguration config = new CorsConfiguration();
            config.addAllowedOrigin("http://localhost:8080");
            config.addAllowedOrigin("http://localhost:8085");
            config.addAllowedOrigin("http://localhost:80");
    
            //设置是否发送cookie信息
            config.setAllowCredentials(true);
    
            //设置允许请求方式
            config.addAllowedMethod("*");
    
            //设置允许的header
            config.addAllowedHeader("*");
    
            //为url添加映射路径
            UrlBasedCorsConfigurationSource corsSource = new UrlBasedCorsConfigurationSource();
            corsSource.registerCorsConfiguration("/**",config);
    
            //返回重新定义好的corsSource
            return new CorsFilter(corsSource);
        }
    }
  • 相关阅读:
    查看Linux内核版本命令
    身份证测试用
    aaa
    aaaaaaaaaaaaaaa
    https
    httpclient调用https
    共阴极数码管编码
    时间单位换算
    Java 中System里getProperty(something)
    VM arguments
  • 原文地址:https://www.cnblogs.com/wxianing/p/13703021.html
Copyright © 2020-2023  润新知