转载自 https://springboot.io/t/topic/2602
早点看到就好了,按照之前的SpringBoot配置跨域,只有火狐可以通过。
谷歌和内核一样的Edge都失败了。
也是之前知道谷歌增加了个啥,可以通过配置浏览器解决。但是总不能每访问网站的人都
配置自己的浏览器吧
今天打开看了一下,发现有个警告
Edge报的警告。然后我搜到了这个文章。
方法一:服务端设置
Set-cookie: key=value; SameSite=None; Secure
Set-cookie: key=value; Secure
方法二:使用Chrome浏览器打开
chrome://flags/#same-site-by-default-cookies
把SameSite by default cookies设置为disabled,重启浏览器即可正常使用
Cookie除了key和value以外有几个属性。
- httpOnly 是否允许js读取cookie
- secure 是否仅仅在https的链接下,才提交cookie
- domain cookie提交的域
- path cookie提交的path
- maxAge cookie存活时间
- sameSite 同站策略,枚举值:Strict Lax None
其他的都很熟悉了,最后一个是 Chrome 51 开始,浏览器的 Cookie 新增加了一个 SameSite 属性,用来防止 CSRF 攻击和用户追踪。
关于SameSite的详细解释 可以看 Cookie 的 SameSite 属性 44
在Javaweb应用中 ,设置 Cookie一般都是用 javax.servlet.http.Cookie,但是SameSite属性出来不久,Servlet库还没更新,所以没有设置SameSite的方法.
javax.servlet.http.Cookie 中定义的的属性
可以看到,还没有SameSite的定义
//
// The value of the cookie itself.
//
private String name; // NAME= ... "$Name" style is reserved
private String value; // value of NAME
//
// Attributes encoded in the header's cookie fields.
//
private String comment; // ;Comment=VALUE ... describes cookie's use
// ;Discard ... implied by maxAge < 0
private String domain; // ;Domain=VALUE ... domain that sees cookie
private int maxAge = -1; // ;Max-Age=VALUE ... cookies auto-expire
private String path; // ;Path=VALUE ... URLs that see the cookie
private boolean secure; // ;Secure ... e.g. use SSL
private int version = 0; // ;Version=1 ... means RFC 2109++ style
private boolean isHttpOnly = false;
通过 ResponseCookie 给客户端设置Cookie
本质上,Cookie也只是一个header。我们可以不使用Cookie对象,而通过自定义Header的方式来给客户端设置Cookie。
ResponseCookie 是Spring定义的一个Cookie构建工具类,极其简单
import java.time.Duration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping
public class TestController {
@GetMapping("/test")
public Object test (HttpServletRequest request,
HttpServletResponse response) throws Exception {
ResponseCookie cookie = ResponseCookie.from("myCookie", "myCookieValue") // key & value
.httpOnly(true) // 禁止js读取
.secure(false) // 在http下也传输
.domain("localhost")// 域名
.path("/") // path
.maxAge(Duration.ofHours(1)) // 1个小时候过期
.sameSite("Lax") // 大多数情况也是不发送第三方 Cookie,但是导航到目标网址的 Get 请求除外
.build()
;
// 设置Cookie Header
response.setHeader(HttpHeaders.SET_COOKIE, cookie.toString());
return "ok";
}
}
响应给客户端的Cookie
所有属性都响应正确 √
HttpSession Cookie 的SameSite属性
HttpSession依赖一个名称叫做JSESSIONID(默认名称)的Cookie。
对于JSESSIONID Cookie 的设置,可以修改如下配置。但是,目前spring也没实现SameSite的配置项。
配置类 : org.springframework.boot.web.servlet.server.Cookie
server.servlet.session.cookie.comment
server.servlet.session.cookie.domain
server.servlet.session.cookie.http-only
server.servlet.session.cookie.max-age
server.servlet.session.cookie.name
server.servlet.session.cookie.path
server.servlet.session.cookie.secure
通过修改容器的配置,对Session Cookie设置SameSite属性
Tomcat 只有8/9更高版本
import org.apache.tomcat.util.http.Rfc6265CookieProcessor;
import org.apache.tomcat.util.http.SameSiteCookies;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TomcatConfiguration {
@Bean
public TomcatContextCustomizer sameSiteCookiesConfig() {
return context -> {
final Rfc6265CookieProcessor cookieProcessor = new Rfc6265CookieProcessor();
// 设置Cookie的SameSite
cookieProcessor.setSameSiteCookies(SameSiteCookies.LAX.getValue());
context.setCookieProcessor(cookieProcessor);
};
}
}
Spring Session的SameSite属性
通过自定义 CookieSerializer 设置 SameSite属性
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.CookieSerializer;
import com.video.common.spring.session.DynamicCookieMaxAgeCookieSerializer;
@Configuration
public class SpringSessionConfiguration {
@Bean
public CookieSerializer cookieSerializer() {
DynamicCookieMaxAgeCookieSerializer serializer = new DynamicCookieMaxAgeCookieSerializer();
serializer.setCookieName("JSESSIONID");
serializer.setDomainName("localhost");
serializer.setCookiePath("/");
serializer.setCookieMaxAge(3600);
serializer.setSameSite("Lax"); // 设置SameSite属性
serializer.setUseHttpOnlyCookie(true);
serializer.setUseSecureCookie(false);
return serializer;
}
}