Spring-如何为我返回的所有响应添加标头?

Spring-如何为我返回的所有响应添加标头?,第1张

Spring-如何为我返回的所有响应添加标头?

正确的答案是使用过滤器。拦截器对此并不正确,无论网上有人怎么说。拦截器无法正常工作。

可行的解决方案是创建一个过滤器,如下所示:

public class myAwesomeFilter extends oncePerRequestFilter {    @Override    protected void doFilterInternal(HttpServletRequest request,          HttpServletResponse response, FilterChain filterChain)  throws ServletException, IOException {        response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");        response.addHeader("pragma", "no-cache");        filterChain.doFilter(request, response);    }}

然后,在web.xml中-您需要以下内容

<filter>    <filter-name>sensitiveFormHeaderFilter</filter-name>    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5021012.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-15
下一篇2022-11-15

发表评论

登录后才能评论

评论列表(0条)

    保存