簡體   English   中英

如果用戶未登錄,則無法重定向到登錄頁面

[英]Unable to redirect to login page if user is not logged in

我有一個在調用Web服務時執行的函數。 如果用戶未登錄,我想重定向到登錄頁面。 我正在使用的前端是AngularJS。

重定向在else部分。

這是功能:

public function postCustomerGetAction(Request $request) {
$tokenValidationObj = new LoginController();
$isValid = $tokenValidationObj->postValidateUserTokenAction($request);

// print_r($this->customer_id);exit;
// print_r($this->association_id);

// print_r($isValid);exit;
if($isValid == 1){
    $log = new LogController();     
    $log->getLogJsonBody($this->get('logger'),$request);
    $display = new Result();
    try {
        $schema = 'customerGet.json';
        $validation = new InputValidation();
        $validationObj = $validation->dataValidation($request, $schema);
        $getKeyMap = array (
                "_customer_id"      => "customerId",
                "_customer_name"    => "customerName"
        );
        $getFunction = 'svk_apt_get_customer';
        $conObj = new ConnectionController();
        $conObj->beginTransaction();
        $dbres = $conObj->buildQuery($getFunction, $getKeyMap, $validationObj);
        $result = $display->getJsonResult($dbres);
        $conObj->dbCommitTransaction();
    } catch ( Exception $ex ) {
        $log->getExceptionLog($this->get('logger'),$ex);
        $result = $display->getJsonException($ex);
    }
    return $result;
}else{
    $result = $this->container->getParameter('email_template_url');
    // print_r($result);exit;
    return new RedirectResponse($result);
    // header('Location: https://stackoverflow.com');exit;
    // return $this->redirect('https://stackoverflow.com');
}               

}

在響應中,我看到以下html,但是它沒有重定向到我指定的頁面。 響應中的html:

<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <meta http-equiv="refresh" content="0;url=https://stackoverflow.com" />

            <title>Redirecting to https://stackoverflow.com</title>
        </head>
        <body>
            Redirecting to <a href="https://stackoverflow.com">https://stackoverflow.com</a>.
        </body>
    </html>

調用Web服務且用戶未登錄時如何重定向?

Web服務/ API不應將前端重定向到另一個頁面。 如果您正在使用Angular與該Web服務進行交互,則應僅返回“未登錄”狀態/標志,然后使Angular進行重定向。

對於創建自定義響應,您可以參考: Symfony安全返回401響應而不是重定向

HTTP狀態401應該足夠了。 此處有關此狀態的更多詳細信息: https : //httpstatuses.com/401

Angular應該檢測到該錯誤,然后重定向。

暫無
暫無

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

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