日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【sprinb-boot】HttpServletResponse设置HTTP缓存

發布時間:2024/9/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【sprinb-boot】HttpServletResponse设置HTTP缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

  • HTTP header 中的 Cache-Control 告知客戶端是否使用緩存。
  • HTTP header 中的 Pragma, 最常用的是Pragma:no-cache。在HTTP/1.1協議中,它的含義和Cache- Control:no-cache相同。 Pragma: no-cache可以應用到http 1.0 和http 1.1,而Cache-Control: no-cache只能應用于http 1.1。
    • HTTP header 中的 Expires 告知客戶端響應過期的具體時間。

瀏覽器會有默認值

當不告知瀏覽器 Cache-Control 、Pragma 具體的值時,瀏覽器會有默認值。chrome默認是會啟用緩存的。
如果不希望瀏覽器緩存時,則需要明確指定相關的參數值。

希望緩存一段時間:比如30分鐘

Cache-Control: max-age = 1800 resp.setHeader("Cache-Control", "max-age=1800");

希望某一刻之后緩存失效:比如2020年2月7日20點0分0秒

Expires : Fri, 7 Feb 2020 20:00:00 +0800 resp.setHeader("Expires", "Fri, 7 Feb 2020 20:00:00 +0800");

時間格式標準參考:https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date expiresDate = sdf.parse("2020-02-07 20:00:00"); ZonedDateTime expiresZonedDate = ZonedDateTime.ofInstant(expiresDate.toInstant(), ZoneId.of("Asia/Shanghai")); String expires = expiresZonedDate.format(DateTimeFormatter.RFC_1123_DATE_TIME);

時區轉換:https://www.cnblogs.com/niceboat/p/7027394.html


貌似使用毫秒數也可以(緩存1分鐘):

resp.setHeader("Expires", System.currentTimeMillis()+60*1000);

參考代碼

@SpringBootApplication @ServletComponentScan public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);} } @ServletComponentScan @WebFilter(urlPatterns = {"/dashboard"},filterName = "checkLoginFilter") public class CheckLoginFilter implements Filter {private String loginURL = "http://app.mydomain.com/login.jsp";private ServletContext context;@Overridepublic void init(FilterConfig filterConfig) throws ServletException {context = filterConfig.getServletContext();}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) request;HttpSession session = req.getSession();HttpServletResponse resp = (HttpServletResponse) response;try {if (session.getAttribute("roleno") == null) {resp.setDateHeader("Expires", 0);resp.setHeader("Cache-Control", "no-cache");resp.setHeader("Pragma", "no-cache");resp.sendRedirect(loginURL);} else {filterChain.doFilter(request, response);}} catch (ServletException sx) {context.log(sx.getMessage());} catch (IOException iox) {context.log(iox.getMessage());}}@Overridepublic void destroy() {}}

參考

https://www.cnblogs.com/Joans/p/3956490.html
https://blog.csdn.net/u014175572/article/details/54861813
https://baijiahao.baidu.com/s?id=1612392982674092834

總結

以上是生活随笔為你收集整理的【sprinb-boot】HttpServletResponse设置HTTP缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。