创建spring-boot项目,首先解决前后端分离的跨域问题。
在项目包下 创建security包,在包下创建类WebConfig
package com.wx.mys.security;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @ Author :wangxu.
* @ Date :Created in 16:00 2018/9/19
* @ Description:
* @ Modified By:
* @Version: $
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
//允许全部请求跨域
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("*").allowCredentials(true);
}
}
实现WebMvcConfigurer接口,实现里面的方法,然后 ,这样,就可以实现跨域了,在前端的ajax请求就可以访问到不是一台服务器的后台接口