springboot解决前后端数据跨域问题

springboot解决跨域问题

创建一个配置类,实现WebMvcConfigurer接口,重写addCorsMappings方法

具体如下

package com.xu.page.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 CrosConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }
}

你可能感兴趣的:(笔记,spring,boot,java)