[根据WSO2突触文件中的HTTP方法为API配置不同的处理程序

问题描述 投票:0回答:1

我有一个WSO2突触文件,可以在网关级别处理我的API请求。我已经为端点配置了GET和POST / PUT,如下所示。我有一个自定义处理程序,用于处理允许特定主机调用这些API的情况。我要求只允许从任何主机进行GET,而仅允许从特定主机进行POST / PUT。但是,我无法在Synapse文件中对其进行配置,因为我们在文件中定义的处理程序将应用于所有HTTP方法。有没有办法为相同的端点/上下文基于HTTP方法配置处理程序。我怎样才能使我的自定义处理程序(com.abc.wso2.handler.HostRestrictionHandler)仅适用于POST?PUT调用?下面是我的配置。

<api xmlns="http://ws.apache.org/ns/synapse" context="/user/notification/v3" version="v2" version-type="context">  
  <resource methods="GET" uri-template="/*"> 
    <inSequence> 
      <class name="org.wso2.carbon.apimgt.gateway.mediators.TokenPasser"/>  
      <filter regex="PRODUCTION" source="$ctx:AM_KEY_TYPE"> 
        <then> 
          <class name="com.abc.mediators.DispatchMediator"> 
            <property name="url" value="http://api.{HOST_NAME}.com/user/notification/v3"/> 
          </class>  
          <send> 
            <endpoint name="Notification_API"> 
              <address> 
                <timeout> 
                  <duration>30000</duration>  
                  <responseAction>fault</responseAction> 
                </timeout>  
                <suspendOnFailure> 
                  <errorCodes>-1</errorCodes>  
                  <initialDuration>0</initialDuration>  
                  <progressionFactor>1.0</progressionFactor>  
                  <maximumDuration>0</maximumDuration> 
                </suspendOnFailure>  
                <markForSuspension> 
                  <errorCodes>-1</errorCodes> 
                </markForSuspension> 
              </address> 
            </endpoint> 
          </send>
        </then>  
        <else> 
          <sequence key="_sandbox_key_error_"/> 
        </else> 
      </filter> 
    </inSequence>  
    <outSequence> 
      <send/> 
    </outSequence> 
  </resource>  
  <resource methods="POST PUT" uri-template="/*"> 
    <inSequence>
      <property name="X-Forwarded-Host" scope="transport" expression="$trp:Host"/>
      <class name="org.wso2.carbon.apimgt.gateway.mediators.TokenPasser"/>  
      <filter regex="PRODUCTION" source="$ctx:AM_KEY_TYPE"> 
        <then> 
          <class name="com.abc.mediators.DispatchMediator"> 
            <property name="url" value="http://api.{HOST_NAME}.com/user/notification/v3"/> 
          </class>  
          <send> 
            <endpoint name="Notification_API_2"> 
              <address> 
                <timeout> 
                  <duration>30000</duration>  
                  <responseAction>fault</responseAction> 
                </timeout>  
                <suspendOnFailure> 
                  <errorCodes>-1</errorCodes>  
                  <initialDuration>0</initialDuration>  
                  <progressionFactor>1.0</progressionFactor>  
                  <maximumDuration>0</maximumDuration> 
                </suspendOnFailure>  
                <markForSuspension> 
                  <errorCodes>-1</errorCodes> 
                </markForSuspension> 
              </address> 
            </endpoint> 
          </send>
        </then>  
        <else> 
          <sequence key="_sandbox_key_error_"/> 
        </else> 
      </filter> 
    </inSequence>  
    <outSequence> 
      <send/> 
    </outSequence> 
  </resource>  
  <handlers>
      <handler class="com.abc.wso2.handler.HostRestrictionHandler">
        <property name="allowedHosts" value="my.customhost.com" />
      </handler>
  </handlers> 
</api>
rest wso2 wso2-am api-management
1个回答
0
投票

您可以使用this(context.getProperty(“ api.ut.HTTP_METHOD”))获取方法资源,并使用this(context.getProperty(“ api.ut.hostName”))获取主机。因此,您可以在处理程序中实现逻辑以检查主机和资源,然后允许请求。

© www.soinside.com 2019 - 2024. All rights reserved.