@RequestMapping("/cache/etag")
public ResponseEntity<String> cache(@RequestHeader(value="If-None-Match",required=false) String ifNoneMatch){
//浏览器验证文档内容的实体If-None-Match
//当前系统时间
long now = System.currentTimeMillis();
long maxAge = 10;
String body = "<a href=''>点击访问当前连接</a>";
//弱实体
String etag = "W/\""+MD5(body)+"\"";
if(StringUtils.equals(ifNoneMatch,etag)){
return new ResponseEntity<String>(HttpStatus.NOT_MODIFIED);
}
DateFormat gmtDateFormat = new SimpleDateFormat("EEE,d MMM yyyy HH:mm:ss 'GMT'",Locale.US);
headers.add("ETag",etag);
headers.add("Date",gmtDateFormat.format(new Date(now)));
headers.add("Cache-control","max-age="+maxAge);
return new ResponseEntity<String>(body,headers,HttpStatus.OK);
}
浏览器缓存中的ETag验证测试用例
最新推荐文章于 2024-04-30 16:10:19 发布