@@ -8,7 +8,9 @@ Record user information from an HTTP request or by registering a Spring bean for
8
8
9
9
## Recording User Information From HTTP Request
10
10
11
- To record the user's IP address and ` Principal#name ` as the username, set the personal information flag to ` true ` .
11
+ To record the user's IP address and ` Principal#name ` as the username so you can then view in [ Event Details] ( /product/performance/event-detail/ ) :
12
+
13
+ 1 . Set the personal information flag on ` @EnableSentry ` to ` true ` .
12
14
13
15
``` Java {tabTitle:Java}
14
16
import org.springframework.context.annotation.Configuration ;
@@ -29,6 +31,140 @@ import io.sentry.spring.EnableSentry
29
31
class SentryConfiguration
30
32
```
31
33
34
+ 2 . Register the servlet filter bean ` SentryUserFilter ` :
35
+
36
+ <Alert level = " warning" title = " Note" >
37
+
38
+ ` SentryUserFilter ` is available in 5.0 beta version of the Sentry Java SDK.
39
+
40
+ </Alert >
41
+
42
+ ``` java
43
+ import io.sentry.IHub ;
44
+ import io.sentry.spring.SentryUserFilter ;
45
+ import io.sentry.spring.SentryUserProvider ;
46
+ import java.util.List ;
47
+ import org.springframework.context.annotation.Bean ;
48
+ import org.springframework.context.annotation.Configuration ;
49
+
50
+ @Configuration
51
+ public class SentryFilterConfig {
52
+
53
+ @Bean
54
+ public SentryUserFilter sentryUserFilter (
55
+ final IHub hub , final List<SentryUserProvider > sentryUserProviders ) {
56
+ return new SentryUserFilter (hub, sentryUserProviders);
57
+ }
58
+ }
59
+ ```
60
+
61
+ ``` kotlin
62
+ import io.sentry.IHub
63
+ import io.sentry.spring.SentryUserProvider
64
+ import io.sentry.spring.SentryUserFilter
65
+ import org.springframework.context.annotation.Bean
66
+ import org.springframework.context.annotation.Configuration
67
+
68
+ @Configuration
69
+ class SentryFilterConfig {
70
+ @Bean
71
+ fun sentryUserFilter (hub : IHub , sentryUserProviders : List <SentryUserProvider >) = SentryUserFilter (hub, sentryUserProviders)
72
+ }
73
+ ```
74
+
75
+ 3 . Configure ` SentryUserFilter ` in ` web.xml ` or ` WebApplicationInitializer ` using ` DelegatingFilterProxy ` :
76
+
77
+ ``` java
78
+ import javax.servlet.Filter ;
79
+ import org.springframework.web.filter.DelegatingFilterProxy ;
80
+ import org.springframework.web.filter.RequestContextFilter ;
81
+ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer ;
82
+
83
+ public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
84
+
85
+ // ...
86
+
87
+ @Override
88
+ protected Filter [] getServletFilters () {
89
+ // filter required by Spring Security
90
+ DelegatingFilterProxy springSecurityFilterChain = new DelegatingFilterProxy ();
91
+ springSecurityFilterChain. setTargetBeanName(" springSecurityFilterChain" );
92
+
93
+ // sets request on RequestContextHolder
94
+ // alternatively configure RequestContextListener
95
+ RequestContextFilter requestContextFilter = new RequestContextFilter ();
96
+
97
+ // sets Sentry user on the scope
98
+ DelegatingFilterProxy sentryUserFilterProxy = new DelegatingFilterProxy ();
99
+ sentryUserFilterProxy. setTargetBeanName(" sentryUserFilter" );
100
+
101
+ return new Filter [] {
102
+ springSecurityFilterChain, requestContextFilter, sentryUserFilterProxy
103
+ };
104
+ }
105
+ }
106
+ ```
107
+
108
+ ``` kotlin
109
+ import org.springframework.web.filter.DelegatingFilterProxy
110
+ import org.springframework.web.filter.RequestContextFilter
111
+ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer
112
+ import javax.servlet.Filter
113
+
114
+ class AppInitializer : AbstractAnnotationConfigDispatcherServletInitializer () {
115
+
116
+ // ...
117
+
118
+ override fun getServletFilters (): Array <Filter > {
119
+ // filter required by Spring Security
120
+ val springSecurityFilterChain = DelegatingFilterProxy ()
121
+ springSecurityFilterChain.setTargetBeanName(" springSecurityFilterChain" )
122
+
123
+ // sets request on RequestContextHolder
124
+ // alternatively configure RequestContextListener
125
+ val requestContextFilter = RequestContextFilter ()
126
+
127
+ // sets Sentry user on the scope
128
+ val sentryUserFilterProxy = DelegatingFilterProxy ()
129
+ sentryUserFilterProxy.setTargetBeanName(" sentryUserFilter" )
130
+ return arrayOf(
131
+ springSecurityFilterChain, requestContextFilter, sentryUserFilterProxy
132
+ )
133
+ }
134
+ }
135
+ ```
136
+
137
+ By default, the username is retrieved from ` HttpServletRequest#userPrincipal ` . To retrieve the username from Spring Security context, register the ` SpringSecuritySentryUserProvider ` bean:
138
+
139
+ ``` java
140
+ import io.sentry.SentryOptions ;
141
+ import io.sentry.spring.SpringSecuritySentryUserProvider ;
142
+ import org.springframework.context.annotation.Bean ;
143
+ import org.springframework.context.annotation.Configuration ;
144
+
145
+ @Configuration
146
+ class SecuritySentryConfig {
147
+ @Bean
148
+ public SpringSecuritySentryUserProvider springSecuritySentryUserProvider (
149
+ SentryOptions sentryOptions ) {
150
+ return new SpringSecuritySentryUserProvider (sentryOptions);
151
+ }
152
+ }
153
+ ```
154
+
155
+ ``` kotlin
156
+ import io.sentry.SentryOptions
157
+ import io.sentry.spring.SpringSecuritySentryUserProvider
158
+ import org.springframework.context.annotation.Bean
159
+ import org.springframework.context.annotation.Configuration
160
+
161
+ @Configuration
162
+ class SecuritySentryConfig {
163
+ @Bean
164
+ fun springSecuritySentryUserProvider (sentryOptions : SentryOptions ) = SpringSecuritySentryUserProvider (sentryOptions)
165
+ }
166
+ ```
167
+
32
168
## Recording Custom User Information
33
169
34
170
To record custom user information, you can register a bean that implements ` SentryUserProvider ` interface.
0 commit comments