我構建了一個應用程式,我的客戶可以在其中處理他的客戶資料。因此,這是一個非常安全的資料集。
為了保護我的應用程式,我添加了一個自定義身份驗證保護過濾器,用于檢查用戶是否有 session isLoggedIn。我將此過濾器添加到每個安全路由:
//AuthGuard.php
class AuthGuard implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
if (!session()->get('isLoggedIn')) {
return redirect()->to('/login');
}
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
}
}
//Routes.php
$routes->add('/list_customer', 'Customer::list_customer', ['filter' => 'authGuard']);
我LoginController正在設定這個會話:
$session_data = [
'id' => $employee->id,
'name' => $employee->name,
'email' => $employee->email,
'isLoggedIn' => true,
'level' => $employee->level,
];
$this->session->set($session_data);
return redirect()->to('/dashboard');
當用戶登錄時,一旦通過身份驗證保護就可以訪問的不同控制器幾乎呈現資料庫中可用的所有資料。我假設,我不需要保護我的模型(就像使用 JWT),因為所有應用程式內容都只能在用戶登錄時訪問。
例如:
class Customer extends BaseController
{
public function list_customer()
{
$customer_model = new CustomerModel();
$data['all_customer'] = $customer_model->findAll(); // <-- this will show ALL customer data. Very very sensitive data!
return view('list_customer', $data);
}
uj5u.com熱心網友回復:
還行吧。我唯一推薦的(不是你做錯了,這只是一個偏好)是使用$filtersunderConfig\Filters而不是為每個路由器定義過濾器。
https://codeigniter4.github.io/userguide/incoming/filters.html#filters
這對我來說更方便,你的選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/396306.html
標籤:php 代码点火器 安全 codeigniter-4
