如何在WSO2中拒绝缺少标头或错误标头的请求。

huangapple 未分类评论47阅读模式
英文:

how to rejected request missing header or wrong header in wso2

问题

我在WSO2 API中有这样的代码:

<api context="/request" name="Request" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET" uri-template="/Executionreport/{vtid}/{token}">
        <inSequence>
            <property expression="get-property('uri.var.vtid')" name="Vtid" scope="default" type="STRING"/>
            <property expression="get-property('uri.var.token')" name="token" scope="default" type="STRING"/>
            <filter regex="1234" source="$ctx:token">
                <then>
                    <log>
                        <property name="sukses masuk token" value="0"/>
                    </log>
                    <sequence key="select_Execution_report"/>
                    <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
                </then>
                <else/>
            </filter>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

所以,对于这个请求,我将像这样使用:

http://localhost:8280/request/Executionreport/1100/1234

所以,如果令牌错误,我将重新发送这个:

HTTP/1.1 200 OK
soapaction: urn:mediate
Host: localhost:8280
Accept-Encoding: gzip,deflate
cache-control: 
Content-Type: application/json; charset=UTF-8
Date: Thu, 23 Jul 2020 07:51:59 GMT
Transfer-Encoding: chunked
Connection: Keep-Alive

{ "status":"401 Not Authenticated" }

我想要更改逻辑,使请求如下:

http://localhost:8280/request/Executionreport/1100

使用GET方法,并且具有标头token=1234,因此如果标头请求中的令牌错误,WSO2将拒绝,因此状态将为HTTP/1.1 401 unauthorized

所以我的问题是如何在WSO2中创建具有GET方法和需要标头的API?


如何在WSO2中创建具有GET方法和需要标头的API?

英文:

I have code in API WSO2 like this

&lt;api context=&quot;/request&quot; name=&quot;Request&quot; xmlns=&quot;http://ws.apache.org/ns/synapse&quot;&gt;
    &lt;resource methods=&quot;GET&quot; uri-template=&quot;/Executionreport/{vtid}/{token}&quot;&gt;
        &lt;inSequence&gt;
            &lt;property expression=&quot;get-property(&#39;uri.var.vtid&#39;)&quot; name=&quot;Vtid&quot; scope=&quot;default&quot; type=&quot;STRING&quot;/&gt;
            &lt;property expression=&quot;get-property(&#39;uri.var.token&#39;)&quot; name=&quot;token&quot; scope=&quot;default&quot; type=&quot;STRING&quot;/&gt;
            &lt;filter regex=&quot;1234&quot; source=&quot;$ctx:token&quot;&gt;
                &lt;then&gt;
                    &lt;log&gt;
                        &lt;property name=&quot;sukses masuk token&quot; value=&quot;0&quot;/&gt;
                    &lt;/log&gt;
                    &lt;sequence key=&quot;select_Execution_report&quot;/&gt;
                    &lt;property name=&quot;messageType&quot; scope=&quot;axis2&quot; type=&quot;STRING&quot; value=&quot;application/json&quot;/&gt;
                &lt;/then&gt;
                &lt;else/&gt;
            &lt;/filter&gt;
        &lt;/inSequence&gt;
        &lt;outSequence/&gt;
        &lt;faultSequence/&gt;
    &lt;/resource&gt;
&lt;/api&gt;

so for this request I will use like this one

http://localhost:8280/request/Executionreport/1100/1234

so if the token is wrong I will replay this one

HTTP/1.1 200 OK
soapaction: urn:mediate
Host: localhost:8280
Accept-Encoding: gzip,deflate
cache-control: 
Content-Type: application/json; charset=UTF-8
Date: Thu, 23 Jul 2020 07:51:59 GMT
Transfer-Encoding: chunked
Connection: Keep-Alive

{ &quot;status&quot;:&quot;401 Not Authenticated&quot; }

i want to change the logic like the request is like
http://localhost:8280/request/Executionreport/1100 with methods GET and have header token=1234
so if token wrong in header request wso2 will rejected so status will be HTTP/1.1 401 unauthorized.

so my question how to create API methods GET and need header in WSO2 ?

答案1

得分: 0

你可以按照以下方式读取传入的标头。

&lt;property name=&quot;Token&quot; expression=&quot;get-property(&#39;token&#39;)&quot; scope=&quot;transport&quot; type=&quot;STRING&quot; /&gt;

基于此,您可以执行任何操作。

英文:

You can read the incoming header as below.

&lt;property name=&quot;Token&quot; expression=&quot;get-property(&#39;token&#39;)&quot; scope=&quot;transport&quot; type=&quot;STRING&quot; /&gt;

Based on that you can perform any action.

huangapple
  • 本文由 发表于 2020年7月23日 15:55:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/63049478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定