zoukankan      html  css  js  c++  java
  • java跨域解决

    import java.util.ArrayList;
    import java.util.List;
    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 org.springframework.web.servlet.config.annotation.CorsRegistration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    public class CorsConfig
      extends WebMvcConfigurerAdapter
    {
      public void addCorsMappings(CorsRegistry registry)
      {
        registry.addMapping("/**").allowedOrigins(new String[] { "*" }).allowCredentials(true).allowedMethods(new String[] { "GET", "HEAD", "POST", "DELETE", "PUT", "OPTIONS" }).maxAge(3600L);
      }
      
      private CorsConfiguration buildConfig()
      {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        List<String> list = new ArrayList();
        list.add("*");
        corsConfiguration.setAllowedOrigins(list);
        
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
      }
      
      @Bean
      public CorsFilter corsFilter()
      {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
      }
    }
  • 相关阅读:
    SDN第七次上机作业
    SDN第六次上机作业
    SDN第五次上机实验
    SDN阅读作业(二)
    软件评测——腾讯音视频
    SDN上机第四次作业
    SDN上机第三次作业
    SDN阅读作业
    SpringBoot整合Swagger2
    JavaWeb项目前后端分离
  • 原文地址:https://www.cnblogs.com/shihaiming/p/9474606.html
Copyright © 2011-2022 走看看