Browse Source
Provides a way to be compliant with RFC 6265 section 4.1.1. See gh-34081pull/34429/head
6 changed files with 84 additions and 56 deletions
@ -0,0 +1,33 @@ |
|||||||
|
package org.springframework.http.support; |
||||||
|
|
||||||
|
import org.springframework.http.ResponseCookie; |
||||||
|
|
||||||
|
import java.net.HttpCookie; |
||||||
|
import java.util.regex.Matcher; |
||||||
|
import java.util.regex.Pattern; |
||||||
|
import java.util.stream.Stream; |
||||||
|
|
||||||
|
import org.jspecify.annotations.Nullable; |
||||||
|
|
||||||
|
public final class DefaultHttpCookieParser implements HttpCookieParser { |
||||||
|
|
||||||
|
private static final Pattern SAME_SITE_PATTERN = Pattern.compile("(?i).*SameSite=(Strict|Lax|None).*"); |
||||||
|
|
||||||
|
@Override |
||||||
|
public Stream<ResponseCookie> parse(String header) { |
||||||
|
Matcher matcher = SAME_SITE_PATTERN.matcher(header); |
||||||
|
String sameSite = (matcher.matches() ? matcher.group(1) : null); |
||||||
|
return HttpCookie.parse(header).stream().map(cookie -> toResponseCookie(cookie, sameSite)); |
||||||
|
} |
||||||
|
|
||||||
|
private static ResponseCookie toResponseCookie(HttpCookie cookie, @Nullable String sameSite) { |
||||||
|
return ResponseCookie.from(cookie.getName(), cookie.getValue()) |
||||||
|
.domain(cookie.getDomain()) |
||||||
|
.httpOnly(cookie.isHttpOnly()) |
||||||
|
.maxAge(cookie.getMaxAge()) |
||||||
|
.path(cookie.getPath()) |
||||||
|
.secure(cookie.getSecure()) |
||||||
|
.sameSite(sameSite) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,10 @@ |
|||||||
|
package org.springframework.http.support; |
||||||
|
|
||||||
|
import org.springframework.http.ResponseCookie; |
||||||
|
|
||||||
|
import java.util.stream.Stream; |
||||||
|
|
||||||
|
public interface HttpCookieParser { |
||||||
|
|
||||||
|
Stream<ResponseCookie> parse(String header); |
||||||
|
} |
||||||
Loading…
Reference in new issue