这不是一个真正的问题,而是如何使用JWT向Wordpress API发出经过身份验证的请求的指南。我写这篇文章是为了提醒自己,也为了那些在同一主题上需要帮助的人。
如何:向WordPress API发出经过JWT身份验证的请求
Why JWT authentication
我正在构建一个使用Wordpress作为后端的网站,以及一个React+Redux应用程序作为前端,因此我通过向Wordpress API发出请求来获取前端的所有内容。有些请求(主要是POST请求)必须经过身份验证,这就是我遇到JWT时的情况。What we need
要在Wordpress中使用JWT身份验证,我们首先需要安装JWT Authentication for WP REST API 插件。正如插件说明中所述,我们还需要修改一些核心Wordpress文件。特别是:在中。htaccess文件包含在Wordpress安装的根文件夹中,我们需要添加以下行:
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
在wp配置中。php文件,也包含在Wordpress安装的根文件夹中,我们需要添加以下行:define(\'JWT_AUTH_SECRET_KEY\', \'your-top-secret-key\'); // Replace \'your-top-secret-key\' with an actual secret key.
define(\'JWT_AUTH_CORS_ENABLE\', true);
Testing to see if JWT is available
为了验证我们现在可以使用JWT,启动Postman并请求Wordpress API的默认“索引”:http://example.com/wp-json/
一些新端点,如/jwt-auth/v1
和/jwt-auth/v1/token
应已添加到API中。如果您可以在对上述请求的响应中找到它们,则意味着JWT现在可用。Getting the JWT token
让我们暂时停留在Postman中,并向Wordpress API请求一个令牌:http://example.com/wp-json/jwt-auth/v1/token
响应将包含JWT令牌,这是一个加密密钥,看起来像这样:EYJ0EXAIIOIJKV1QILCJHBGCIOIJIUZI1NIJ9。Eyjpc3mioijodhrwolwvxc9sb2nhbghvc3q6odg4ofvzm90b3jvb20tbmv4dcisimlhdci6mtuymju5nzq1miwibmijmijoxntiyntk3nduylcjlehaiojelmjmymdidyntisimrhdgeionsidxnlcii6eyjpzci6ijeifx19。hxaaT9iowAX1Xf8RUM42OwbP7QgRNxux8eTtKhWvEUM公司
Making an authenticated request
让我们尝试更改ID为300的帖子的标题,作为使用JWT进行身份验证的请求的示例。在Postman中,选择POST作为方法并键入以下端点:
http://example.com/wp-json/wp/v2/posts/300
在“授权”选项卡中选择“无授权”,并在“标题”选项卡中添加以下内容:\'Content-type\': \'application/json\',
\'Authorization\': \'Bearer jwtToken\' // Replace jwtToken with the actual token (the encrypted key above)
最后,在Body选项卡中,选择raw和JSON(应用程序/JSON)选项,然后在选项正下方的编辑器中键入以下内容:{ "title": "YES! Authenticated requests with JWT work" }
现在您可以点击发送。在response选项卡中查看关于我们请求的帖子的所有数据:标题键的值现在应该是YES! Authenticated requests with JWT work
作为对@grazianodev答案的补充,以下是使用cURL获取授权令牌的方法:
/**
* Generate a JWT token for future API calls to WordPress
*/
private function getToken() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,\'https://site.localhost/wp-json/jwt-auth/v1/token\');
curl_setopt($ch, CURLOPT_POST, 1);
# Admin credentials here
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=admin&password=Str0ngPass");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
if ($server_output === false) {
die(\'Error getting JWT token on WordPress for API integration.\');
}
$server_output = json_decode($server_output);
if ($server_output === null && json_last_error() !== JSON_ERROR_NONE) {
die(\'Invalid response getting JWT token on WordPress for API integration.\');
}
if (!empty($server_output->token)) {
$this->token = $server_output->token; # Token is here
curl_close ($ch);
return true;
} else {
die(\'Invalid response getting JWT token on WordPress for API integration.\');
}
return false;
}
之后,发送标题为“Authorization:Bearer$token”的请求其中$token是上面的getToken()函数返回的令牌。
我个人使用插件“Disable REST API and Require JWT / OAuth Authentication“仅使用上述令牌限制API访问。