JS跨域请求接口时,默认是不保存cookie的,需要前后端都配置一下。
spring拦截器
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter {
static final String ORIGINS[] = new String[]{"GET", "POST", "PUT", "DELETE"};
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods(ORIGINS)
.maxAge(3600);
}
}
JS请求需要配置withCredentials:true
例如:
$.ajax({
type : "POST",
url : 'https://xxxx:8080/app/company/queryList',
dataType : "JSON",
data : "",
xhrFields: {withCredentials: true},
success : function(res){
}
});
http.post("https://xxxx:8080/app/company/queryList",this.user,{withCredentials:true})
.toPromise()
.then(response=>{console.log(response);return response.text()});
参考资料 https://segmentfault.com/a/1190000012469713
真的很有帮助
感谢楼主的分享,很不错的技术帖子
前两天网站挂了吧
前两天在迁移网站