折腾:
【未解决】给crifan.com的WordPress网站REST的API添加JWT的token认证
期间,对于WordPress的jwt-auth,现在去用Postman去试试,创建token和验证token。
去用postman访问:
/wp-json/jwt-auth/v1/token
看看效果
去用postman测试jwt-auth:
1 2 3 4 5 6 7 8 | https: //www .crifan.com /wp-json/jwt-auth/v1/token POST { "username" : "root" , "password" : "xxx" } |
结果:
1 2 3 4 5 6 7 | { "code" : "[jwt_auth] invalid_username" , "message" : "<strong>ERROR</strong>: Invalid username. <a href=\"https://www.crifan.com/wp-login.php?action=lostpassword\" title=\"Password Lost and Found\">Lost your password</a>?" , "data" : { "status" : 403 } } |

难道是:就只是此处用户名写错了
去找找之前账号的用户名,重新试试

还真的就是用户名和密码搞错了。
以为是ssh的账号和密码呢。
去换成WordPress的用户名和密码,就正常了。
然后得到了token了。
1 2 3 4 5 6 7 8 9 10 11 12 13 | https: //www .crifan.com /wp-json/jwt-auth/v1/token POST { "username" : "crifan" , "password" : "xxx" } { "token" : "eyxxxiJ9.eyxxxfQ.8xx1v-kEVxxxYB4" , "user_email" : "admin@crifan.com" , "user_nicename" : "crifan" , "user_display_name" : "crifan" } |
参考
1 | config.headers.Authorization = 'Bearer ' + globals.currentUser.token; |
估计后续api中就是加上:
1 | Authorization: Bearer eyxxxiJ9.eyxxxfQ.8xx1v-kEVxxxYB4 |
去试试
再去validate看看
1 2 3 4 | https: //www .crifan.com /wp-json/jwt-auth/v1/token/validate POST Authorization: Bear eyxxx9.eyxxxxfQ.8PxxxB4 |
结果:
1 2 3 4 5 6 7 | { "code" : "jwt_auth_bad_auth_header" , "message" : "Authorization header malformed." , "data" : { "status" : 403 } } |

换了个新生成的token再去试试,问题依旧。
发现是自己写错了,不是:Bear,而是:Bearer
然后就可以了:
1 2 3 4 5 6 | { "code" : "jwt_auth_valid_token" , "data" : { "status" : 200 } } |

转载请注明:在路上 » 【已解决】WordPress的jwt-auth用Postman去测试生成token和验证token