Skip to content

Commit

Permalink
Fix handling missing Spring Security on classpath on Java 8. (#1552)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejwalkowiak authored Jun 23, 2021
1 parent 802f79c commit 007cf3d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* Fix: Handling missing Spring Security on classpath on Java 8 (#1552)
* Feat: Support transaction waiting for children to finish. (#1535)
* Feat: Capture logged marker in log4j2 and logback appenders (#1551)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,24 @@ static class HubConfiguration {
@Open
static class SentryWebMvcConfiguration {

/**
* Configures {@link SpringSecuritySentryUserProvider} only if Spring Security is on the
* classpath. Its order is set to be higher than {@link
* SentryWebConfiguration#httpServletRequestSentryUserProvider(SentryOptions)}
*
* @param sentryOptions the Sentry options
* @return {@link SpringSecuritySentryUserProvider}
*/
@Bean
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(SecurityContextHolder.class)
@Order(1)
public @NotNull SpringSecuritySentryUserProvider springSecuritySentryUserProvider(
final @NotNull SentryOptions sentryOptions) {
return new SpringSecuritySentryUserProvider(sentryOptions);
@Open
static class SentrySecurityConfiguration {
/**
* Configures {@link SpringSecuritySentryUserProvider} only if Spring Security is on the
* classpath. Its order is set to be higher than {@link
* SentryWebConfiguration#httpServletRequestSentryUserProvider(SentryOptions)}
*
* @param sentryOptions the Sentry options
* @return {@link SpringSecuritySentryUserProvider}
*/
@Bean
@Order(1)
public @NotNull SpringSecuritySentryUserProvider springSecuritySentryUserProvider(
final @NotNull SentryOptions sentryOptions) {
return new SpringSecuritySentryUserProvider(sentryOptions);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import java.lang.RuntimeException
import javax.servlet.Filter
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.aspectj.lang.ProceedingJoinPoint
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -50,6 +51,7 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.client.RestTemplate

class SentryAutoConfigurationTest {
Expand Down Expand Up @@ -341,6 +343,19 @@ class SentryAutoConfigurationTest {
}
}

@Test
fun `when Spring Security is not on the classpath, SpringSecuritySentryUserProvider is not configured`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.send-default-pii=true")
.withClassLoader(FilteredClassLoader(SecurityContextHolder::class.java))
.run { ctx ->
val userProviders = ctx.getSentryUserProviders()
assertTrue(userProviders.isNotEmpty())
userProviders.forEach {
assertFalse(it is SpringSecuritySentryUserProvider)
}
}
}

@Test
fun `when tracing is enabled, creates tracing filter`() {
contextRunner.withPropertyValues("sentry.dsn=http://key@localhost/proj", "sentry.enable-tracing=true")
Expand Down

0 comments on commit 007cf3d

Please sign in to comment.