From 2fad834b55abd8bcbc05a5110122ad3e44fe4f92 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Mon, 6 Nov 2023 17:01:26 +0100 Subject: [PATCH] Do not register `sentrySpringFilter` in ServletContext for Spring Boot (#3027) --- CHANGELOG.md | 6 ++++++ ...SentrySpringServletContainerInitializer.java | 17 +++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 788797d00a..2f0422b9fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Do not register `sentrySpringFilter` in ServletContext for Spring Boot ([#3027](https://github.com/getsentry/sentry-java/pull/3027)) + ## 6.33.0 ### Features diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringServletContainerInitializer.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringServletContainerInitializer.java index 21c9c92b12..9a71bc6910 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringServletContainerInitializer.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringServletContainerInitializer.java @@ -1,5 +1,7 @@ package io.sentry.spring.jakarta; +import static io.sentry.util.ClassLoaderUtils.classLoaderOrDefault; + import com.jakewharton.nopen.annotation.Open; import jakarta.servlet.DispatcherType; import jakarta.servlet.FilterRegistration; @@ -20,10 +22,17 @@ public class SentrySpringServletContainerInitializer implements ServletContainer @Override public void onStartup(final @Nullable Set> c, final @NotNull ServletContext ctx) throws ServletException { - final FilterRegistration.Dynamic dynamic = - ctx.addFilter("sentrySpringFilter", SentrySpringFilter.class); - if (dynamic != null) { - dynamic.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*"); + try { + Class.forName( + "org.springframework.boot.SpringApplication", + false, + classLoaderOrDefault(getClass().getClassLoader())); + } catch (ClassNotFoundException e) { + final FilterRegistration.Dynamic dynamic = + ctx.addFilter("sentrySpringFilter", SentrySpringFilter.class); + if (dynamic != null) { + dynamic.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), false, "/*"); + } } } }