Jimy-Guo commented on issue #218:
URL: 
https://github.com/apache/apisix-java-plugin-runner/issues/218#issuecomment-1342119901

   > Possible errors I see.
   > 
   > 1. the built jar does not contain your custom filter;
   > 2. 0.3.1-SNAPSHOT contains errors.
   > 
   > I suggest you
   > 
   > 1. use 0.4.0 (already released)
   > 2. try with 
[https://github.com/apache/apisix-java-plugin-runner/blob/main/docs/en/latest/installation-guide.md#install,](https://github.com/apache/apisix-java-plugin-runner/blob/main/docs/en/latest/installation-guide.md#install%EF%BC%8C)
 example: https://github.com/tzssangglass/java-plugin-runner-demo-1
   
原来的apache-apisix-java-plugin-runner已经更新代码生成使用apache-apisix-java-plugin-runner-0.4.0-bin.tar.gz,插件仍然没有起效:
   ```
   "plugins": {
       "ext-plugin-pre-req": {
         "conf": [
           {
             "name": "StopRequestDemoFilter",
             "value": 
"{\"stop_response_body\":\"{\"hello\":\"success\"}\",\"stop_response_header_name\":\"x-test-header\",\"stop_response_header_value\":\"success\",\"stop_response_code\":\"401\"}"
           }
         ]
       },
   ```
   对应的StopRequestDemoFilter为
   ```
   @Component
   public class StopRequestDemoFilter implements PluginFilter {
       @Override
       public String name() {
           return "StopRequestDemoFilter";
       }
   
       @Override
       public void filter(HttpRequest request, HttpResponse response, 
PluginFilterChain chain) {
           /*
            * If the conf you configured is of type json, you can convert it to 
Map or json.
            */
   
           String configStr = request.getConfig(this);
           Gson gson = new Gson();
           Map<String, Object> conf = new HashMap<>();
           conf = gson.fromJson(configStr, conf.getClass());
   
           /*
            * You can use the parameters in the configuration.
            */
           
response.setStatusCode(Double.valueOf(conf.get("stop_response_code").toString()).intValue());
 
           response.setHeader((String) conf.get("stop_response_header_name"), 
(String) conf.get("stop_response_header_value"));
           /* note: The body is currently a string type.
                    If you need the json type, you need to escape the json 
content here.
                    For example, if the body is set as below
                    "{\"key1\":\"value1\",\"key2\":2}"
   
                    The body received by the client will be as below
                    {"key1":"value1","key2":2}
            */
           response.setBody((String) conf.get("stop_response_body"));
   
           /*  Using the above code, the client side receives the following
   
               header:
               HTTP/1.1 401 Unauthorized
               Content-Type: text/plain; charset=utf-8
               Connection: keep-alive
               new-header: header_by_runner
               Server: APISIX/2.6
   
               body:
               {"key1":"value1","key2":2}
            */
           chain.filter(request, response);
       }
   
       @Override
       public List<String> requiredVars() {
           return null;
       }
   
       @Override
       public Boolean requiredBody() {
           return null;
       }
   }
   ```
   按照逻辑来讲是无论成功与否都是返回401的,但是仍然返回:
   ```
   curl -i "http://0.0.0.0:9080/v1/admin/routes";
   HTTP/1.1 200 OK
   Content-Type: application/json
   Transfer-Encoding: chunked
   Connection: keep-alive
   Date: Thu, 08 Dec 2022 05:59:21 GMT
   Access-Control-Allow-Origin: *
   Access-Control-Allow-Credentials: true
   Access-Control-Expose-Headers: *
   Access-Control-Max-Age: 3600
   Server: APISIX/2.15.1
   
   
{"action":"get","count":2,"node":{"key":"\/apisix\/routes","nodes":[{"modifiedIndex":224,"createdIndex":36,"key":"\/apisix\/routes\/435797591813260093","value":{"labels":{"clife-user":"user","API_VERSION":"v1"},"upstream_id":"435797316717249341","methods":["GET","POST","PUT","DELETE","PATCH","HEAD","OPTIONS","CONNECT","TRACE"],"update_time":1670210706,"plugins":{"limit-conn":{"default_conn_delay":1.5,"key_type":"var","only_use_default_delay":false,"key":"remote_addr","rejected_msg":"conn
 too 
more","rejected_code":503,"disable":false,"allow_degradation":false,"burst":5,"conn":2},"ext-plugin-pre-req":{"conf":[{"value":"{\"validate_header\":\"token\",\"rejected_code\":\"403\"}","name":"TokenCheckFilter2"}]},"proxy-rewrite":{"uri":"\/clife-user-app-api\/test","headers":{"Host":"clife-user-app-api.clife-public"}}},"status":1,"create_time":1669285206,"uri":"\/v1\/account\/test","id":"435797591813260093","name":"testApi"}},{"modifiedIndex":1079,"createdIndex":390,"key":"\/apisix\/routes\
 
/437376598874784573","value":{"labels":{"API_VERSION":"v1"},"methods":["GET","POST","PUT","DELETE","PATCH","HEAD","OPTIONS","CONNECT","TRACE"],"update_time":1670478982,"plugins":{"ext-plugin-pre-req":{"conf":[{"value":"{\"stop_response_body\":\"{\"hello\":\"success\"}\",\"stop_response_header_name\":\"x-test-header\",\"stop_response_header_value\":\"success\",\"stop_response_code\":\"401\"}","name":"StopRequestDemoFilter"}]},"proxy-rewrite":{"uri":"\/apisix\/admin\/routes","headers":{"X-API-KEY":"
 
edd1c9f034335f136f87ad84b625c8f1"}}},"status":1,"uri":"\/v1\/admin\/routes","create_time":1670226368,"id":"437376598874784573","name":"test","upstream":{"scheme":"http","type":"roundrobin","pass_host":"pass","keepalive_pool":{"idle_timeout":60,"requests":1000,"size":320},"nodes":[{"port":9180,"host":"localhost","weight":1}],"timeout":{"connect":6,"read":6,"send":6}}}}],"dir":true}}
   ```
   明显没有生效,这是还有什么其他我需要添加的内容吗?
   
文档中https://github.com/apache/apisix-java-plugin-runner/blob/main/docs/en/latest/installation-guide.md#install%EF%BC%8C的
   Add the apisix-java-plugin-runner dependency in your POM, like:
   <dependency>
       <groupId>org.apache.apisix</groupId> 
       <artifactId>apisix-runner-starter</artifactId>
       <version>0.4.0</version>
   </dependency>
   这一部分是加在哪里的呢?
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to