Skip to content

Commit d152693

Browse files
Java: Fetch user data based on yaml/properties configuration in Spring Boot applications (#1147)
1 parent df01fd4 commit d152693

29 files changed

+878
-267
lines changed

packages/java/examples/OwlTestApp/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
</properties>
2020

2121
<dependencies>
22+
<dependency>
23+
<groupId>com.readme</groupId>
24+
<artifactId>metrics-spring-boot-starter</artifactId>
25+
<version>0.0.1-SNAPSHOT</version>
26+
</dependency>
2227
<dependency>
2328
<groupId>org.springframework.boot</groupId>
2429
<artifactId>spring-boot-starter-web</artifactId>

packages/java/examples/OwlTestApp/src/main/java/com/readme/example/OwlController.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.readme.example;
22

3+
import org.springframework.beans.factory.annotation.Value;
34
import org.springframework.web.bind.annotation.GetMapping;
45
import org.springframework.web.bind.annotation.PathVariable;
56
import org.springframework.web.bind.annotation.PutMapping;
@@ -13,6 +14,9 @@
1314
@RestController
1415
public class OwlController {
1516

17+
@Value("${readme.readmeApiKey}")
18+
private String readmeApiKey;
19+
1620
private final Map<String, String> owlStorage = new HashMap<>();
1721

1822
public OwlController() {
@@ -21,7 +25,7 @@ public OwlController() {
2125

2226
@GetMapping("/owl/{id}")
2327
public String getOwlById(@PathVariable String id) {
24-
return "Owl with id " + id;
28+
return "Owl with id " + id + " and key is " + readmeApiKey;
2529
}
2630

2731
@GetMapping("/owls")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
readme:
2+
readmeApiKey: ${README_API_KEY}
3+
userdata:
4+
apiKey:
5+
source: header
6+
fieldName: X-User-Name
7+
email:
8+
source: header
9+
fieldName: X-User-Email
10+
label:
11+
source: header
12+
fieldName: X-User-Id
13+
14+
#readme:
15+
# readmeApiKey: ${README_API_KEY}
16+
# userdata:
17+
# apiKey:
18+
# source: jsonBody
19+
# fieldName: /owl-creator/name
20+
# email:
21+
# source: jsonBody
22+
# fieldName: /owl-creator/contacts/email
23+
# label:
24+
# source: jsonBody
25+
# fieldName: owl-creator/label
26+
27+
#readme:
28+
# readmeApiKey: ${README_API_KEY}
29+
# userdata:
30+
# apiKey:
31+
# source: jwt
32+
# fieldName: name
33+
# email:
34+
# source: jwt
35+
# fieldName: aud
36+
# label:
37+
# source: jwt
38+
# fieldName: user_id
39+
40+
41+

packages/java/examples/OwlTestApp/src/test/java/com/readme/example/ExampleApplicationTests.java

-13
This file was deleted.

packages/java/readme-metrics-spring-boot-starter/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ Each field (`apiKey`, `email`, `label`) requires two sub-properties:
2020
### Example Configuration (YAML)
2121
```yaml
2222
readme:
23-
readmeApiKey: a11b33b2c44de78f7a
23+
readmeApiKey: ${readmeApiKey}
2424
userdata:
2525
apiKey:
2626
source: header
2727
fieldName: X-User-Id
2828
email:
29-
source: jwtClaim
29+
source: jwt
3030
fieldName: aud
3131
label:
3232
source: jsonBody

packages/java/readme-metrics-spring-boot-starter/pom.xml

+15-23
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,33 @@
2222

2323
<dependencies>
2424
<dependency>
25-
<groupId>com.readme</groupId>
26-
<artifactId>readme-metrics</artifactId>
27-
<version>${readme-metrics.version}</version>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
2827
</dependency>
29-
3028
<dependency>
3129
<groupId>org.springframework.boot</groupId>
32-
<artifactId>spring-boot-starter-web</artifactId>
30+
<artifactId>spring-boot-configuration-processor</artifactId>
3331
<optional>true</optional>
3432
</dependency>
3533
<dependency>
36-
<groupId>jakarta.servlet</groupId>
37-
<artifactId>jakarta.servlet-api</artifactId>
38-
<version>6.0.0</version>
39-
<scope>provided</scope>
40-
<optional>true</optional>
34+
<groupId>com.readme</groupId>
35+
<artifactId>readme-metrics</artifactId>
36+
<version>${readme-metrics.version}</version>
37+
</dependency>
38+
39+
<!-- HANDLING JWT TOKENS-->
40+
<dependency>
41+
<groupId>com.auth0</groupId>
42+
<artifactId>java-jwt</artifactId>
43+
<version>4.4.0</version>
4144
</dependency>
45+
46+
<!-- TESTS-->
4247
<dependency>
4348
<groupId>org.springframework.boot</groupId>
4449
<artifactId>spring-boot-starter-test</artifactId>
4550
<scope>test</scope>
4651
</dependency>
4752
</dependencies>
4853

49-
<build>
50-
<plugins>
51-
<plugin>
52-
<groupId>org.springframework.boot</groupId>
53-
<artifactId>spring-boot-maven-plugin</artifactId>
54-
<configuration>
55-
<skip>true</skip>
56-
</configuration>
57-
</plugin>
58-
</plugins>
59-
</build>
60-
61-
6254
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.readme.starter.config;
2+
3+
import com.readme.dataextraction.RequestDataCollector;
4+
import com.readme.dataextraction.UserDataCollector;
5+
import com.readme.starter.datacollection.DataCollectionFilter;
6+
import com.readme.starter.datacollection.ServletDataPayloadAdapter;
7+
import lombok.AllArgsConstructor;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
9+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.ComponentScan;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.core.Ordered;
14+
15+
/**
16+
* Configuration class for registering and initializing the JakartaDataCollectionFilter
17+
* along with its dependencies in a Spring Boot application.
18+
* <p>
19+
* This configuration provides the following:
20+
* <ul>
21+
* <li>Instantiates the {@link DataCollectionFilter} with required collectors.</li>
22+
* <li>Registers the filter using {@link FilterRegistrationBean} for servlet-based applications.</li>
23+
* <li>Sets up default implementations for collecting request and user data.</li>
24+
* </ul>
25+
*/
26+
@Configuration
27+
@ConditionalOnClass({UserDataProperties.class})
28+
@ComponentScan(basePackages = {"com.readme.starter"})
29+
@AllArgsConstructor
30+
public class DataCollectionAutoConfiguration {
31+
32+
@Bean
33+
public FilterRegistrationBean<DataCollectionFilter> metricsFilter(
34+
RequestDataCollector<ServletDataPayloadAdapter> requestDataCollector,
35+
UserDataCollector<ServletDataPayloadAdapter> userDataCollector) {
36+
FilterRegistrationBean<DataCollectionFilter> registrationBean = new FilterRegistrationBean<>();
37+
registrationBean.setFilter(new DataCollectionFilter(requestDataCollector, userDataCollector));
38+
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
39+
registrationBean.addUrlPatterns("/*");
40+
return registrationBean;
41+
}
42+
43+
}

packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/JakartaDataCollectionConfig.java

-70
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.readme.starter.config;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.stereotype.Component;
6+
7+
@Data
8+
@Component
9+
@ConfigurationProperties(prefix = "readme")
10+
public class ReadmeConfigurationProperties {
11+
12+
private String readmeApiKey;
13+
14+
}

packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/MonitoringProperties.java packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/UserDataProperties.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.readme.config.FieldMapping;
44
import lombok.Data;
55
import org.springframework.boot.context.properties.ConfigurationProperties;
6-
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.stereotype.Component;
77

88
/**
99
* Configuration properties for monitoring library.
@@ -14,12 +14,12 @@
1414
* (e.g., header, jwtClaim, or jsonBody) and its corresponding value.
1515
* </p>
1616
*/
17-
@Configuration
18-
@ConfigurationProperties(prefix = "readme.userdata")
17+
1918
@Data
20-
public class MonitoringProperties {
19+
@Component
20+
@ConfigurationProperties(prefix = "readme.userdata")
21+
public class UserDataProperties {
2122

22-
private String readmeApiKey;
2323
private FieldMapping apiKey;
2424
private FieldMapping email;
2525
private FieldMapping label;

0 commit comments

Comments
 (0)