SpringBoot 2.0 前后端分离 跨域方法

在 SpringBoot 中 WebMvcConfigurerAdapter 已过时,现在采用新接口
方法一

package com.love.forever.web;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig implements WebMvcConfigurer {

//解决跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  
                .allowedOrigins("*")  
                .allowCredentials(true)  
                .allowedMethods("GET", "POST", "DELETE", "PUT")  
                .maxAge(3600);  
    }  
 }

方法二采用
注解 @CrossOrigin("*") 我没有成功,需要前后端对应。

你可能感兴趣的:(Spring,框架)