Record User Information
Record user information from an HTTP request or by registering a Spring bean for custom user information capture.
Recording User Information From HTTP Request
To record the user's IP address and Principal#name
as the username so you can then view in Event Details:
- Set the personal information flag on
@EnableSentry
totrue
.
Copied
import org.springframework.context.annotation.Configuration;
import io.sentry.spring.EnableSentry;
@EnableSentry(dsn = "https://examplePublicKey@o0.ingest.sentry.io/0", sendDefaultPii = true)
@Configuration
class SentryConfiguration {
}
- Register the servlet filter bean
SentryUserFilter
:
Copied
import io.sentry.IHub;
import io.sentry.spring.SentryUserFilter;
import io.sentry.spring.SentryUserProvider;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SentryFilterConfig {
@Bean
public SentryUserFilter sentryUserFilter(
final IHub hub, final List<SentryUserProvider> sentryUserProviders) {
return new SentryUserFilter(hub, sentryUserProviders);
}
}
- Configure
SentryUserFilter
inweb.xml
orWebApplicationInitializer
usingDelegatingFilterProxy
:
Copied
import javax.servlet.Filter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.filter.RequestContextFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
// ...
@Override
protected Filter[] getServletFilters() {
// filter required by Spring Security
DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy();
springSecurityFilterChain.setTargetBeanName("springSecurityFilterChain");
// sets request on RequestContextHolder
// alternatively configure RequestContextListener
RequestContextFilter requestContextFilter = new RequestContextFilter();
// sets Sentry user on the scope
DelegatingFilterProxy sentryUserFilterProxy = new DelegatingFilterProxy();
sentryUserFilterProxy.setTargetBeanName("sentryUserFilter");
return new Filter[] {
springSecurityFilterChain, requestContextFilter, sentryUserFilterProxy
};
}
}
By default, the username is retrieved from HttpServletRequest#userPrincipal
. To retrieve the username from Spring Security context, register the SpringSecuritySentryUserProvider
bean:
Copied
import io.sentry.SentryOptions;
import io.sentry.spring.SpringSecuritySentryUserProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class SecuritySentryConfig {
@Bean
public SpringSecuritySentryUserProvider springSecuritySentryUserProvider(
SentryOptions sentryOptions) {
return new SpringSecuritySentryUserProvider(sentryOptions);
}
}
Recording Custom User Information
To record custom user information, you can register a bean that implements SentryUserProvider
interface.
Copied
import org.springframework.stereotype.Component;
import io.sentry.core.protocol.User;
import io.sentry.spring.SentryUserProvider;
@Component
class CustomSentryUserProvider implements SentryUserProvider {
public User provideUser() {
User user = User();
// ... set user information
return user
}
}
- Package:
- maven:io.sentry:sentry-spring
- Version:
- 7.13.0
- Repository:
- https://github.com/getsentry/sentry-java