簡體   English   中英

令牌認證休息API會話

[英]Token Authentication Rest API Session

我正在使用Slim Framework來創建無狀態REST API。 在使用之前,我在服務器端創建了一個SESSION,並在每個頁面上進行會話檢查。 但現在,我不知道如何控制它。

我的數據庫中為每個用戶都有一個api_key。 在用戶登錄后,我使用api_key進行響應並將用戶重定向到index.php。 但是不保留api_key。 如何使用Javascript將api_key傳遞給每個頁面? 原因是如果有人想要我的REST API中的數據,他們必須向我發送api_key,如果用戶在我不想再次顯示登錄頁面之前登錄。

這是我的REST API部分:

$app->post('/userlogin', function() use ($app) {
    verifyRequiredParams(array('email', 'password'));
    $email = $app->request->post('email');
    $password = $app->request->post('password');

    $objUserRegLog = new UserRegistrationLogin;
    $result = $objUserRegLog->getUserByEmailAndPassword($email, $password);
    if (!$result) {
        $response["error"] = true;
        $response["message"] = "Error! Invalid e-mail address or password.";
    } else {
        $response["error"] = false;
        $response["id"] = $result["id"];
        $response["email"] = $result["email"];
        $response["api_key"] = $result["api_key"];
    }
    echoResponse(200, $response);
});

$app->get('/students', 'authenticateStudent', function() use ($app) {
    $objStd = new Students;
    $result = $objCases->getAllStudents();
    if (!$result) {
        $response["error"] = true;
        $response["error_msg"] = "An error occured.";
        $status_code = 404;
    } else {
        $response["error"] = false;
        $response["cases"] = $result;
        $status_code = 200;
    }
    echoResponse($status_code, $response);
});

function authenticateStudent(\Slim\Route $route) {
    $headers = apache_request_headers();
    $response = array();
    $app = \Slim\Slim::getInstance();

    if (isset($headers['Authorization'])) {
        $db = new DbOperation();
        $api_key = $headers['Authorization'];
        if (!$db->isValidStudent($api_key)) {
            $response["error"] = true;
            $response["message"] = "Access Denied. Invalid Api key";
            echoResponse(401, $response);
            $app->stop();
        }
    } else {
        $response["error"] = true;
        $response["message"] = "Api key is misssing";
        echoResponse(400, $response);
        $app->stop();
    }
}

和AJAX一起調用:

$.signin = function() {
    var inputVals = $("#form_signin").serialize();
    $.ajax({
        url : "api/v1/userlogin",
        data : inputVals,
        dataType : "json",
        type : "post",
        success : function(response) {
            if (response.error) {
                $(".popup").trigger("click");
                $(".modal-title").html(response.message_title);
                $(".modal-body").html(response.message);
            } else {
                window.location.href = "index.php";
            }
            console.log(response);
        }
    });
    return false;
}

那么,您需要了解客戶端發送給您的服務器的每個請求都是獨立的,因此您需要在客戶端系統中放置一個變量(令牌),以便讓他在每個請求中發送它,這樣您就知道誰是一直跟你的服務器說話。 開始閱讀: http//www.w3schools.com/php/php_cookies.asp

一旦了解了Cookie的工作原理和方式,請嘗試進一步閱讀有關身份驗證和授權主題的信息。

您有三種方法可以提供此類信息。

餅干

在大多數情況下,如果您有登錄屏幕,則需要使用Hector提到的cookie。 一個潛在的cookie問題,有些人不希望它們來自“隨機”網站。 但是,好處是您可以關閉選項卡,重新打開它,您仍然可以登錄(這取決於cookie的類型,但在大多數情況下,您希望使用此類型)。

請求參數

另一種方法,繁瑣,是添加一個帶有會話標識符的查詢字符串參數。 這也意味着您必須確保頁面上的每個鏈接都包含該會話標識符。 此外,它通常被視為一個安全問題,因為看着你肩膀的人可以看到會話標識符(坦率地說,除非他們有照片記憶......)對於阻止cookie的人,這是另一種方式。 但是,由於它使用的是完全相同類型的會話標識符,因此您可能會侵犯用戶在阻止Cookie時的含義。

HTML代碼

最后一種方法是將值放在HTML中,這需要更多的工作。 例如,您可以使用<meta ...>標記。 至少就是這樣,人們看着你的肩膀是隱藏的。 但是,當某人點擊你的鏈接時,你仍需要傳輸它。 這意味着您必須使用JavaScript中的POST加載任何其他頁面。 那是非常傳統的。 就像以前的方法一樣。 您可能侵犯了用戶“不跟蹤請”的意願。

安全怎么樣?

最安全的是與標記為僅HTTP的cookie建立HTTPS連接(即JavaScript無法訪問它,因此無法使用它進行調整。)

所有其他方法都有其他安全問題。

暫無
暫無

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

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