springboot处理跨域请求(PUT,DELETE,GET,POST,OPTIONS)

第一种配置 :在Application.java文件内注入bean     

 

         @Bean
	    public WebMvcConfigurer corsConfigurer() {
	        return new WebMvcConfigurerAdapter() {
	            @Override
	            public void addCorsMappings(CorsRegistry registry) {
	                registry.addMapping("/**")
	                .allowedOrigins("*")
	    			.allowedMethods("PUT", "DELETE","GET","POST")
	                .allowedHeaders("*")
	    			.exposedHeaders("access-control-allow-headers",
	    					"access-control-allow-methods",
	    					"access-control-allow-origin",
	    					"access-control-max-age",
	    					"X-Frame-Options")
	    			.allowCredentials(false).maxAge(3600);
	            }
	        };
	  }

第二种另起配置类:

 


/**
 * 全局跨域配置
 */
@Configuration
public class GlobalCorsConfig {

    /**
     * 允许跨域调用的过滤器
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        //允许所有域名进行跨域调用
        config.addAllowedOrigin("*");
        //允许跨越发送cookie
        config.setAllowCredentials(true);
        //放行全部原始头信息
        config.addAllowedHeader("*");
        //允许所有请求方法跨域调用
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

}

 

你可能感兴趣的:(跨域)