Skip to content

Commit 2f003f7

Browse files
Merge pull request #94 from SAP/sample-spring-boot
Provide Spring Boot Sample app
2 parents ae49104 + c543315 commit 2f003f7

23 files changed

+1172
-2
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,11 @@ description about how to apply this feature can be found
307307
Stacktraces can be logged within one log message. Further details can be found
308308
[here](https://github.com/SAP/cf-java-logging-support/wiki/Logging-Stack-Traces).
309309

310-
## Sample Application
310+
## Sample Applications
311311

312-
In order to illustrate how the different features are used, this repository includes a simple application in the [./sample folder](./sample).
312+
In order to illustrate how the different features are used, this repository includes two sample applications:
313+
* a Jersey implementation in the [./sample folder](./sample)
314+
* a Spring Boot implementaiton in the [./sample-spring-boot folder](./sample-spring-boot)
313315

314316
## Documentation
315317

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
<module>cf-java-logging-support-jersey</module>
193193
<module>cf-java-monitoring-custom-metrics-clients</module>
194194
<module>sample</module>
195+
<module>sample-spring-boot</module>
195196
</modules>
196197

197198
<dependencies>

sample-spring-boot/README.md

Lines changed: 241 additions & 0 deletions
Large diffs are not rendered by default.

sample-spring-boot/manifest.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
applications:
3+
#
4+
# You may want/need to change these to avoid naming conflicts
5+
#
6+
- name: logging-sample-app
7+
instances: 1
8+
path: target/sample-app-spring-boot-3.2.1.jar
9+
env:
10+
# Set LOG_*: true to activate logging of respective field
11+
LOG_SENSITIVE_CONNECTION_DATA: false
12+
LOG_REMOTE_USER: false
13+
LOG_REFERER: false

sample-spring-boot/pom.xml

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>sample-app-spring-boot</artifactId>
7+
<name>sample-app-spring-boot</name>
8+
<description>Logging Sample App for Spring Boot</description>
9+
<parent>
10+
<groupId>com.sap.hcp.cf.logging</groupId>
11+
<artifactId>cf-java-logging-support-parent</artifactId>
12+
<version>3.3.0</version>
13+
<relativePath>../pom.xml</relativePath>
14+
</parent>
15+
16+
<properties>
17+
<java.version>11</java.version>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<maven.compiler.source>11</maven.compiler.source>
20+
<maven.compiler.target>11</maven.compiler.target>
21+
<spring.boot.version>2.3.4.RELEASE</spring.boot.version>
22+
<keytool.plugin.version>1.5</keytool.plugin.version>
23+
<keystore.token.store_password>0bzhBRNUXBR5
24+
</keystore.token.store_password>
25+
<keystore.token.key_password>0bzhBRNUXBR5
26+
</keystore.token.key_password>
27+
<keystore.token.key_alias>jwt-token</keystore.token.key_alias>
28+
<keystore.token.dname>CN=cf-java-logging-support, OU=None, O=SAP,
29+
L=Unknown, ST=Unknown, C=Unknown
30+
</keystore.token.dname>
31+
</properties>
32+
33+
<dependencyManagement>
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-dependencies</artifactId>
38+
<version>${spring.boot.version}</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
</dependencies>
43+
</dependencyManagement>
44+
45+
<dependencies>
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-actuator</artifactId>
49+
<exclusions>
50+
<exclusion>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-logging</artifactId>
53+
</exclusion>
54+
</exclusions>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-web</artifactId>
59+
<exclusions>
60+
<exclusion>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-starter-logging</artifactId>
63+
</exclusion>
64+
</exclusions>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-configuration-processor</artifactId>
69+
<optional>true</optional>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-starter-security</artifactId>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.springframework.security</groupId>
77+
<artifactId>spring-security-test</artifactId>
78+
<scope>test</scope>
79+
</dependency>
80+
81+
<!-- We're using the Servlet Filter instrumentation -->
82+
<dependency>
83+
<groupId>com.sap.hcp.cf.logging</groupId>
84+
<artifactId>cf-java-logging-support-servlet</artifactId>
85+
<version>${project.version}</version>
86+
</dependency>
87+
88+
<dependency>
89+
<groupId>org.springframework.boot</groupId>
90+
<artifactId>spring-boot-starter-test</artifactId>
91+
<scope>test</scope>
92+
<exclusions>
93+
<exclusion>
94+
<groupId>org.junit.vintage</groupId>
95+
<artifactId>junit-vintage-engine</artifactId>
96+
</exclusion>
97+
</exclusions>
98+
</dependency>
99+
</dependencies>
100+
101+
<build>
102+
<resources>
103+
<resource>
104+
<directory>src/main/resources</directory>
105+
<filtering>true</filtering>
106+
</resource>
107+
<resource>
108+
<directory>${basedir}/target/generated-resources/keystore</directory>
109+
</resource>
110+
</resources>
111+
112+
<plugins>
113+
<plugin>
114+
<groupId>org.springframework.boot</groupId>
115+
<artifactId>spring-boot-maven-plugin</artifactId>
116+
<version>${spring.boot.version}</version>
117+
<executions>
118+
<execution>
119+
<goals>
120+
<goal>repackage</goal>
121+
</goals>
122+
</execution>
123+
</executions>
124+
</plugin>
125+
</plugins>
126+
</build>
127+
128+
<profiles>
129+
<profile>
130+
<id>initialze-keystore</id>
131+
<activation>
132+
<file>
133+
<missing>${basedir}/target/generated-resources/keystore/token_keystore.jks</missing>
134+
</file>
135+
</activation>
136+
<build>
137+
<plugins>
138+
<plugin>
139+
<groupId>org.codehaus.mojo</groupId>
140+
<artifactId>keytool-maven-plugin</artifactId>
141+
<version>${keytool.plugin.version}</version>
142+
<executions>
143+
<execution>
144+
<id>create-key-pair</id>
145+
<goals>
146+
<goal>generateKeyPair</goal>
147+
</goals>
148+
<phase>generate-resources</phase>
149+
</execution>
150+
</executions>
151+
<configuration>
152+
<keystore>${basedir}/target/generated-resources/keystore/token_keystore.jks</keystore>
153+
<storepass>${keystore.token.store_password}</storepass>
154+
<keypass>${keystore.token.key_password}</keypass>
155+
<alias>${keystore.token.key_alias}</alias>
156+
<dname>${keystore.token.dname}</dname>
157+
<sigalg>SHA256withRSA</sigalg>
158+
<ext></ext>
159+
<validity>100</validity>
160+
<keyalg>RSA</keyalg>
161+
<keysize>2048</keysize>
162+
</configuration>
163+
</plugin>
164+
</plugins>
165+
</build>
166+
</profile>
167+
<profile>
168+
<id>logback</id>
169+
<activation>
170+
<property>
171+
<name>!log4j2</name>
172+
</property>
173+
</activation>
174+
<dependencies>
175+
<dependency>
176+
<groupId>org.springframework.boot</groupId>
177+
<artifactId>spring-boot-starter-logging</artifactId>
178+
</dependency>
179+
<dependency>
180+
<groupId>com.sap.hcp.cf.logging</groupId>
181+
<artifactId>cf-java-logging-support-logback</artifactId>
182+
<version>${project.version}</version>
183+
</dependency>
184+
</dependencies>
185+
</profile>
186+
<profile>
187+
<id>log4j2</id>
188+
<activation>
189+
<property>
190+
<name>log4j2</name>
191+
</property>
192+
</activation>
193+
<dependencies>
194+
<dependency>
195+
<groupId>org.springframework.boot</groupId>
196+
<artifactId>spring-boot-starter-log4j2</artifactId>
197+
</dependency>
198+
<dependency>
199+
<groupId>com.sap.hcp.cf.logging</groupId>
200+
<artifactId>cf-java-logging-support-log4j2</artifactId>
201+
<version>${project.version}</version>
202+
</dependency>
203+
</dependencies>
204+
</profile>
205+
</profiles>
206+
207+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.sap.hcp.cf.logging.sample.springboot;
2+
3+
import java.time.Clock;
4+
5+
import javax.servlet.DispatcherType;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
13+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
14+
15+
import com.sap.hcp.cf.logging.sample.springboot.keystore.KeyStoreDynLogConfiguration;
16+
import com.sap.hcp.cf.logging.servlet.dynlog.DynamicLogLevelConfiguration;
17+
import com.sap.hcp.cf.logging.servlet.filter.AddHttpHeadersToLogContextFilter;
18+
import com.sap.hcp.cf.logging.servlet.filter.AddVcapEnvironmentToLogContextFilter;
19+
import com.sap.hcp.cf.logging.servlet.filter.CompositeFilter;
20+
import com.sap.hcp.cf.logging.servlet.filter.CorrelationIdFilter;
21+
import com.sap.hcp.cf.logging.servlet.filter.DynamicLogLevelFilter;
22+
import com.sap.hcp.cf.logging.servlet.filter.GenerateRequestLogFilter;
23+
import com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter;
24+
25+
@SpringBootApplication
26+
@EnableWebMvc
27+
public class SampleAppSpringBootApplication {
28+
29+
public static void main(String[] args) {
30+
SpringApplication.run(SampleAppSpringBootApplication.class, args);
31+
}
32+
33+
/**
34+
* Registers a customized {@link RequestLoggingFilter} with the servlet. We
35+
* inject our own dynamic logging configuration, that contains the public RSA
36+
* key from our keystore.
37+
*
38+
* @param dynLogConfig autowired with {@link KeyStoreDynLogConfiguration}
39+
* @return a registration of the {@link RequestLoggingFilter}
40+
*/
41+
@Bean
42+
public FilterRegistrationBean<MyLoggingFilter> loggingFilter(@Autowired DynamicLogLevelConfiguration dynLogConfig) {
43+
FilterRegistrationBean<MyLoggingFilter> registrationBean = new FilterRegistrationBean<>();
44+
registrationBean.setFilter(new MyLoggingFilter(dynLogConfig));
45+
registrationBean.setName("request-logging");
46+
registrationBean.addUrlPatterns("/*");
47+
registrationBean.setDispatcherTypes(DispatcherType.REQUEST);
48+
return registrationBean;
49+
}
50+
51+
/**
52+
* Provides a global {@link Clock} instance. Useful for testing.
53+
*
54+
* @return the global clock
55+
*/
56+
@Bean
57+
public Clock clock() {
58+
return Clock.systemUTC();
59+
}
60+
61+
private class MyLoggingFilter extends CompositeFilter {
62+
63+
private MyLoggingFilter(DynamicLogLevelConfiguration dynLogConfig) {
64+
super(new AddVcapEnvironmentToLogContextFilter(), new AddHttpHeadersToLogContextFilter(),
65+
new CorrelationIdFilter(), new DynamicLogLevelFilter(() -> dynLogConfig),
66+
new GenerateRequestLogFilter());
67+
}
68+
}
69+
70+
/**
71+
* Provides a {@link BCryptPasswordEncoder} for Basic-Auth.
72+
*
73+
* @return the encoder
74+
*/
75+
@Bean
76+
public BCryptPasswordEncoder encoder() {
77+
return new BCryptPasswordEncoder();
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.sap.hcp.cf.logging.sample.springboot.config;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ConfigurationProperties(prefix = "auth.basic")
8+
public class BasicAuthenticationConfiguration {
9+
10+
private String username;
11+
private String password;
12+
13+
public String getUsername() {
14+
return username;
15+
}
16+
17+
public void setUsername(String username) {
18+
this.username = username;
19+
}
20+
21+
public String getPassword() {
22+
return password;
23+
}
24+
25+
public void setPassword(String password) {
26+
this.password = password;
27+
}
28+
29+
}

0 commit comments

Comments
 (0)