BAEL-614 base module for cache-control

This commit is contained in:
Tomasz Lelek 2017-01-25 23:06:22 +01:00 committed by Andrew Morgan
parent b57cf0cf56
commit c7203bd564
7 changed files with 160 additions and 0 deletions

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-security-cache-control</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
</dependency>
</dependencies>
<properties>
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
</properties>
</project>

View File

@ -0,0 +1,35 @@
package com.baeldung.cachecontrol;
import com.baeldung.cachecontrol.model.TimestampDto;
import com.baeldung.cachecontrol.model.UserDto;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.TimeUnit;
@RestController
public class ResourceEndpoint {
@RequestMapping(value = "/users/{name}", method = RequestMethod.GET)
public ResponseEntity<UserDto> getUser(@PathVariable(value = "name") String name) {
return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS))
.body(new UserDto(name));
}
@RequestMapping(value = "/timestamp", method = RequestMethod.GET)
public ResponseEntity<TimestampDto> getServerTimestamp() {
return ResponseEntity.ok()
.cacheControl(CacheControl.noStore())
.cacheControl(CacheControl.noCache())
.body(new TimestampDto(LocalDateTime.now().toInstant(ZoneOffset.UTC).toEpochMilli()));
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.cachecontrol.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppRunner {
public static void main(String[] args) {
SpringApplication.run(AppRunner.class, args);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.cachecontrol.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().permitAll()
.and().headers()
.defaultsDisabled()
.cacheControl();
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.cachecontrol.config;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.util.concurrent.TimeUnit;
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/")
.setCacheControl(CacheControl.maxAge(24, TimeUnit.HOURS));
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.cachecontrol.model;
public class TimestampDto {
public final Long timestamp;
public TimestampDto(Long timestamp) {
this.timestamp = timestamp;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.cachecontrol.model;
public class UserDto {
public final String name;
public UserDto(String name) {
this.name = name;
}
}