gpt4 book ai didi

java - Spring HttpSecurity 覆盖响应头

转载 作者:行者123 更新时间:2023-11-30 07:26:35 26 4
gpt4 key购买 nike

您可以通过执行以下操作向响应添加 header

@EnableWebSecurity
public class CacheControl extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.headers()
.addHeaderWriter(new StaticHeadersWriter("Cache-Control", "private, max-age=43200"));
}
}

如果响应已经具有 Cache-Control 属性,则只会向其添加第二个同名属性:

Cache-Control: original content
Cache-Control: added content

有没有一种简单的方法来覆盖现有的属性?

最佳答案

我发现的最简单的解决方案是禁用 Cache-Control,然后再次添加 Cache-Control header 。

@Configuration
@EnableWebSecurity
public class CacheControlAdapter extends WebSecurityConfigurerAdapter {
public CacheControlAdapter() {}

@Override
@RequestMapping("/app/")
protected void configure(HttpSecurity http) throws Exception {
StaticHeadersWriter writer = new StaticHeadersWriter("Cache-Control", "value");
http
.headers()
.cacheControl()
.disable()
.addHeaderWriter(writer);
}
}

关于java - Spring HttpSecurity 覆盖响应头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36751782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com