webflux与webmvc的差异
webflux读写cookie不像webmvc那么直接,最主要的原因是webmvc是基于servlet规范的,而webflux仅仅遵守的是http协议。所以在使用的时候会发现httpservletrequest、httpservletresponse这些servlet层级的接口根本就无法使用。
cookie与servlet并没有太直接的关系,前者是属于http规范的而后者是一个j2ee的规范,在应用层面仅有的联系就是servlet会读写cookie中的jsessionid来标记与前端浏览器和服务端的关系。而httpservletrequest、httpservletresponse仅是servlet为请求和响应提供header、body管理的接口。
webflux的cookie管理
webflux目前并没有为写cookie提供任何工具。这就需要开发者按照http的规范来写cookie。 在http协议交互的过程中,服务端可以通过在response中添加set-cookie头来让浏览器记录cookie,而浏览器则在request中使用cookie头来传递cookie。
写cookie
写cookie使用responseentity向response头中添加set-cookie即可。cookiebuilder的代码比较长,它是用于构建一个cookie字符串,set-cookie头除了设置key=value,还可以设置过期日期expires,域名domain,路径path等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
@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的过期日期为gmt格式的时间。 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这个annotation来获取:
1 2 3 4 5 6 7 8 9 10 11 12 |
@restcontroller @requestmapping ( "/cookie" ) public class cookiereadawritecontroller { @getmapping ( "/read/annotation" ) /** * @param value * @return */ public string cookiereadannotation( @cookievalue ( "cookie-text" ) string value) { return "当前cookie中的内容" + value; } } |
也可以直接从request的header中获取:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@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 "当前cookie中的内容" + cookie; } } |
使用annotatin是直接标记cookie的key来获取value。而使用requestentity需要从头中先获取cookie的内容,然后再解析key和value,存在一个key对应多个value的情况需要使用requestentity。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://my.oschina.net/chkui/blog/2993002
查看更多关于Spinrg WebFlux中Cookie的读写的示例的详细内容...