前后端分离跨域问题解决:Access-Control-Allow-Origin

在这里插入图片描述

报错信息:
Access to XMLHttpRequest at 'http://localhost:8091/user/doLogin' from origin 'http://localhost:8090' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
解决:

后端配置全局跨域访问

/**
 * 配置全局跨域
 * @ClassName GlobalCorsConfig
 * @Author 药岩
 * @Date 2020/5/15
 * Version 1.0
 */

@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);
    }
}

你可能感兴趣的:(工作中的那些问题,java)