CorsConfig.java 4 KB
package com.glsk.config;



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 解决异步访问跨域
 */
//@Configuration
//public class CorsConfig  {
//
//    @Bean
//    public WebMvcConfigurer corsConfigurer(){
//        return new WebMvcConfigurer() {
//            @Override
//            public void addCorsMappings(CorsRegistry registry) {
//                registry.addMapping("/**")
//                        .allowedHeaders("*")
//                        .allowedMethods("*")
//                        .maxAge(86400L)
//                        .allowedOrigins("*");
//            }
//        };
//    }
//
//
////    public class CorsConfig implements WebMvcConfigurer {
////    @Override
////    public void addCorsMappings(CorsRegistry registry) {
////        //本应用的所有方法都会去处理跨域请求
////        registry.addMapping("/**")
////                .allowedOrigins("*")
////                //允许远端访问的域名
//////                .allowedOrigins("http://192.168.1.149:9527")
////                .allowedOrigins("*")
////                //允许请求的方法("POST", "GET", "PUT", "OPTIONS", "DELETE")
////                .allowedMethods("*")
////                //允许请求头
////                .allowedHeaders("*");
////
////
////
////    }
////
////
////
////    private CorsConfiguration buildConfig() {
////        CorsConfiguration corsConfiguration = new CorsConfiguration();
////        corsConfiguration.setAllowCredentials(true);  //sessionid 多次访问一致
////
////        // 允许访问的客户端域名
////        List<String> allowedOriginPatterns = new ArrayList<>();
////        allowedOriginPatterns.add("*");
////
//////        corsConfiguration.setAllowedOriginPatterns(allowedOriginPatterns);
////        corsConfiguration.setAllowedOrigins(allowedOriginPatterns);
////        corsConfiguration.addAllowedOrigin("*"); // 允许任何域名使用
////        corsConfiguration.addAllowedHeader("*"); // 允许任何头
////        corsConfiguration.addAllowedMethod("*"); // 允许任何方法(post、get等)
////        return corsConfiguration;
////    }
////
////    @Bean
////    public CorsFilter corsFilter() {
////        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
////        source.registerCorsConfiguration("/**", buildConfig()); // 对接口配置跨域设置
////
////        CorsConfiguration corsConfiguration = new CorsConfiguration();
////        corsConfiguration.addAllowedHeader("*");
////        corsConfiguration.addAllowedOrigin("*");
////        corsConfiguration.addAllowedMethod("*");
////        corsConfiguration.setAllowCredentials(true);
////        source.registerCorsConfiguration("/**",corsConfiguration);
////
////
////        CorsFilter corsFilter = new CorsFilter(source);
////
////
////        return corsFilter;//new CorsFilter(source)
////    }
////
//
//}


/**
 * 跨域配置
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {
 @Bean
 public WebMvcConfigurer corsConfigurer() {

        return new WebMvcConfigurer() {
         @Override
          public void addCorsMappings(CorsRegistry registry) {
             registry.addMapping("/**").
             allowedOrigins("*"). //允许跨域的域名,可以用*表示允许任何域名使用
//             allowedOrigins("https://www.dustyblog.cn"). //允许跨域的域名,可以用*表示允许任何域名使用
             allowedMethods("*"). //允许任何方法(post、get等)
             allowedHeaders("*"). //允许任何请求头
             allowCredentials(true). //带上cookie信息
            exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
         }
 };
 }


}