---
title: "Record User Information"
description: "Learn about how you can record or customize the capture of user information."
url: https://docs.sentry.io/platforms/java/guides/spring/advanced-usage/
---

# Record User Information | Sentry for Spring

Record user information from an HTTP request or by registering a Spring bean for custom user information capture.

## [Recording User Information From HTTP Request](https://docs.sentry.io/platforms/java/guides/spring/advanced-usage.md#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 [trace view](https://docs.sentry.io/concepts/key-terms/tracing/trace-view.md):

1. Set the personal information flag on `@EnableSentry` to `true`.

```Java
import org.springframework.context.annotation.Configuration;
import io.sentry.spring.EnableSentry;

@EnableSentry(dsn = "___PUBLIC_DSN___", sendDefaultPii = true)
@Configuration
class SentryConfiguration {
}
```

2. Register the servlet filter bean `SentryUserFilter`:

```java
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);
  }
}
```

3. Configure `SentryUserFilter` in `web.xml` or `WebApplicationInitializer` using `DelegatingFilterProxy`:

```java
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:

```java
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](https://docs.sentry.io/platforms/java/guides/spring/advanced-usage.md#recording-custom-user-information)

To record custom user information, you can register a bean that implements `SentryUserProvider` interface.

```java
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
  }
}
```
