You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/04/15 07:04:49 UTC
[GitHub] [apisix] alanjiang opened a new issue, #6857: bug: TheAPISIX plugin cannot recognize the http header name spell with Camel style
alanjiang opened a new issue, #6857:
URL: https://github.com/apache/apisix/issues/6857
### Current Behavior
![image](https://user-images.githubusercontent.com/8122752/163533134-9b45ed5f-9b04-4a9c-ae40-34fadc5b82cf.png)
As the pic above, I log the request.getHeaders() , and i found that the http header is remactoken and not the "remacToken" . So the program cannot get the remacToken header value by request.getHeader("remacToken") . Below is my logic for APISIX plugin.
I config the "ext-plugin-pre-req" plugin in APISIX admin page:
{
"disable": false,
"plugins": {
"ext-plugin-pre-req": {
"conf": [
{
"name": "token-filter-plugin",
"value": "
{\"validate_header\":\"remacToken\",\"validate_url\":\"https://****/v1/user/tokenValidate\",\"rejected_code\":\"403\"}"
}
]
}
},
"upstream": {
"nodes": {
"172.16.X.XX:9301": 1,
"172.16.X.XXX:9302": 2
},
"type": "roundrobin"
},
"uri": "/*"
}
And my plugin class logic is :
package org.apache.apisix.plugin.runner.filter;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.remacsmart.http.sdk.OkHttpUtil;
import lombok.extern.log4j.Log4j2;
import okhttp3.Response;
import org.apache.apisix.plugin.runner.HttpRequest;
import org.apache.apisix.plugin.runner.HttpResponse;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Log4j2
@Component
public class RemacTokenFilter implements PluginFilter {
private final static String FILTER_NAME = "token-filter-plugin";
@Override
public String name() {
return FILTER_NAME;
}
@Override
public void filter(HttpRequest request, HttpResponse response, PluginFilterChain chain) {
// parse `conf` to json
String configStr = request.getConfig(this);
log.info("--->configStr:{}", configStr);
Gson gson = new Gson();
Map<String, Object> conf = new HashMap<>();
conf = gson.fromJson(configStr, conf.getClass());
// get configuration parameters validate_header
String headerName = (String)conf.get("validate_header");
log.info("--->headerName:{}",headerName );
headerName = headerName.toLowerCase(); // //如果不转成小写,驼峰的的HTTP头部是空的
String remacToken = request.getHeader(headerName);
log.info("---->插件获取参数remacToken:{}", remacToken);
Map<String,String> headers = request.getHeaders();
log.info("--->headers = "+headers);
if (remacToken == null) {
String rejected_code = (String) conf.get("rejected_code");
response.setStatusCode(Integer.parseInt(rejected_code));
JSONObject body = new JSONObject();
body.put("code","1");
body.put("message","remacToken缺失");
response.setBody(body.toJSONString());
chain.filter(request, response);
}else{
String validate_url = (String) conf.get("validate_url");
boolean flag = false;
try {
flag = validate(remacToken, validate_url);
}catch(Exception e) {
log.error(e.getMessage());
}
// token verification results
if (!flag) {
String rejected_code = (String) conf.get("rejected_code");
response.setStatusCode(Integer.parseInt(rejected_code));
chain.filter(request, response);
}else{
chain.filter(request, response);
}
}
}
/**
*
* @param remacToken
* @param validate_url: 用于校验Http header
* @return
*/
private Boolean validate(String remacToken, String validate_url) throws Exception{
int connectTimeOut = 10;
int readTimeOut = 5;
int writeTimeOut = 5;
int maxIdleConnections = 100;
long keepAliveDuration = 300L;
OkHttpUtil okHttpUtil = new OkHttpUtil(connectTimeOut, readTimeOut, writeTimeOut, maxIdleConnections, keepAliveDuration);
Map<String,String> headers = new HashMap<String, String>();
headers.put("remacToken", remacToken);
Response response = okHttpUtil.get(validate_url, headers);
String res = response.body().string();
JSONObject json = JSONObject.parseObject(res);
log.info("--->校验token响应:{}", json);
if (!json.getString("code").equals("200")) {
return false;
}
return true;
}
@Override
public List<String> requiredVars() {
return null;
}
@Override
public Boolean requiredBody() {
return null;
}
}
### Expected Behavior
When I use " request.getHeader("remacToken") " should not get null, why the http header "remacToken" is transformed to "remactoken" when the request through APISIX as the pic i mentioned above ?
### Error Logs
none
### Steps to Reproduce
As i menthoned.
### Environment
- APISIX version (run `apisix version`):
- Operating system (run `uname -a`):
- OpenResty / Nginx version (run `openresty -V` or `nginx -V`):
- etcd version, if relevant (run `curl http://127.0.0.1:9090/v1/server_info`):
- APISIX Dashboard version, if relevant:
- Plugin runner version, for issues related to plugin runners:
- LuaRocks version, for installation issues (run `luarocks --version`):
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] liangliang4ward commented on issue #6857: bug: TheAPISIX plugin cannot recognize the http header name spell with Camel style
Posted by GitBox <gi...@apache.org>.
liangliang4ward commented on issue #6857:
URL: https://github.com/apache/apisix/issues/6857#issuecomment-1100053095
this not apisix bug .
http header Case insensitive。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [apisix] spacewander closed issue #6857: bug: TheAPISIX plugin cannot recognize the http header name spell with Camel style
Posted by GitBox <gi...@apache.org>.
spacewander closed issue #6857: bug: TheAPISIX plugin cannot recognize the http header name spell with Camel style
URL: https://github.com/apache/apisix/issues/6857
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org