Skip to content

Commit bcd8c4a

Browse files
committed
Enable caching.
This removes the predefined Spring headers - Cache-Control: no-cache, no-store, max-age=0, must-revalidate - Pragma: no-cache - Expires: 0 and adds the header - Cache-Control: must-revalidate
1 parent 7972f4f commit bcd8c4a

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/main/java/com/bannergress/backend/security/KeycloakSecurityConfig.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,38 @@
33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
55
import org.springframework.context.annotation.Profile;
6+
import org.springframework.http.HttpHeaders;
67
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
78
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
89
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
10+
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
11+
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer.CacheControlConfig;
912
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
1013
import org.springframework.security.web.SecurityFilterChain;
14+
import org.springframework.security.web.header.writers.StaticHeadersWriter;
1115

1216
/** Keycloak security configuration. */
1317
@Configuration
1418
@EnableMethodSecurity(jsr250Enabled = true, prePostEnabled = true)
1519
@Profile("!dev")
1620
public class KeycloakSecurityConfig {
1721
@Bean
18-
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
22+
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
1923
return http //
2024
.csrf(CsrfConfigurer::disable) //
25+
.headers(this::customizeHeaders)
2126
.oauth2ResourceServer(
2227
oauth2 -> oauth2.jwt(jwt -> jwt.jwtAuthenticationConverter(jwtAuthenticationConverter()))) //
2328
.authorizeHttpRequests(auth -> auth.anyRequest().permitAll()) //
2429
.build();
2530
}
2631

32+
private HeadersConfigurer<HttpSecurity> customizeHeaders(HeadersConfigurer<HttpSecurity> customizer) {
33+
return customizer //
34+
.cacheControl(CacheControlConfig::disable)
35+
.addHeaderWriter(new StaticHeadersWriter(HttpHeaders.CACHE_CONTROL, "must-revalidate"));
36+
}
37+
2738
private JwtAuthenticationConverter jwtAuthenticationConverter() {
2839
JwtAuthenticationConverter jwtConverter = new JwtAuthenticationConverter();
2940
jwtConverter.setJwtGrantedAuthoritiesConverter(new KeycloakRealmRoleConverter());

src/main/resources/application.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ spring:
1616
hibernate.dialect: org.hibernate.dialect.PostgreSQLDialect
1717
hibernate.search.backend.directory.root: searchindex/
1818
hibernate.id.db_structure_naming_strategy: single
19-
web:
20-
resources:
21-
cache:
22-
cachecontrol:
23-
no-store: false
2419
sql:
2520
init:
2621
platform: postgresql

0 commit comments

Comments
 (0)