2015-11-05 107 views
1

我正在按照本指南实施wso2-am的自定义身份验证处理程序https://docs.wso2.com/display/AM190/Writing+Custom+Handlers 但我不清楚当我的身份验证处理程序返回false时如何处理大小写。这个handleRequest的示例代码WSO2 API管理器 - 处理使用自定义身份验证器的身份验证失败

public boolean handleRequest(MessageContext messageContext) { 
    try { 
     if (authenticate(messageContext)) { 
      return true; 
     } 
    } catch (APISecurityException e) { 
     e.printStackTrace(); 
    } 
    return false; 
} 

如果我尝试调用具有有效凭据一切的API顺利的话(该方法返回true),我得到一个“HTTP 200 OK”响应。如果我尝试使用无效凭证,则该方法返回false,但是我得到一个HTTP 202 ACCEPTED响应。我想要接收另一个响应代码(例如400)。如何处理此认证失败路径?

谢谢。

+0

您绝对可以处理故障情况。看看处理程序中处理API Manager的节流场景的[1]。 [1] - https://github.com/wso2/carbon-apimgt/blob/release-1.10.x/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/ WSO2 /碳/ apimgt /网关/处理/节流/ APIThrottleHandler.java#L210-L263 – harsha89

回答

0

在理想情况下需要调用handleAuthFaliure方法与消息上下文。

private void handleAuthFailure(MessageContext messageContext, APISecurityException e) 

当调用该方法,请创建APISecurityException对象(如下所列),并通过它,然后错误代码和错误信息将被从拾取在那里,并自动发送错误给客户端(默认情况下,我们返回401安全excep tions和如果定义然后发送定义的错误代码和消息)。

public class APISecurityException extends Exception { 
    private int errorCode; 
    public APISecurityException(int errorCode, String message) { 
     super(message); 
     this.errorCode = errorCode; 
    } 

    public APISecurityException(int errorCode, String message, Throwable cause) { 
     super(message, cause); 
     this.errorCode = errorCode; 
    } 

    public int getErrorCode() { 
     return errorCode; 
    } 
} 

希望这会对你有用。

谢谢

sanjeewa。