test: cover the encoded scan-path case with the rule it expects to match
ci / middleware (go) (push) Canceled after 0s
ci / py-monitor (python) (push) Canceled after 0s
ci / java-cache (maven) (push) Canceled after 0s
ci / shell-sync (syntax) (push) Canceled after 0s

This commit is contained in:
hoelee committed 2026-11-18 09:06:34 +08:00
1 parent b5e7143742
commit de280a2fd9
39 files changed
+2703 -1

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
# java-cache
Bounded Caffeine cache plus role-based security configuration, extracted from
the Spring Boot service in `hoelee/springboot-hoelee-demo`.
`CacheConfig` wires a size/TTL-bounded Caffeine cache with statistics enabled;
`CacheProperties` keeps those numbers in configuration instead of code;
`SecurityConfig` keeps reads public while every mutation requires the editor role.
```
mvn -q test
```
+52
View File
@@ -0,0 +1,52 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.16</version>
<relativePath/>
</parent>
<groupId>com.hoelee.platform</groupId>
<artifactId>java-cache</artifactId>
<version>1.0.0</version>
<name>java-cache</name>
<description>Bounded cache and security configuration extracted from a Spring Boot service.</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,29 @@
package com.hoelee.platform.cache;
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;
/**
* @version v2, 2024-10-29 10:05:00PM
* @author hoelee
* Learning note: a bounded local cache improves hot-read latency while its statistics make the benefit observable.
*/
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
CacheManager cacheManager(CacheProperties properties) {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("posts");
cacheManager.setCaffeine(Caffeine.newBuilder()
.recordStats()
.maximumSize(properties.maximumSize())
.expireAfterWrite(properties.ttl()));
return cacheManager;
}
}
@@ -0,0 +1,18 @@
package com.hoelee.platform.cache;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import java.time.Duration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
/**
* @version v2, 2024-10-29 09:05:00PM
* @author hoelee
* Learning note: cache limits are configuration rather than magic numbers, so operations can tune them safely.
*/
@Validated
@ConfigurationProperties(prefix = "app.cache")
public record CacheProperties(@Min(1) int maximumSize, @NotNull Duration ttl) {
}
@@ -0,0 +1,52 @@
package com.hoelee.platform.cache;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
/**
* @version v2, 2024-10-30 09:10:00PM
* @author hoelee
* Learning note: public portfolio pages and reads remain accessible while every data mutation needs the editor role.
*/
@Configuration
public class SecurityConfig {
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/", "/index.html", "/css/**", "/js/**", "/actuator/health").permitAll()
.requestMatchers(HttpMethod.GET, "/api/**").permitAll()
.requestMatchers("/api/**").hasRole("EDITOR")
.anyRequest().denyAll())
.httpBasic(Customizer.withDefaults())
.build();
}
@Bean
UserDetailsService userDetailsService(SecurityProperties properties, PasswordEncoder passwordEncoder) {
return new InMemoryUserDetailsManager(User.withUsername(properties.editorUsername())
.password(passwordEncoder.encode(properties.editorPassword()))
.roles("EDITOR")
.build());
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
@@ -0,0 +1,16 @@
package com.hoelee.platform.cache;
import jakarta.validation.constraints.NotBlank;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
/**
* @version v2, 2024-10-30 08:10:00PM
* @author hoelee
* Learning note: credentials are configuration inputs, keeping real deployment values out of source control.
*/
@Validated
@ConfigurationProperties(prefix = "app.security")
public record SecurityProperties(@NotBlank String editorUsername, @NotBlank String editorPassword) {
}
@@ -0,0 +1,5 @@
/**
* Bounded cache and role-based security configuration, extracted from the
* Spring Boot portfolio app so other services can reuse the same defaults.
*/
package com.hoelee.platform.cache;
@@ -0,0 +1,22 @@
package com.hoelee.platform.cache;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.time.Duration;
import org.junit.jupiter.api.Test;
class CachePropertiesTest {
@Test
void keepsConfiguredSizeAndTtl() {
CacheProperties props = new CacheProperties(500, Duration.ofMinutes(5));
assertEquals(500, props.maximumSize());
assertEquals(Duration.ofMinutes(5), props.ttl());
}
@Test
void ttlIsRequired() {
assertNotNull(new CacheProperties(1, Duration.ofSeconds(1)).ttl());
}
}