com.sap.hcp.cf.logging.sample.springboot.SampleAppSpringBootApplication Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sample-app-spring-boot Show documentation
Show all versions of sample-app-spring-boot Show documentation
Logging Sample App for Spring Boot
package com.sap.hcp.cf.logging.sample.springboot;
import java.time.Clock;
import javax.servlet.DispatcherType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.sap.hcp.cf.logging.sample.springboot.keystore.KeyStoreDynLogConfiguration;
import com.sap.hcp.cf.logging.servlet.dynlog.DynamicLogLevelConfiguration;
import com.sap.hcp.cf.logging.servlet.filter.AddHttpHeadersToLogContextFilter;
import com.sap.hcp.cf.logging.servlet.filter.AddVcapEnvironmentToLogContextFilter;
import com.sap.hcp.cf.logging.servlet.filter.CompositeFilter;
import com.sap.hcp.cf.logging.servlet.filter.CorrelationIdFilter;
import com.sap.hcp.cf.logging.servlet.filter.DynamicLogLevelFilter;
import com.sap.hcp.cf.logging.servlet.filter.GenerateRequestLogFilter;
import com.sap.hcp.cf.logging.servlet.filter.RequestLoggingFilter;
@SpringBootApplication
@EnableWebMvc
public class SampleAppSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SampleAppSpringBootApplication.class, args);
}
/**
* Registers a customized {@link RequestLoggingFilter} with the servlet. We
* inject our own dynamic logging configuration, that contains the public RSA
* key from our keystore.
*
* @param dynLogConfig autowired with {@link KeyStoreDynLogConfiguration}
* @return a registration of the {@link RequestLoggingFilter}
*/
@Bean
public FilterRegistrationBean loggingFilter(@Autowired DynamicLogLevelConfiguration dynLogConfig) {
FilterRegistrationBean registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new MyLoggingFilter(dynLogConfig));
registrationBean.setName("request-logging");
registrationBean.addUrlPatterns("/*");
registrationBean.setDispatcherTypes(DispatcherType.REQUEST);
registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registrationBean;
}
/**
* Provides a global {@link Clock} instance. Useful for testing.
*
* @return the global clock
*/
@Bean
public Clock clock() {
return Clock.systemUTC();
}
private class MyLoggingFilter extends CompositeFilter {
private MyLoggingFilter(DynamicLogLevelConfiguration dynLogConfig) {
super(new AddVcapEnvironmentToLogContextFilter(), new AddHttpHeadersToLogContextFilter(),
new CorrelationIdFilter(), new DynamicLogLevelFilter(() -> dynLogConfig),
new GenerateRequestLogFilter());
}
}
/**
* Provides a {@link BCryptPasswordEncoder} for Basic-Auth.
*
* @return the encoder
*/
@Bean
public BCryptPasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy