Skip to content

Commit c7203bd

Browse files
Tomasz Lelekmogronalol
authored andcommitted
BAEL-614 base module for cache-control
1 parent b57cf0c commit c7203bd

7 files changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>spring-security-cache-control</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.4.3.RELEASE</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-actuator</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.security</groupId>
28+
<artifactId>spring-security-core</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.security</groupId>
32+
<artifactId>spring-security-config</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.security</groupId>
36+
<artifactId>spring-security-web</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>javax.servlet</groupId>
40+
<artifactId>javax.servlet-api</artifactId>
41+
<version>${javax.servlet-api.version}</version>
42+
</dependency>
43+
</dependencies>
44+
45+
<properties>
46+
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
47+
</properties>
48+
49+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.cachecontrol;
2+
3+
4+
import com.baeldung.cachecontrol.model.TimestampDto;
5+
import com.baeldung.cachecontrol.model.UserDto;
6+
import org.springframework.http.CacheControl;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import java.time.LocalDateTime;
14+
import java.time.ZoneOffset;
15+
import java.util.concurrent.TimeUnit;
16+
17+
@RestController
18+
public class ResourceEndpoint {
19+
20+
@RequestMapping(value = "/users/{name}", method = RequestMethod.GET)
21+
public ResponseEntity<UserDto> getUser(@PathVariable(value = "name") String name) {
22+
return ResponseEntity.ok()
23+
.cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS))
24+
.body(new UserDto(name));
25+
}
26+
27+
28+
@RequestMapping(value = "/timestamp", method = RequestMethod.GET)
29+
public ResponseEntity<TimestampDto> getServerTimestamp() {
30+
return ResponseEntity.ok()
31+
.cacheControl(CacheControl.noStore())
32+
.cacheControl(CacheControl.noCache())
33+
.body(new TimestampDto(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()));
34+
}
35+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.cachecontrol.app;
2+
3+
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
public class AppRunner {
9+
public static void main(String[] args) {
10+
SpringApplication.run(AppRunner.class, args);
11+
}
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.cachecontrol.config;
2+
3+
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
8+
9+
@Configuration
10+
@EnableWebSecurity
11+
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
12+
13+
@Override
14+
protected void configure(HttpSecurity http) throws Exception {
15+
http
16+
.authorizeRequests()
17+
.anyRequest().permitAll()
18+
.and().headers()
19+
.defaultsDisabled()
20+
.cacheControl();
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.cachecontrol.config;
2+
3+
import org.springframework.http.CacheControl;
4+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
5+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
8+
import java.util.concurrent.TimeUnit;
9+
10+
@EnableWebMvc
11+
public class WebConfiguration extends WebMvcConfigurerAdapter {
12+
@Override
13+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
14+
registry
15+
.addResourceHandler("/resources/**")
16+
.addResourceLocations("/resources/")
17+
.setCacheControl(CacheControl.maxAge(24, TimeUnit.HOURS));
18+
19+
}
20+
}
21+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.cachecontrol.model;
2+
3+
4+
public class TimestampDto {
5+
public final Long timestamp;
6+
7+
public TimestampDto(Long timestamp) {
8+
this.timestamp = timestamp;
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.cachecontrol.model;
2+
3+
4+
public class UserDto {
5+
public final String name;
6+
7+
public UserDto(String name) {
8+
this.name = name;
9+
}
10+
11+
}

0 commit comments

Comments
 (0)