【问题标题】:Getting error when removing the defalt apiAuthentication handler and adding myCustomhandler, any idea?删除默认 api 身份验证处理程序并添加 myCustoms 处理程序时出错,知道吗?
【发布时间】:2022-01-13 05:24:48
【问题描述】:

(以前它工作正常,但没有改变) 当我从突触配置中删除默认处理程序并添加我的自定义处理程序时,它会给出此错误

[2022-01-13 10:44:41,924] ERROR - ServerWorker Error processing POST request for : /dev/21.2/ext-rt/publish/TestTenant/SmooksProviderTranslation.csv.

java.lang.NullPointerException: null
        at org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler.doRoleBasedAccessThrottlingWithCEP_aroundBody0(ThrottleHandler.java:193) ~[org.wso2.carbon.apimgt.gateway_6.7.206.jar:?]
        at org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler.doRoleBasedAccessThrottlingWithCEP(ThrottleHandler.java:145) ~[org.wso2.carbon.apimgt.gateway_6.7.206.jar:?]
        at org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler.doThrottle_aroundBody12(ThrottleHandler.java:585) ~[org.wso2.carbon.apimgt.gateway_6.7.206.jar:?]
        at org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler.doThrottle(ThrottleHandler.java:562) ~[org.wso2.carbon.apimgt.gateway_6.7.206.jar:?]
        at org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler.handleRequest_aroundBody8(ThrottleHandler.java:522) ~[org.wso2.carbon.apimgt.gateway_6.7.206.jar:?]
        at org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler.handleRequest(ThrottleHandler.java:503) ~[org.wso2.carbon.apimgt.gateway_6.7.206.jar:?]
        at org.apache.synapse.rest.API.process(API.java:373) ~[synapse-core_2.1.7.wso2v183.jar:2.1.7-wso2v183]
        at org.apache.synapse.rest.RESTRequestHandler.apiProcessNonDefaultStrategy(RESTRequestHandler.java:144) ~[synapse-core_2.1.7.wso2v183.jar:2.1.7-wso2v183]
        at org.apache.synapse.rest.RESTRequestHandler.identifyAPI(RESTRequestHandler.java:164) ~[synapse-core_2.1.7.wso2v183.jar:2.1.7-wso2v183]
        at org.apache.synapse.rest.RESTRequestHandler.dispatchToAPI(RESTRequestHandler.java:95) ~[synapse-core_2.1.7.wso2v183.jar:2.1.7-wso2v183]
        at org.apache.synapse.rest.RESTRequestHandler.process(RESTRequestHandler.java:73) ~[synapse-core_2.1.7.wso2v183.jar:2.1.7-wso2v183]
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:331) ~[synapse-core_2.1.7.wso2v183.jar:2.1.7-wso2v183]
        at org.apache.synapse.core.axis2.SynapseMessageReceiver.receive(SynapseMessageReceiver.java:99) ~[synapse-core_2.1.7.wso2v183.jar:2.1.7-wso2v183]
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180) ~[axis2_1.6.1.wso2v41.jar:?]
        at org.apache.synapse.transport.passthru.ServerWorker.processNonEntityEnclosingRESTHandler(ServerWorker.java:367) [synapse-nhttp-transport_2.1.7.wso2v183.jar:?]
        at org.apache.synapse.transport.passthru.ServerWorker.processEntityEnclosingRequest(ServerWorker.java:426) [synapse-nhttp-transport_2.1.7.wso2v183.jar:?]
        at org.apache.synapse.transport.passthru.ServerWorker.run(ServerWorker.java:181) [synapse-nhttp-transport_2.1.7.wso2v183.jar:?]
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172) [axis2_1.6.1.wso2v41.jar:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
        at java.lang.Thread.run(Thread.java:834) [?:?]

下面是 myCustomHandler.java 类的 authenticate() 方法,它有 AuthenticationContext 对象

    public boolean authenticate(MessageContext synCtx) throws APISecurityException {
    Map headers = getTransportHeaders(synCtx);
    String authHeader = getAuthorizationHeader(headers);
    String postmanToken = getPostmanToken(headers);

    String clientIP = "10.110.67.195";

    org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx)
            .getAxis2MessageContext();
    AuthenticationContext authContext = new AuthenticationContext();
    authContext.setAuthenticated(true);

    // Can modify to support scopes based throttle policy selection
    authContext.setTier(APIConstants.UNLIMITED_TIER);
    authContext.setStopOnQuotaReach(true);
    authContext.setApiKey(clientIP);
    authContext.setKeyType(APIConstants.API_KEY_TYPE_PRODUCTION);
    authContext.setUsername((String) axis2MessageContext.getProperty("user"));
    authContext.setCallerToken(null);
    authContext.setApplicationName(null);
    authContext.setApplicationId(clientIP);
    authContext.setConsumerKey(null);
    log.debug("**** applicationID is --- " + authContext.getApplicationId());
    APISecurityUtils.setAuthenticationContext(synCtx, authContext, null);

    System.out.println("**** TE is HERE");
    log.debug("**** TE is HERE2");
    log.debug("**** Header is --- " + headers);
    log.debug("**** AuthHeader is --- " + authHeader);
    log.debug("**** Postman token  is --- " + postmanToken);

    return true;
    /*
     * if (authHeader.startsWith("Bearer ")) { return true; } return false;
     */
}

【问题讨论】:

  • 什么是 APIM 版本? APIM 3.2.0?
  • 是 wso2 apim 3.2.0

标签: wso2 handler wso2-am wso2carbon


【解决方案1】:

我认为问题出在 AuthenticationContext 对象上。我们在身份验证处理程序(在每个身份验证器 [1] 中)填充 authContext 对象,并且该对象在节流处理程序 [2] 中使用。当您将身份验证处理程序替换为自定义处理程序时,您可能错过了填充此对象。尝试将此对象填充为 [1]。

[1] - https://github.com/wso2/carbon-apimgt/blob/v6.7.206/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/oauth/OAuthAuthenticator.java#L301 [2] - https://github.com/wso2/carbon-apimgt/blob/v6.7.206/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/throttling/ThrottleHandler.java#L193

【讨论】:

  • 感谢 Akshitha,感谢您的回复。为什么他们的官方文件中没有提到这一点?那里的代码看起来很简单这是链接请分享您的宝贵想法我很长时间以来一直停留在这一点上...apim.docs.wso2.com/en/3.2.0/develop/extending-api-manager/…
  • 你能帮忙吗?
  • 这是一个我们必须修复的错误。在节流处理程序中,authcontext 对象为空,我们需要在检查是否为空后检索它的 applicaitonId。你能像我们在 OAuthAuthenticator 中那样填充 authenticationContext 对象并将对象设置为消息上下文吗?这将修复 NPE。
  • 我已经填充了 authenticationContext 对象并设置了一些上下文,但仍然显示 NPE。我还在我的 customHandler 中打印了应用程序 ID,它不为空......那么为什么 ThrottleHandler 类无法检索这个对象?(我已经用代码 sn-p 编辑了问题,你可以通过检查它是否对吧?)
  • 问题在于用户名即将为空,我将其设置为 authcontext。感谢您的帮助!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 2017-01-24
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多