[英]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.