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,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;
}
}