JWT Auth白名单

JWT Auth做API鉴权之后,除了鉴权访问之外,需要对一些API做白名单控制,比如IndexNow、Jetpack等插件,都有API的。

做一个例子,来为IndexNow增加白名单。

在未增加白名单前,接口会被拦截:

IndexNow被JWT Auth拦截
IndexNow被JWT Auth拦截

functions.php中增加白名单:

/**
 * JWT Auth WHITELISTING ENDPOINTS.
 *
 * @link https://cn.wordpress.org/plugins/jwt-auth/
 */
add_filter( 'jwt_auth_whitelist', function ( $endpoints ) {
	$your_endpoints = array(
		'/wp-json/indexnow/v_1.0.1/apiKey',
		'/wp-json/indexnow/v_1.0.1/submitUrl',
		'/wp-json/indexnow/v_1.0.1/getStats',
		'/wp-json/indexnow/v_1.0.1/allSubmissions',
	);
	return array_unique( array_merge( $endpoints, $your_endpoints ) );
} );
JWT Auth设置IndexNow白名单
JWT Auth设置IndexNow白名单

增加后,IndexNow插件就可以正常使用了。

最近发现Yoast SEO Premium的API会受到拦截。经测试,其实并不指Yoast SEO,只要API带有问号?就都会被拦截。解决办法是将白名单功能作为WordPress插件来使用即可。

JWT Auth Whitelist
JWT Auth Whitelist

我已经做好了一个白名单插件,下载地址:JWT Auth Whitelist

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注