Sprng Boot 微服务配置 跨域请求


pom.xml


跨域 依赖包

 
            javax.servlet
            javax.servlet-api
            3.1.0

创建 config 文件夹=> 创建 CorsConfig 跨域配置 Class

package com.demo.service.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("*")
                .maxAge(3600);
    }
}  

@Configuration

Spring 容器在启动时,会加载默认的一些 PostPRocessor,其中就有 ConfigurationClassPostProcessor,这个后置处理程序专门处理带有 @Configuration 注解的类,这个程序会在 bean 定义加载完成后,在 bean 初始化前进行处理。主要处理的过程就是使用 cglib 动态代理增强类,而且是对其中带有 @Bean 注解的方法进行处理。

你可能感兴趣的:(Spring,Boot,跨域,cors,跨域,跨域,Access-Contr,CORS,跨域,spring,boot,ajax)