From 52bd7fb2b617cec761c5558f40dbd61c664ad19e Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Thu, 11 Jun 2020 11:41:39 -0600 Subject: [PATCH 1/3] Add examples for BAEL-4098 --- .../baeldung/caffeine/AddressController.java | 26 +++++++++++ .../com/baeldung/caffeine/AddressService.java | 44 +++++++++++++++++++ .../caffeine/CaffeineConfiguration.java | 30 +++++++++++++ .../caffeine/CaffieneTutorialApp.java | 11 +++++ .../caffeine/SecurityConfiguration.java | 23 ++++++++++ 5 files changed, 134 insertions(+) create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffieneTutorialApp.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java new file mode 100644 index 0000000000..eeb0cf89d3 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java @@ -0,0 +1,26 @@ +package com.baeldung.caffeine; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class AddressController +{ + @Autowired + private AddressService addressService; + + @GetMapping("/address/{id}") + public ResponseEntity getAddress(@PathVariable("id") long customerId) + { + return ResponseEntity.ok(addressService.getAddress(customerId)); + } + + @GetMapping("/address2/{id}") + public ResponseEntity getAddress2(@PathVariable("id") long customerId) + { + return ResponseEntity.ok(addressService.getAddress2(customerId)); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java new file mode 100644 index 0000000000..2096b60d93 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java @@ -0,0 +1,44 @@ +package com.baeldung.caffeine; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +/** + * Service that uses caching. + */ +@Service +public class AddressService +{ + private final static Logger LOG = LoggerFactory.getLogger(AddressService.class); + + @Autowired + private CacheManager cacheManager; + + @Cacheable(cacheNames = "addresses") + public String getAddress(long customerId) + { + LOG.info("Method getAddress is invoked for customer {}", customerId); + + return "123 Main St"; + } + + public String getAddress2(long customerId) + { + if(cacheManager.getCache("addresses2").get(customerId) != null) + { + return cacheManager.getCache("addresses2").get(customerId).get().toString(); + } + + LOG.info("Method getAddress2 is invoked for customer {}", customerId); + + String address = "123 Main St"; + + cacheManager.getCache("addresses2").put(customerId, address); + + return address; + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java new file mode 100644 index 0000000000..45a8262282 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java @@ -0,0 +1,30 @@ +package com.baeldung.caffeine; + +import com.github.benmanes.caffeine.cache.Caffeine; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.concurrent.TimeUnit; + +@EnableCaching +@Configuration +public class CaffeineConfiguration { + @Bean + public Caffeine caffeineConfig() + { + return Caffeine.newBuilder() + .expireAfterWrite(60, TimeUnit.MINUTES); + } + + @Bean + public CacheManager cacheManager(Caffeine caffeine) + { + CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(); + caffeineCacheManager.getCache("addresses"); + caffeineCacheManager.setCaffeine(caffeine); + return caffeineCacheManager; + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffieneTutorialApp.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffieneTutorialApp.java new file mode 100644 index 0000000000..3828e48fee --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffieneTutorialApp.java @@ -0,0 +1,11 @@ +package com.baeldung.caffeine; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +@SpringBootApplication +public class CaffieneTutorialApp { + public static void main(String[] args) { + new SpringApplicationBuilder(CaffieneTutorialApp.class).run(args); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java new file mode 100644 index 0000000000..778614a8cf --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java @@ -0,0 +1,23 @@ +package com.baeldung.caffeine; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +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; +import org.zalando.problem.spring.web.advice.security.SecurityProblemSupport; + +@Configuration +@EnableWebSecurity +@Import(SecurityProblemSupport.class) +public class SecurityConfiguration extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.csrf().disable(); + + http.authorizeRequests() + .antMatchers("/**") + .permitAll(); + } +} From f06b1f6296db642ffc76d9099a8260ea625f9230 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Sat, 13 Jun 2020 12:33:48 -0600 Subject: [PATCH 2/3] Fix formatting --- .../java/com/baeldung/caffeine/AddressController.java | 6 ++---- .../main/java/com/baeldung/caffeine/AddressService.java | 9 +++------ .../com/baeldung/caffeine/CaffeineConfiguration.java | 6 ++---- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java index eeb0cf89d3..1a48092451 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressController.java @@ -13,14 +13,12 @@ public class AddressController private AddressService addressService; @GetMapping("/address/{id}") - public ResponseEntity getAddress(@PathVariable("id") long customerId) - { + public ResponseEntity getAddress(@PathVariable("id") long customerId) { return ResponseEntity.ok(addressService.getAddress(customerId)); } @GetMapping("/address2/{id}") - public ResponseEntity getAddress2(@PathVariable("id") long customerId) - { + public ResponseEntity getAddress2(@PathVariable("id") long customerId) { return ResponseEntity.ok(addressService.getAddress2(customerId)); } } diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java index 2096b60d93..595e7f410a 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/AddressService.java @@ -19,17 +19,14 @@ public class AddressService private CacheManager cacheManager; @Cacheable(cacheNames = "addresses") - public String getAddress(long customerId) - { + public String getAddress(long customerId) { LOG.info("Method getAddress is invoked for customer {}", customerId); return "123 Main St"; } - public String getAddress2(long customerId) - { - if(cacheManager.getCache("addresses2").get(customerId) != null) - { + public String getAddress2(long customerId) { + if(cacheManager.getCache("addresses2").get(customerId) != null) { return cacheManager.getCache("addresses2").get(customerId).get().toString(); } diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java index 45a8262282..575ae4a720 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/CaffeineConfiguration.java @@ -13,15 +13,13 @@ import java.util.concurrent.TimeUnit; @Configuration public class CaffeineConfiguration { @Bean - public Caffeine caffeineConfig() - { + public Caffeine caffeineConfig() { return Caffeine.newBuilder() .expireAfterWrite(60, TimeUnit.MINUTES); } @Bean - public CacheManager cacheManager(Caffeine caffeine) - { + public CacheManager cacheManager(Caffeine caffeine) { CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(); caffeineCacheManager.getCache("addresses"); caffeineCacheManager.setCaffeine(caffeine); From 2ac608dfd3544ec52e695b1c07f34ef8c0a6ae89 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Sat, 20 Jun 2020 12:33:30 -0600 Subject: [PATCH 3/3] BAEL-4098: Add note about spring security --- .../java/com/baeldung/caffeine/SecurityConfiguration.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java index 778614a8cf..7f3ad7988f 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/caffeine/SecurityConfiguration.java @@ -1,15 +1,16 @@ package com.baeldung.caffeine; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; 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; -import org.zalando.problem.spring.web.advice.security.SecurityProblemSupport; +/** + * Because the POM imports Spring Security, we need a simple security + * configuration for this example application to allow all HTTP requests. + */ @Configuration @EnableWebSecurity -@Import(SecurityProblemSupport.class) public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override