0
点赞
收藏
分享

微信扫一扫

Spring boot设置允许跨域


跨域永远是后台开发遇到的一个永恒问题,使用Spring boot开始时需要进行跨域,查看了网上的一些方法,说的都不是非常清楚,不适合刚刚入门的新手,这里我总结一下:

新建类

package com.example.demo;

import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class NetWorkConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }
}

使用

在Spring boot启动Application中加入下面代码:

@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
//    加入的代码在这里
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new NetWorkConfig();
    }
}

 

举报

相关推荐

0 条评论