油桐树 發表於 2022-4-3 17:39:00

[Next.js] Hide Sensitive Information from the Consumers of Next.js API

<p>We'll learn how to use Next.js API Routes to hide sensitive information from the clients. In this case, we're calling the JSON Placeholder API with a "secret" value in the headers. All that sensitive information is hidden from the clients since they don't call, or even know, that we're calling the JSON Placeholder API under the hood.</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>async <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> getSuperSecretData() {
const result </span>= await fetch("https://jsonplaceholder.typicode.com/todos/1"<span style="color: rgba(0, 0, 0, 1)">, {
    headers: {
      authorization: </span>'SUPER SECRET VALUE'<span style="color: rgba(0, 0, 0, 1)">
    }
}).then(res </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> res.json())

</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result
}

async </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> handler(req, res) {
const secretTodo </span>=<span style="color: rgba(0, 0, 0, 1)"> await getSuperSecretData()

res.json({todo: secretTodo})
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> handler<br><br></pre>
</div>
<p>authorization header won't be seen from the request in backend</p>
<p><img src="https://img2022.cnblogs.com/blog/364241/202204/364241-20220403173839886-105976748.png" alt=""></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/Answer1215/p/16096703.html
頁: [1]
查看完整版本: [Next.js] Hide Sensitive Information from the Consumers of Next.js API