簡體   English   中英

WSO2 API管理器-使用自定義身份驗證器處理身份驗證失敗

[英]WSO2 API Manager - Handle Authentication failure with Custom Authenticator

我正在按照本指南https://docs.wso2.com/display/AM190/Writing+Custom+Handlers為wso2-am實現自定義身份驗證處理程序,但是當我的身份驗證處理程序返回false時,尚不清楚如何處理這種情況。 handleRequest的示例代碼是

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)。如何處理此身份驗證失敗路徑?

謝謝。

理想情況下,您需要使用消息上下文調用handleAuthFaliure方法。

private void handleAuthFailure(MessageContext messageContext, APISecurityException e) 

調用該方法時,請創建APISecurityException對象(如下所列)並傳遞它。 然后將從那里選擇錯誤代碼和錯誤消息,並自動將錯誤發送給客戶端(默認情況下,對於安全異常,我們返回401,如果已定義,則發送定義的錯誤代碼和消息)。

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM