<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>网站进行交互 Archives - CNERIS</title>
	<atom:link href="https://cneris.com/zh/tag/%e7%bd%91%e7%ab%99%e8%bf%9b%e8%a1%8c%e4%ba%a4%e4%ba%92/feed/" rel="self" type="application/rss+xml" />
	<link>https://cneris.com/zh/tag/网站进行交互/</link>
	<description></description>
	<lastBuildDate>Tue, 03 Dec 2024 20:53:53 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>如何禁止未登录用户访问 REST API？</title>
		<link>https://cneris.com/zh/%e5%a6%82%e4%bd%95%e7%a6%81%e6%ad%a2%e6%9c%aa%e7%99%bb%e5%bd%95%e7%94%a8%e6%88%b7%e8%ae%bf%e9%97%ae-rest-api%ef%bc%9f/</link>
					<comments>https://cneris.com/zh/%e5%a6%82%e4%bd%95%e7%a6%81%e6%ad%a2%e6%9c%aa%e7%99%bb%e5%bd%95%e7%94%a8%e6%88%b7%e8%ae%bf%e9%97%ae-rest-api%ef%bc%9f/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 03 Dec 2024 20:53:53 +0000</pubDate>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[计算机安全]]></category>
		<category><![CDATA[禁止未登录用户访问]]></category>
		<category><![CDATA[网站进行交互]]></category>
		<category><![CDATA[访问 REST API]]></category>
		<guid isPermaLink="false">https://cneris.com/?p=2730</guid>

					<description><![CDATA[<p>WordPress 的 REST API 是一个强大的工具，可以通过 HTTP 请求与网站进行交互。然而，有时候你可能希望限制 API 的访问，特别是对未登录的用户，以提高安全性或保护隐私。 如何禁止未登录用户访问 REST API？ 默认情况下，WordPress 允许某些 REST API 的端点被公开访问。要限制这些访问，可以使用 rest_authentication_errors 过滤器。以下代码会阻止未登录用户的 REST API 请求，并返回自定义错误信息。 add_filter(‘rest_authentication_errors’, function($result) { if (!empty($result)) { return $result; } if (!is_user_logged_in()) { return new WP_Error(‘rest_not_logged_in’, ‘您必须登录才能访问 REST API。’, array(‘status’ =&gt; 401)); } return $result; }); 这段代码的作用 rest_authentication_errors 过滤器：此过滤器用于处理 REST API 的身份验证错误。 检查已有错误：如果 $result [...]</p>
<p>The post <a href="https://cneris.com/zh/%e5%a6%82%e4%bd%95%e7%a6%81%e6%ad%a2%e6%9c%aa%e7%99%bb%e5%bd%95%e7%94%a8%e6%88%b7%e8%ae%bf%e9%97%ae-rest-api%ef%bc%9f/">如何禁止未登录用户访问 REST API？</a> appeared first on <a href="https://cneris.com/zh">CNERIS</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>WordPress 的 REST API 是一个强大的工具，可以通过 HTTP 请求与网站进行交互。然而，有时候你可能希望限制 API 的访问，特别是对未登录的用户，以提高安全性或保护隐私。<span id="more-2730"></span></p>
<h4>如何禁止未登录用户访问 REST API？</h4>
<p>默认情况下，WordPress 允许某些 REST API 的端点被公开访问。要限制这些访问，可以使用 <code>rest_authentication_errors</code> 过滤器。以下代码会阻止未登录用户的 REST API 请求，并返回自定义错误信息。</p>
<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950">
<blockquote>
<div class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none">add_filter(&#8216;rest_authentication_errors&#8217;, function($result) {<br />
if (!empty($result)) {<br />
return $result;<br />
}<br />
if (!is_user_logged_in()) {<br />
return new WP_Error(&#8216;rest_not_logged_in&#8217;, &#8216;您必须登录才能访问 REST API。&#8217;, array(&#8216;status&#8217; =&gt; 401));<br />
}<br />
return $result;<br />
});</div>
</blockquote>
</div>
<h4>这段代码的作用</h4>
<ol>
<li><strong><code>rest_authentication_errors</code> 过滤器</strong>：此过滤器用于处理 REST API 的身份验证错误。</li>
<li><strong>检查已有错误</strong>：如果 <code>$result</code> 已包含错误，则直接返回。</li>
<li><strong>验证用户会话</strong>：如果用户未登录（<code>!is_user_logged_in()</code>），则返回一个 <code>WP_Error</code>，包含自定义消息和 HTTP 状态代码 401（未授权）。</li>
<li><strong>返回结果</strong>：如果不满足上述条件，则继续处理正常的请求。</li>
</ol>
<h4>结果</h4>
<p>通过这段代码，未登录用户在访问 REST API 时将收到提示信息，要求登录后才能继续操作。这样可以有效保护 REST API 不被未经授权的用户访问。</p>
<p>The post <a href="https://cneris.com/zh/%e5%a6%82%e4%bd%95%e7%a6%81%e6%ad%a2%e6%9c%aa%e7%99%bb%e5%bd%95%e7%94%a8%e6%88%b7%e8%ae%bf%e9%97%ae-rest-api%ef%bc%9f/">如何禁止未登录用户访问 REST API？</a> appeared first on <a href="https://cneris.com/zh">CNERIS</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cneris.com/zh/%e5%a6%82%e4%bd%95%e7%a6%81%e6%ad%a2%e6%9c%aa%e7%99%bb%e5%bd%95%e7%94%a8%e6%88%b7%e8%ae%bf%e9%97%ae-rest-api%ef%bc%9f/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
