使用ResponseCookie來生成Cookie

Spring-web-5.0.4.RELEASE源碼如下,後續又推出了samesite屬性 java // // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.http; import java.time.Duration; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; public final class ResponseCookie extends HttpCookie { private final Duration maxAge; [@Nullable](https://my.oschina.net/u/2896689) private final String domain; [@Nullable](https://my.oschina.net/u/2896689) private final String path; private final boolean secure; private final boolean httpOnly; private ResponseCookie(String name, String value, Duration maxAge, [@Nullable](https://my.oschina.net/u/2896689) String domain, [@Nullable](https://my.oschina.net/u/2896689) String path, boolean secure, boolean httpOnly) { super(name, value); Assert.notNull(maxAge, "Max age must not be null"); this.maxAge = maxAge; this.domain = domain; this.path = path; this.secure = secure; this.httpOnly = httpOnly; } public Duration getMaxAge() { return this.maxAge; } [@Nullable](https://my.oschina.net/u/2896689) public String getDomain() { return this.domain; } @Nullable public String getPath() { return this.path; } public boolean isSecure() { return this.secure; } public boolean isHttpOnly() { return this.httpOnly; } public boolean equals(Object other) { if (this == other) { return true; } else if (!(other instanceof ResponseCookie)) { return false; } else { ResponseCookie otherCookie = (ResponseCookie)other; return this.getName().equalsIgnoreCase(otherCookie.getName()) && ObjectUtils.nullSafeEquals(this.path, otherCookie.getPath()) && ObjectUtils.nullSafeEquals(this.domain, otherCookie.getDomain()); } } public int hashCode() { int result = super.hashCode(); result = 31 * result + ObjectUtils.nullSafeHashCode(this.domain); result = 31 * result + ObjectUtils.nullSafeHashCode(this.path); return result; } public String toString() { StringBuilder sb = new StringBuilder(); sb.append(this.getName()).append('=').append(this.getValue()); if (StringUtils.hasText(this.getPath())) { sb.append("; Path=").append(this.getPath()); } if (StringUtils.hasText(this.domain)) { sb.append("; Domain=").append(this.domain); } if (!this.maxAge.isNegative()) { sb.append("; Max-Age=").append(this.maxAge); sb.append("; Expires="); HttpHeaders headers = new HttpHeaders(); long seconds = this.maxAge.getSeconds(); headers.setExpires(seconds > 0L ? System.currentTimeMillis() + seconds : 0L); sb.append(headers.getFirst("Expires")); } if (this.secure) { sb.append("; Secure"); } if (this.httpOnly) { sb.append("; HttpOnly"); } return sb.toString(); } public static ResponseCookie.ResponseCookieBuilder from(final String name, final String value) { return new ResponseCookie.ResponseCookieBuilder() { private Duration maxAge = Duration.ofSeconds(-1L); @Nullable private String domain; @Nullable private String path; private boolean secure; private boolean httpOnly; public ResponseCookie.ResponseCookieBuilder maxAge(Duration maxAge) { this.maxAge = maxAge; return this; } public ResponseCookie.ResponseCookieBuilder maxAge(long maxAgeSeconds) { this.maxAge = maxAgeSeconds >= 0L ? Duration.ofSeconds(maxAgeSeconds) : Duration.ofSeconds(-1L); return this; } public ResponseCookie.ResponseCookieBuilder domain(String domain) { this.domain = domain; return this; } public ResponseCookie.ResponseCookieBuilder path(String path) { this.path = path; return this; } public ResponseCookie.ResponseCookieBuilder secure(boolean secure) { this.secure = secure; return this; } public ResponseCookie.ResponseCookieBuilder httpOnly(boolean httpOnly) { this.httpOnly = httpOnly; return this; } public ResponseCookie build() { return new ResponseCookie(name, value, this.maxAge, this.domain, this.path, this.secure, this.httpOnly); } }; } public interface ResponseCookieBuilder { ResponseCookie.ResponseCookieBuilder maxAge(Duration var1); ResponseCookie.ResponseCookieBuilder maxAge(long var1); ResponseCookie.ResponseCookieBuilder path(String var1); ResponseCookie.ResponseCookieBuilder domain(String var1); ResponseCookie.ResponseCookieBuilder secure(boolean var1); ResponseCookie.ResponseCookieBuilder httpOnly(boolean var1); ResponseCookie build(); } } 使用方法,這樣就可以獲取一個cookie字符串

捕捉異常,因爲cookie的value有特殊限制,所以代碼裏也做了判斷,當調用build方法是就會進行校驗

public HttpCookie(String name, @Nullable String value) { 
Assert.hasLength(name, "'name' is required and must not be empty."); 
this.name = name; this.value = value != null ? value : "";
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章