获取session
获取request
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
使用reques获取session
HttpSession session = request.getSession()
给session添加数据
session.setAttribute("user", user);
获取session内的数据
session.getAttribute("user")
解决springboot取得session每次都不一样!!!
处理跨域请求时:
先创建一个配置类
@Configuration
public class MyConfigration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
.allowCredentials(true) //设置为true
.maxAge(3600);;
}
}
发送请求时:
vue项目设置:
axios.defaults.withCredentials = true
设置位置:导入axios的地方(main.js)
Jquery项目设置:
xhrFields:{
withCredentials:true
}