改了@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")和xhrFields:{withCredentials:true},
这两个地方还是null,springboot版本是2.0前端代码要加上这俩:
xhrFields: {withCredentials: true},
crossDomain: true,后端如果启动不了,就这么写
@CrossOrigin(origins = "http://localhost:63342",allowCredentials = "true", allowedHeaders = "*")
我的跨域问题就这么解决的
前端代码要加上这俩:
xhrFields: {withCredentials: true},后端如果启动不了,就这么写
@CrossOrigin(origins = "http://localhost:63342",allowCredentials = "true", allowedHeaders = "*")
我的跨域问题就这么解决的
springboot2.6.x版本可以如下设置,在application.properties中设置
server.servlet.session.cookie.same-site=none server.servlet.session.cookie.secure=true 配置UserController上的@CrossOrigin @CrossOrigin(originPatterns = "*", allowCredentials = "true")
他妈的搞了好几天终于解决这个问题了,帖子里在response里设置header里set-cookie的不会生效试过了,响应头里会有两个set-cookie不知道为什么,可能是chorme版本问题。视频spring里使用的是2.0.5.RELEASE版本
这个版本找不到sameSite属性,我将springboot升级到然后设置一个config类,在config类中设置如下的配置
@Bean
public CookieSerializer httpSessionIdResolver(){
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
// 源码默认为Lax
// private String sameSite = "Lax";
cookieSerializer.setSameSite("None");
cookieSerializer.setUseSecureCookie(true);
return cookieSerializer;
}
亲测有用,被这个bug折磨死了!!!!!responseCookie找不到怎么办,这儿报红

这个是根据楼上回答写的,主要的原因是springboot里面多加了samesite这个设置,需要降低其等级。具体原理,可以自己百度一下这个参数。
在类中加入
@Autowired private HttpServletResponse httpServletResponse;
之后,在接口中设置samesite=None, httponly,secure等属性
ResponseCookie cookie = ResponseCookie.from("JSESSIONID", httpServletRequest.getSession().getId() ) // key & value
.httpOnly(true) // 禁止js读取
.secure(true) // 在http下也传输
.domain("localhost")// 域名
.path("/") // path
.maxAge(3600) // 1个小时候过期
.sameSite("None") // 大多数情况也是不发送第三方 Cookie,但是导航到目标网址的 Get 请求除外
.build()
;
httpServletResponse.setHeader(HttpHeaders.SET_COOKIE, cookie.toString());我也有这个问题,不知道怎么解决,求教
@CrossOrigin(origins = {"*"}, allowCredentials = "true")试试这个,我用这个可以