【问题标题】:Unable to set the custom response code in class mediator and xml wso2 apim 3.2.0无法在类调解器和 xml wso2 apim 3.2.0 中设置自定义响应代码
【发布时间】:2021-08-23 11:16:38
【问题描述】:

我在自定义序列(xml)中使用了一个 java 类中介,我在其中处理请求有效负载并检查用户是否有效。

如果用户有效,请求将被发送到后端。

但是,如果用户无效,则不应将请求发送到后端,并且需要通过java类中介或自定义序列(xml)设置响应代码。 目前我收到 202 Accepted 我需要将状态代码覆盖为 401。

在 java 类中介器中,我使用以下几行来设置属性。

org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) mc).getAxis2MessageContext();
msgContext.setProperty("HTTP_SC", "401"); 
//msgContext.setProperty("HTTP_SC", 401); (tried without quotes 401 too!!)
//mc.setProperty("HTTP_SC", 401);
((Axis2MessageContext) mc).setAxis2MessageContext(msgContext);

我尝试使用以下行通过自定义序列(xml),

<property action="remove" name="HTTP_SC" scope="axis2"/>
<property name="SC_ACCEPTED" scope="axis2" value="false"/>
<property name="HTTP_SC" scope="axis2" value="401"/>
<send/>

在这两种情况下都不返回 401,只返回 202..

建议一种在类调解器或自定义序列中执行此操作的方法。

【问题讨论】:

    标签: java xml wso2 wso2-am http-status-codes


    【解决方案1】:

    您可以尝试以下中介

    <api xmlns="http://ws.apache.org/ns/synapse" name="test" context="/test">
        <resource methods="GET">
            <inSequence>
                <property name="HTTP_SC" scope="axis2" value="409"/>
                <respond/>
            </inSequence>
            <outSequence/>
            <faultSequence/>
        </resource>
    </api>
    
    curl -is http://localhost:8280/test
    
    HTTP/1.1 401 Unauthorized
    

    您可以使用以下 Dockerfile 对其进行测试或使用

    FROM wso2/wso2am:3.2.0
    
    COPY test.xml ${WSO2_SERVER_HOME}/repository/deployment/server/synapse-configs/default/api/test.xml
    
    EXPOSE 8280
    
    docker build -t test .
    docker run --rm --name test -it -p 8280:8280 test
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多