sentry配置XFORWARDEDFOR作为真实ip

编程

nginx 配置

http {

include mime.types;

default_type application/octet-stream;

log_format main "$remote_addr - $remote_user [$time_local] "$request" "

"$status $body_bytes_sent "$http_referer" "

""$http_user_agent" "$http_x_forwarded_for"";

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

#gzip on;

#set_real_ip_from 127.0.0.1;

#real_ip_header X-Forwarded-For;

#real_ip_recursive on;

proxy_set_header Host $host;

proxy_set_header X-Client-IP $remote_addr;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto $scheme;

proxy_set_header X-Request-Id $request_id;

server {

listen 80;

server_name api.xx.com;

#charset koi8-r;

#access_log logs/host.access.log main;

gzip_static on;

location /api { #转发后端接口

proxy_pass http://127.0.0.1:7080;

}

}

...

}

Java 配置

@Configuration

@ConditionalOnClass({HandlerExceptionResolver.class, SentryExceptionResolver.class})

@EnableConfigurationProperties(SentryProperties.class)

@ConditionalOnWebApplication

@ConditionalOnProperty(name = "sentry.enabled", havingValue = "true", matchIfMissing = true)

public class SentryConf {

@Bean

@ConditionalOnMissingBean

public SentryClient sentryClient(SentryProperties properties,

@Autowired(required = false) List<ShouldSendEventCallback> shouldSendEventCallbacks) {

String dsn = properties.getDsn() != null ? properties.getDsn().toString() : null;

SentryOptions sentryOptions = SentryOptions.from(createLookup(properties), dsn, new MySentryClientFactory());

SentryClient sentryClient = Sentry.init(sentryOptions);

if (!StringUtils.isEmpty(properties.getRelease())) {

sentryClient.setRelease(properties.getRelease());

}

if (!StringUtils.isEmpty(properties.getDist())) {

sentryClient.setDist(properties.getDist());

}

if (!StringUtils.isEmpty(properties.getEnvironment())) {

sentryClient.setEnvironment(properties.getEnvironment());

}

if (!StringUtils.isEmpty(properties.getServerName())) {

sentryClient.setServerName(properties.getServerName());

}

if (properties.getTags() != null && !properties.getTags().isEmpty()) {

for (Map.Entry<String, String> tag : properties.getTags().entrySet()) {

sentryClient.addTag(tag.getKey(), tag.getValue());

}

}

if (properties.getMdcTags() != null && !properties.getMdcTags().isEmpty()) {

for (String mdcTag : properties.getMdcTags()) {

sentryClient.addMdcTag(mdcTag);

}

}

if (properties.getExtra() != null && !properties.getExtra().isEmpty()) {

for (Map.Entry<String, Object> extra : properties.getExtra().entrySet()) {

sentryClient.addExtra(extra.getKey(), extra.getValue());

}

}

if (shouldSendEventCallbacks != null && !shouldSendEventCallbacks.isEmpty()) {

for (ShouldSendEventCallback shouldSendEventCallback : shouldSendEventCallbacks) {

sentryClient.addShouldSendEventCallback(shouldSendEventCallback);

}

}

return sentryClient;

}

private Lookup createLookup(SentryProperties properties) {

return Lookup.getDefaultWithAdditionalProviders(

Collections.<ConfigurationProvider>singletonList(new SpringBootConfigurationProvider(properties)),

Collections.<ConfigurationProvider>emptyList()

);

}

/**

* 业务异常不上报

*

* @return 是否应该上报

*/

@Bean

public List<ShouldSendEventCallback> shouldSendEventCallbacks() {

ArrayList<ShouldSendEventCallback> list = new ArrayList<>();

list.add(event -> {

for (Map.Entry<String, SentryInterface> interfaceEntry : event.getSentryInterfaces().entrySet()) {

if (interfaceEntry.getValue() instanceof ExceptionInterface) {

ExceptionInterface i = (ExceptionInterface) interfaceEntry.getValue();

for (SentryException sentryException : i.getExceptions()) {

if (sentryException.getExceptionClassName().equals("ApiException") ||

sentryException.getExceptionClassName().equals("WxErrorException")) {

return false;

}

}

}

}

return true;

});

return list;

}

public static class MySentryClientFactory extends DefaultSentryClientFactory {

@Override

public SentryClient createSentryClient(Dsn dsn) {

SentryClient sentryClient = new SentryClient(createConnection(dsn), getContextManager(dsn));

/*

Create and use the ForwardedAddressResolver, which will use the

X-FORWARDED-FOR header for the remote address if it exists.

*/

ForwardedAddressResolver forwardedAddressResolver = new ForwardedAddressResolver();

sentryClient.addBuilderHelper(new HttpEventBuilderHelper(forwardedAddressResolver));

sentryClient.addBuilderHelper(new ContextBuilderHelper(sentryClient));

return configureSentryClient(sentryClient, dsn);

}

}

}

application.yml 相关配置

sentry:

dsn: "https://a1b29065c7f54fe5ad8ed5237e28caf7@o245236.ingest.sentry.io/5214956"

maven 相关配置

<dependency>

<groupId>io.sentry</groupId>

<artifactId>sentry-spring-boot-starter</artifactId>

<version>${sentry.version}</version>

</dependency>

233

以上是 sentry配置XFORWARDEDFOR作为真实ip 的全部内容, 来源链接: utcz.com/z/517882.html

回到顶部