美文网首页
SpringBoot Web 跨域访问

SpringBoot Web 跨域访问

作者: 调雨为酥 | 来源:发表于2019-04-19 10:00 被阅读0次

import org.springframework.boot.web.servlet.FilterRegistrationBean;

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;

import javax.servlet.Filter;

@Configuration

public class GlobalCorsConfig {

@Bean

  public FilterRegistrationBeancorsFilter() {

UrlBasedCorsConfigurationSource source =new UrlBasedCorsConfigurationSource();

    CorsConfiguration config =new CorsConfiguration();

    config.setAllowCredentials(true);

    config.addAllowedOrigin("*");

    config.addAllowedHeader("*");

    config.addAllowedMethod("*");

//  config.addExposedHeader("x-auth-token");

//      config.addExposedHeader("x-total-count");

//      config.addExposedHeader("Authorization");

//      config.addExposedHeader("Authorization-Ticket");

    source.registerCorsConfiguration("/**", config);

    FilterRegistrationBean bean =new FilterRegistrationBean(new CorsFilter(source));

    bean.setOrder(0);

    return bean;

  }

}

相关文章

网友评论

      本文标题:SpringBoot Web 跨域访问

      本文链接:https://www.haomeiwen.com/subject/umnfgqtx.html