springboot 跨域

 

参考:

https://blog.csdn.net/qq779446849/article/details/53102925

https://blog.csdn.net/wo541075754/article/details/50696841

 

springboot启动类中添加2个方法:

    private CorsConfiguration buildConfig() {  
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许跨域访问的域名
        corsConfiguration.addAllowedOrigin("*");// 请求头
        corsConfiguration.addAllowedHeader("*");  
        // 请求方法 HttpMethod.DELETE/POST/GET/PUT/DELETE/OPTIONS
        corsConfiguration.addAllowedMethod("*");  
        // 预检请求的有效期,单位为秒。
        corsConfiguration.setMaxAge(3600L);
        
        return corsConfiguration;  
    }  
      
    /** 
     * 跨域过滤器 
     * @return 
     */  
    @Bean  
    public CorsFilter corsFilter() {  
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  
        source.registerCorsConfiguration("/**", buildConfig()); // 4  
        return new CorsFilter(source);  
    }

 

跨域安全问题:https://www.cnblogs.com/cloudshare/p/7192646.html

posted @ 2018-04-09 16:33  lvlin241  阅读(110)  评论(0编辑  收藏  举报