Record User Information
Learn about how you can record or customize the capture of user information.
Record user information from an HTTP request or by registering a Spring bean for custom user information capture.
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);
}
}
To record custom user information, you can register a bean that implements SentryUserProvider
interface.
Copied
import org.springframework.stereotype.Component;
import io.sentry.protocol.User;
import io.sentry.spring.SentryUserProvider;
@Component
class CustomSentryUserProvider implements SentryUserProvider {
public User provideUser() {
User user = User();
// ... set user information
return user
}
}
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").