All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.shoulder.web.interceptor.ApiResponseNoCacheInterceptor Maven / Gradle / Ivy

Go to download

shoulder WEB 模块,基于Spring Boot Web提供了 Controller AOP 日志、AOP异常处理,统一返回值,健康检查,租户、用户解析,Web 安全防护,通用CrudController,动态字典,标签管理,HTTP client AOP日志、AOP异常处理等能力,助力Web飞速开发。

The newest version!
package org.shoulder.web.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

import java.lang.invoke.MethodHandle;

import javax.annotation.Nonnull;

/**
 * 阻止浏览器缓存接口返回值
 * 适用于非查询类的管理 web 应用,达到展示实时数据的效果。
 *
 * @author lym
 */
public class ApiResponseNoCacheInterceptor implements HandlerInterceptor {

    /**
     * 抛异常也要执行,因为异常返回值也不能缓存
     */
    @Override
    public void afterCompletion(@Nonnull HttpServletRequest req, @Nonnull HttpServletResponse resp, @Nonnull Object handler, Exception e) {
        if (handler instanceof MethodHandle) {
            addNoCacheResponseHeaders(resp);
        }
    }

    /**
     * 为响应加上禁止缓存的标识
     */
    private static void addNoCacheResponseHeaders(HttpServletResponse resp) {
        resp.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
        resp.setHeader("Pragma", "no-cache");
        resp.setDateHeader("expires", -1);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy