欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SpinrgWebFlux中Cookie的讀寫(xiě)的示例

WebFLux與WebMvc的差異

成都創(chuàng)新互聯(lián)公司專(zhuān)注于做網(wǎng)站、成都網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開(kāi)發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營(yíng)銷(xiāo)中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對(duì)待客戶,用專(zhuān)業(yè)的服務(wù)創(chuàng)造價(jià)值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

WebFlux讀寫(xiě)Cookie不像WebMvc那么直接,最主要的原因是WebMvc是基于Servlet規(guī)范的,而WebFlux僅僅遵守的是HTTP協(xié)議。所以在使用的時(shí)候會(huì)發(fā)現(xiàn)HttpServletRequest、HttpServletResponse這些Servlet層級(jí)的接口根本就無(wú)法使用。

Cookie與Servlet并沒(méi)有太直接的關(guān)系,前者是屬于HTTP規(guī)范的而后者是一個(gè)J2EE的規(guī)范,在應(yīng)用層面僅有的聯(lián)系就是Servlet會(huì)讀寫(xiě)Cookie中的JSESSIONID來(lái)標(biāo)記與前端瀏覽器和服務(wù)端的關(guān)系。而HttpServletRequest、HttpServletResponse僅是Servlet為請(qǐng)求和響應(yīng)提供header、body管理的接口。

WebFlux的Cookie管理

WebFlux目前并沒(méi)有為寫(xiě)Cookie提供任何工具。這就需要開(kāi)發(fā)者按照HTTP的規(guī)范來(lái)寫(xiě)Cookie。 在HTTP協(xié)議交互的過(guò)程中,服務(wù)端可以通過(guò)在response中添加Set-Cookie頭來(lái)讓瀏覽器記錄Cookie,而瀏覽器則在request中使用Cookie頭來(lái)傳遞cookie。

寫(xiě)Cookie

寫(xiě)cookie使用ResponseEntity向response頭中添加Set-Cookie即可。CookieBuilder的代碼比較長(zhǎng),它是用于構(gòu)建一個(gè)cookie字符串,Set-Cookie頭除了設(shè)置key=value,還可以設(shè)置過(guò)期日期expires,域名domain,路徑path等。

@RestController
@RequestMapping("/cookie")
public class CookieReadAWriteController {
 @GetMapping("/write")
 public ResponseEntity<String> cookieWrite() {
 HttpHeaders headers = new HttpHeaders();
 String cookie = new CookieBuilder().setKey("cookie-text")
  .setValue(cookieText)
  .setMaxAge(840000)
  .setPath("/")
  .build();
 headers.add("Set-Cookie", cookie);
 return new ResponseEntity<String>("hi," + userName, headers, HttpStatus.OK);
 }
}


class CookieBuilder {
 private String key;
 private String value;
 private String expires;
 private String domain;
 private String path;

 public CookieBuilder setKey(String key) {
 this.key = key;
 return this;
 }

 public CookieBuilder setValue(String value) {
 this.value = value;
 return this;
 }

 public CookieBuilder setMaxAge(long ms) {
 //cookie的過(guò)期日期為GMT格式的時(shí)間。
 Date date = new Date(new Date().getTime() + ms);
 SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
 sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
 this.expires = sdf.format(date);
 return this;
 }

 public CookieBuilder setDomain(String domain) {
 this.domain = domain;
 return this;
 }

 public CookieBuilder setPath(String path) {
 this.path = path;
 return this;
 }

 public String build() {
 StringBuilder sb = new StringBuilder();
 sb.append(this.key);
 sb.append("=");
 sb.append(this.value);
 sb.append(";");
 if (null != this.expires) {
  sb.append("expires=");
  sb.append(this.expires);
  sb.append(";");
 }
 if (null != this.domain) {
  sb.append("domain=");
  sb.append(this.domain);
  sb.append(";");
 }
 if (null != this.path) {
  sb.append("path=");
  sb.append(this.path);
  sb.append(";");
 }
 return sb.toString();
 }
}

讀cookie

獲取cookie就比較直觀,可以直接使用@CookieValue這個(gè)Annotation來(lái)獲取:

@RestController
@RequestMapping("/cookie")
public class CookieReadAWriteController {
 @GetMapping("/read/annotation")
 /**
 * @param value
 * @return
 */
 public String cookieReadAnnotation(@CookieValue("cookie-text") String value) {
 return "當(dāng)前Cookie中的內(nèi)容" + value;
 }
}

也可以直接從Request的Header中獲取:

@RestController
@RequestMapping("/cookie")
public class CookieReadAWriteController {
 @GetMapping("/read/annotation")
 /**
 * @param value
 * @return
 */
 @GetMapping("/read/entity")
 public String cookieReadEntity(RequestEntity<String> entity) {
 HttpHeaders headers = entity.getHeaders();
 List<String> cookie = headers.get("Cookie");
 return "當(dāng)前Cookie中的內(nèi)容" + cookie;
 }
}

使用Annotatin是直接標(biāo)記Cookie的key來(lái)獲取value。而使用RequestEntity需要從頭中先獲取Cookie的內(nèi)容,然后再解析key和value,存在一個(gè)key對(duì)應(yīng)多個(gè)value的情況需要使用RequestEntity。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁(yè)標(biāo)題:SpinrgWebFlux中Cookie的讀寫(xiě)的示例
網(wǎng)頁(yè)路徑:http://www.chinadenli.net/article10/pessgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版網(wǎng)站收錄App設(shè)計(jì)外貿(mào)建站自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)