SpringBoot 后台服务允许跨域
作者:互联网
1.通过 @CrossOrigin 解决跨域问题
在Controller 上增加@CrossOrigin 注解
@CrossOrigin
@RestController
@RequestMapping("/user")
@Api(value = "用户测试模块")
public class UsersController {
}
但是每个Controller 都需要增加,比较麻烦,可以增加一个BaseController,设置BaseController允许跨域
@CrossOrigin
public class BaseController {
}
@RestController
@RequestMapping("/user")
@Api(value = "用户测试模块")
public class UsersController extends BaseController {
}
2、通过CorsConfiguration 设置允许跨域
@Configuration
public class CorsConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(false)
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.allowedOrigins("*");
}
};
}
}
标签:SpringBoot,RestController,CrossOrigin,class,跨域,后台,public,BaseController 来源: https://blog.csdn.net/weixin_41003771/article/details/115136660