我尝试用CI4创建一个JWT过滤器,但是当我在app/Config/Filter.php
文件中设置我的过滤器时,它总是抛出一个错误,尽管它确实在app/Filters/
目录中。
Class "App\Filters\FilterJwt" not found
我一直试图在app
路径中创建一个新目录,并将其命名为AuthFilter
,但没有成功。
- /配置/过滤器. php*:
<?php
namespace Config;
use App\Filters\FilterJwt;
use CodeIgniter\Config\BaseConfig;
use CodeIgniter\Filters\CSRF;
use CodeIgniter\Filters\DebugToolbar;
use CodeIgniter\Filters\Honeypot;
use CodeIgniter\Filters\InvalidChars;
use CodeIgniter\Filters\SecureHeaders;
class Filters extends BaseConfig
{
/**
* Configures aliases for Filter classes to
* make reading things nicer and simpler.
*/
public array $aliases = [
'csrf' => CSRF::class,
'toolbar' => DebugToolbar::class,
'honeypot' => Honeypot::class,
'invalidchars' => InvalidChars::class,
'secureheaders' => SecureHeaders::class,
'authentification' => FilterJwt::class
];
/**
* List of filter aliases that are always
* applied before and after every request.
*/
public array $globals = [
'before' => [
// 'honeypot',
// 'csrf',
// 'invalidchars',
],
'after' => [
'toolbar',
// 'honeypot',
// 'secureheaders',
],
];
/**
* List of filter aliases that works on a
* particular HTTP method (GET, POST, etc.).
*
* Example:
* 'post' => ['foo', 'bar']
*
* If you use this, you should disable auto-routing because auto-routing
* permits any HTTP method to access a controller. Accessing the controller
* with a method you don’t expect could bypass the filter.
*/
public array $methods = [];
/**
* List of filter aliases that should run on any
* before or after URI patterns.
*
* Example:
* 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']]
* @var array
*/
public $filters = [
'authentification' => [
'before' => [
'users/*',
'users'
]
]
];
}
- /应用程序/过滤器/过滤器Jwt. php:*
<?php
namespace App\Filters;
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\API\ResponseTrait;
use Exeption;
class MyFilter implements FilterInterface
{
use ResponseTrait;
public function before(RequestInterface $request, $arguments = null)
{
$header = $request->getServer('HTTP_AUTHORIZATION');
try {
helper('jwt');
$encodedToken = getJwt($header);
checkJWT($encodedToken);
return $request;
} catch (Exeption $e) {
return Services::response()->setJson([
'error' => $e->getMessage()
])->setStatusCode(ResponseInterface::HTTP_UNAUTHORIZED);
}
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// Do something here
}
}
- 文件在目录中的证明:**
- CI4错误:**
- CI4错误:**
如您所见,CI4框架找不到"Filters"文件夹,尽管它就在那里。
1条答案
按热度按时间mjqavswn1#
代替:
/应用程序/过滤器/过滤器Jwt.php
使用这个:
/应用程序/过滤器/过滤器Jwt.php