Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for customizing ConfigurableRequestItemParser for SC gateway and Zuul adapter #2542

Merged
merged 4 commits into from
Jan 20, 2022

Conversation

icodening
Copy link
Contributor

Describe what this PR does / why we need it
#2456 放开SentinelGatewayFilter写死ServerWebExchangeItemParser的逻辑,支持对参数获取的扩展,增加灵活性
Does this pull request fix one issue?
#2456
Describe how you did it
新增ConfigurableRequestItemParser,可以通过添加Function<T,R>的方式扩充解析逻辑
Describe how to verify it
在装配SentinelGatewayFilter前,先配置remoteAddress的获取逻辑。
nginx代理时需要在第一级代理的header中添加 X-Real-IP,proxy_set_header X-Real-IP $remote_addr;

List headerNames = Arrays.asList("X-Real-IP", "Client-IP");
//delegate default parser and add extractor
ConfigurableRequestItemParser requestItemParser = new ConfigurableRequestItemParser<>(new ServerWebExchangeItemParser());
requestItemParser.addRemoteAddressExtractor(serverWebExchange -> {
for (String headerKey : headerNames) {
String remoteAddress = serverWebExchange.getRequest().getHeaders().getFirst(headerKey);
if (StringUtils.hasLength(remoteAddress)) {
return remoteAddress;
}
}
return null;
});
return new SentinelGatewayFilter(requestItemParser);

@CLAassistant
Copy link

CLAassistant commented Jan 14, 2022

CLA assistant check
All committers have signed the CLA.

@sczyh30 sczyh30 added the area/gateway-flow-control Issues or PRs related to API gateway flow control label Jan 14, 2022
Copy link
Contributor Author

@icodening icodening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will solve it

brotherlu-xcq
brotherlu-xcq previously approved these changes Jan 19, 2022
Copy link
Collaborator

@brotherlu-xcq brotherlu-xcq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

return delegate.getCookieValue(request, cookieName);
}

public void addPathExtractor(Function<T, String> extractor) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe fluent-style API seems better? Just return this like a builder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, that's a good suggestion. I'll change it

@sczyh30 sczyh30 added this to the 1.8.4 milestone Jan 20, 2022
@sczyh30 sczyh30 changed the title #2456 支持灵活配置获取原始客户端IP的方式以适应不同场景 Add support for customizing ConfigurableRequestItemParser for SC gateway and Zuul adapter Jan 20, 2022
Copy link
Member

@sczyh30 sczyh30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sczyh30 sczyh30 merged commit 55ce1a2 into alibaba:master Jan 20, 2022
@sczyh30
Copy link
Member

sczyh30 commented Jan 20, 2022

Thanks for contributing!

Zhang-0952 pushed a commit to Zhang-0952/Sentinel that referenced this pull request Mar 4, 2022
…way and Zuul adapter (alibaba#2542)

* Add ConfigurableRequestItemParser and support customize RequestItemParser
* fluent-style API
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/gateway-flow-control Issues or PRs related to API gateway flow control
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants