-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from Media-XI/develop
feat: sentry set user real address from x-forward header
- Loading branch information
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/example/codebase/filter/SentryInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.codebase.filter; | ||
|
||
import io.sentry.Sentry; | ||
import io.sentry.protocol.User; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
public class SentryInterceptor implements HandlerInterceptor { | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
String ipAddress = getClientIpAddress(request); | ||
User realIpUser = new User(); | ||
realIpUser.setIpAddress(ipAddress); | ||
if (ipAddress != null) { | ||
Sentry.configureScope(scope -> { | ||
scope.setUser(realIpUser); | ||
}); | ||
} | ||
return true; | ||
} | ||
|
||
private String getClientIpAddress(HttpServletRequest request) { | ||
String xForwardedForHeader = request.getHeader("X-Forwarded-For"); | ||
if (xForwardedForHeader != null) { | ||
return xForwardedForHeader.split(",")[0]; | ||
} | ||
return request.getRemoteAddr(); | ||
} | ||
} |