{"id":3016,"date":"2023-10-28T14:26:07","date_gmt":"2023-10-28T05:26:07","guid":{"rendered":"https:\/\/edu.ujhb.org\/?p=3016"},"modified":"2023-10-28T14:26:07","modified_gmt":"2023-10-28T05:26:07","slug":"angular%e5%ae%89%e5%85%a8%e5%92%8c%e8%ae%a4%e8%af%81%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/edu.ujhb.org\/?p=3016","title":{"rendered":"Angular\u5b89\u5168\u548c\u8ba4\u8bc1\u6559\u7a0b"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Angular\u524d\u7aef\u5f00\u53d1\u5b89\u5168\u548c\u8ba4\u8bc1\u6559\u7a0b\u5927\u7eb2<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e00\u7ae0\uff1a\u5f15\u8a00<\/h2>\n\n\n\n<p>1.1 \u4e3a\u4ec0\u4e48\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u5f88\u91cd\u8981<br>1.2 Angular\u5728\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u65b9\u9762\u7684\u4f18\u52bf\u548c\u6311\u6218<br>1.3 \u672c\u6559\u7a0b\u76ee\u6807\u548c\u9884\u5907\u77e5\u8bc6<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840<\/h2>\n\n\n\n<p>2.1 DOM\u7684\u5b89\u5168\u6027<br>2.2 HTTP\u5b89\u5168<br>2.3 \u8def\u7531\u5b89\u5168<br>2.4 Angular\u5b89\u5168\u6700\u4f73\u5b9e\u8df5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e09\u7ae0\uff1a\u8ba4\u8bc1\u57fa\u7840<\/h2>\n\n\n\n<p>3.1 \u4ec0\u4e48\u662f\u8ba4\u8bc1<br>3.2 \u8ba4\u8bc1\u7684\u79cd\u7c7b<br>3.3 \u5982\u4f55\u5728Angular\u4e2d\u5b9e\u73b0\u8ba4\u8bc1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u56db\u7ae0\uff1a\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1<\/h2>\n\n\n\n<p>4.1 \u4ec0\u4e48\u662fJWT<br>4.2 JWT\u7684\u5de5\u4f5c\u539f\u7406<br>4.3 \u5728Angular\u4e2d\u4f7f\u7528JWT<br>4.4 JWT\u5b89\u5168\u6027\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e94\u7ae0\uff1a\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1<\/h2>\n\n\n\n<p>5.1 \u4ec0\u4e48\u662fOAuth 2.0<br>5.2 OAuth 2.0\u7684\u5de5\u4f5c\u539f\u7406<br>5.3 \u5728Angular\u4e2d\u4f7f\u7528OAuth 2.0<br>5.4 OAuth 2.0\u5b89\u5168\u6027\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u516d\u7ae0\uff1a\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1<\/h2>\n\n\n\n<p>6.1 \u4ec0\u4e48\u662fFirebase<br>6.2 Firebase\u7684\u5de5\u4f5c\u539f\u7406<br>6.3 \u5728Angular\u4e2d\u4f7f\u7528Firebase<br>6.4 Firebase\u5b89\u5168\u6027\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e03\u7ae0\uff1a\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406<\/h2>\n\n\n\n<p>7.1 \u4ec0\u4e48\u662f\u89d2\u8272\u548c\u6743\u9650<br>7.2 \u5982\u4f55\u5728Angular\u4e2d\u7ba1\u7406\u89d2\u8272\u548c\u6743\u9650<br>7.3 \u89d2\u8272\u548c\u6743\u9650\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u516b\u7ae0\uff1a\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5<\/h2>\n\n\n\n<p>8.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u5b89\u5168\u6027\u5ba1\u8ba1<br>8.2 \u5982\u4f55\u8fdb\u884c\u5b89\u5168\u6027\u6d4b\u8bd5<br>8.3 \u4f7f\u7528\u5de5\u5177\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u8ba1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u4e5d\u7ae0\uff1a\u603b\u7ed3\u548c\u672a\u6765\u5c55\u671b<\/h2>\n\n\n\n<p>9.1 \u672c\u6559\u7a0b\u603b\u7ed3<br>9.2 \u672a\u6765\u7684\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u8d8b\u52bf<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7b2c\u5341\u7ae0\uff1a\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u8d44\u6e90<\/h2>\n\n\n\n<p>10.1 \u6587\u6863\u548c\u6559\u7a0b<br>10.2 \u5728\u7ebf\u8bfe\u7a0b<br>10.3 \u793e\u533a\u548c\u8bba\u575b<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u662f\u672c\u6559\u7a0b\u7684\u5927\u7eb2\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u9010\u7ae0\u9010\u8282\u8fdb\u884c\u8be6\u7ec6\u7684\u6559\u7a0b\u89e3\u91ca\u3002\u6bcf\u6b21\u56de\u7b54\u5c06\u53ea\u8be6\u7ec6\u89e3\u91ca\u4e00\u4e2a\u5c0f\u8282\u7684\u5185\u5bb9\u3002\u8bf7\u786e\u8ba4\u5927\u7eb2\u662f\u5426\u7b26\u5408\u60a8\u7684\u9700\u6c42\uff0c\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u8be6\u7ec6\u7684\u6559\u7a0b\u7f16\u5199\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e00\u7ae0\uff1a\u5f15\u8a00\u76841.1 \u4e3a\u4ec0\u4e48\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u5f88\u91cd\u8981ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e00\u7ae0\uff1a\u5f15\u8a00<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 \u4e3a\u4ec0\u4e48\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u5f88\u91cd\u8981<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728\u5f53\u4eca\u7684\u4e92\u8054\u7f51\u73af\u5883\u4e2d\uff0c\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u65e0\u8bba\u662f\u4e2a\u4eba\u7f51\u7ad9\u3001\u4f01\u4e1a\u5e94\u7528\u8fd8\u662f\u5927\u578b\u7684\u5728\u7ebf\u5e73\u53f0\uff0c\u90fd\u9762\u4e34\u7740\u5404\u79cd\u5b89\u5168\u5a01\u80c1\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u6570\u636e\u6cc4\u9732\u3001\u8eab\u4efd\u76d7\u7a83\u548c\u672a\u7ecf\u6388\u6743\u7684\u8bbf\u95ee\u3002\u8fd9\u4e9b\u5a01\u80c1\u4e0d\u4ec5\u53ef\u80fd\u5bfc\u81f4\u8d22\u52a1\u635f\u5931\uff0c\u8fd8\u53ef\u80fd\u635f\u5bb3\u4f01\u4e1a\u548c\u4e2a\u4eba\u7684\u58f0\u8a89\u3002<\/p>\n\n\n\n<p>\u8ba4\u8bc1\u662f\u5b89\u5168\u6027\u7684\u4e00\u4e2a\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u5b83\u786e\u4fdd\u53ea\u6709\u7ecf\u8fc7\u6388\u6743\u7684\u7528\u6237\u624d\u80fd\u8bbf\u95ee\u7279\u5b9a\u7684\u8d44\u6e90\u548c\u6570\u636e\u3002\u6ca1\u6709\u6709\u6548\u7684\u8ba4\u8bc1\u673a\u5236\uff0c\u6076\u610f\u7528\u6237\u53ef\u80fd\u4f1a\u8f7b\u6613\u5730\u8bbf\u95ee\u548c\u7be1\u6539\u654f\u611f\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u5728\u524d\u7aef\u5f00\u53d1\u4e2d\uff0c\u5c24\u5176\u662f\u4f7f\u7528Angular\u8fd9\u6837\u7684\u73b0\u4ee3\u6846\u67b6\u65f6\uff0c\u5f00\u53d1\u8005\u9700\u8981\u66f4\u52a0\u5173\u6ce8\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u3002\u56e0\u4e3a\u524d\u7aef\u4ee3\u7801\u662f\u5728\u7528\u6237\u7684\u6d4f\u89c8\u5668\u4e2d\u8fd0\u884c\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u66f4\u5bb9\u6613\u53d7\u5230\u5404\u79cd\u653b\u51fb\uff0c\u5982XSS\uff08\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff09\u548cCSRF\uff08\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u867d\u7136\u8fd9\u4e00\u5c0f\u8282\u4e3b\u8981\u662f\u7406\u8bba\u6027\u7684\uff0c\u4f46\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Angular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Angular\u7684\u5185\u7f6e\u6307\u4ee4\u6765\u9632\u6b62XSS\u653b\u51fb\u3002<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Component } from '@angular\/core'; import { DomSanitizer, SafeHtml } from '@angular\/platform-browser'; @Component({ selector: 'app-root', template: `&lt;div [innerHTML]=\"safeHtml\"&gt;&lt;\/div&gt;` }) export class AppComponent { safeHtml: SafeHtml; constructor(private sanitizer: DomSanitizer) { const html = '&lt;script&gt;alert(\"This is an attack!\")&lt;\/script&gt;'; this.safeHtml = this.sanitizer.bypassSecurityTrustHtml(html); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Angular\u7684<code>DomSanitizer<\/code>\u670d\u52a1\u6765\u6e05\u7406\u4e0d\u5b89\u5168\u7684HTML\uff0c\u4ece\u800c\u9632\u6b62XSS\u653b\u51fb\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u5728Angular\u524d\u7aef\u5f00\u53d1\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u539f\u56e0\u3002\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u5c06\u66f4\u8be6\u7ec6\u5730\u4ecb\u7ecd\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u8fd9\u4e9b\u5b89\u5168\u63aa\u65bd\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e00\u7ae0\uff1a\u5f15\u8a00\u76841.2 Angular\u5728\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u65b9\u9762\u7684\u4f18\u52bf\u548c\u6311\u6218ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e00\u7ae0\uff1a\u5f15\u8a00<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1.2 Angular\u5728\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u65b9\u9762\u7684\u4f18\u52bf\u548c\u6311\u6218<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f18\u52bf<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5185\u7f6e\u5b89\u5168\u6027\u7279\u6027<\/strong>: Angular\u6846\u67b6\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u7684\u5b89\u5168\u6027\u7279\u6027\uff0c\u5982DOM\u7684\u5b89\u5168\u6027\u5904\u7406\u548cHTTP\u5b89\u5168\u6027\uff0c\u8fd9\u4e9b\u7279\u6027\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u5bb9\u6613\u5730\u6784\u5efa\u5b89\u5168\u7684\u5e94\u7528\u3002<\/li>\n\n\n\n<li><strong>\u793e\u533a\u652f\u6301<\/strong>: Angular\u6709\u4e00\u4e2a\u5e9e\u5927\u7684\u5f00\u53d1\u8005\u793e\u533a\u548c\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u8fd9\u4e9b\u5e93\u901a\u5e38\u5305\u62ec\u5404\u79cd\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n\n\n\n<li><strong>\u6a21\u5757\u5316\u548c\u7ec4\u4ef6\u5316<\/strong>: Angular\u7684\u6a21\u5757\u5316\u548c\u7ec4\u4ef6\u5316\u67b6\u6784\u4f7f\u5f97\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u7684\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u7ef4\u62a4\u3002<\/li>\n\n\n\n<li><strong>\u4e0e\u540e\u7aef\u7684\u826f\u597d\u96c6\u6210<\/strong>: Angular\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u4e0e\u5404\u79cd\u540e\u7aef\u6280\u672f\uff08\u5982Node.js, Java, .NET\u7b49\uff09\u96c6\u6210\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u6574\u5957\u6210\u719f\u7684\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u673a\u5236\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u6311\u6218<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u590d\u6742\u6027<\/strong>: Angular\u662f\u4e00\u4e2a\u529f\u80fd\u4e30\u5bcc\u4f46\u76f8\u5bf9\u590d\u6742\u7684\u6846\u67b6\uff0c\u8fd9\u53ef\u80fd\u4f1a\u589e\u52a0\u5b9e\u73b0\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u7684\u590d\u6742\u6027\u3002<\/li>\n\n\n\n<li><strong>\u524d\u7aef\u5b89\u5168\u6027\u7684\u5c40\u9650\u6027<\/strong>: \u7531\u4e8eAngular\u662f\u4e00\u4e2a\u524d\u7aef\u6846\u67b6\uff0c\u6709\u4e9b\u5b89\u5168\u6027\u95ee\u9898\u9700\u8981\u540e\u7aef\u7684\u652f\u6301\u624d\u80fd\u5b8c\u5168\u89e3\u51b3\u3002<\/li>\n\n\n\n<li><strong>\u7248\u672c\u66f4\u65b0<\/strong>: Angular\u7ecf\u5e38\u66f4\u65b0\uff0c\u65b0\u7248\u672c\u53ef\u80fd\u4f1a\u5f15\u5165\u65b0\u7684\u5b89\u5168\u6027\u7279\u6027\u6216\u6539\u53d8\u73b0\u6709\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u8fd9\u8981\u6c42\u5f00\u53d1\u8005\u9700\u8981\u6301\u7eed\u5173\u6ce8\u548c\u5b66\u4e60\u3002<\/li>\n\n\n\n<li><strong>\u7b2c\u4e09\u65b9\u5e93\u7684\u98ce\u9669<\/strong>: \u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u867d\u7136\u65b9\u4fbf\uff0c\u4f46\u4e5f\u53ef\u80fd\u5e26\u6765\u5b89\u5168\u98ce\u9669\uff0c\u7279\u522b\u662f\u5f53\u8fd9\u4e9b\u5e93\u6ca1\u6709\u5f97\u5230\u826f\u597d\u7ef4\u62a4\u6216\u8005\u6ca1\u6709\u7ecf\u8fc7\u5b89\u5168\u5ba1\u8ba1\u65f6\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u8fd9\u4e00\u5c0f\u8282\u4e3b\u8981\u662f\u6982\u5ff5\u6027\u7684\u5185\u5bb9\uff0c\u4ee3\u7801\u793a\u4f8b\u53ef\u80fd\u4e0d\u592a\u9002\u7528\u3002\u4f46\u4e3a\u4e86\u7ed9\u51fa\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Angular\u7684<code>HttpClient<\/code>\u6a21\u5757\u5b89\u5168\u5730\u53d1\u9001HTTP\u8bf7\u6c42\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class AuthService { constructor(private http: HttpClient) {} login(username: string, password: string) { const headers = new HttpHeaders({ 'Content-Type': 'application\/json' }); return this.http.post('\/api\/login', { username, password }, { headers }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Angular\u7684<code>HttpClient<\/code>\u6a21\u5757\uff0c\u5e76\u8bbe\u7f6e\u4e86HTTP\u5934\u6765\u786e\u4fdd\u8bf7\u6c42\u7684\u5b89\u5168\u6027\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u4e86\u89e3\u4e86Angular\u5728\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u65b9\u9762\u7684\u4f18\u52bf\u548c\u6311\u6218\u3002\u5728\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5145\u5206\u5229\u7528\u8fd9\u4e9b\u4f18\u52bf\uff0c\u540c\u65f6\u5e94\u5bf9\u8fd9\u4e9b\u6311\u6218\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e00\u7ae0\uff1a\u5f15\u8a00\u76841.3 \u672c\u6559\u7a0b\u76ee\u6807\u548c\u9884\u5907\u77e5\u8bc6ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e00\u7ae0\uff1a\u5f15\u8a00<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">1.3 \u672c\u6559\u7a0b\u76ee\u6807\u548c\u9884\u5907\u77e5\u8bc6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u6559\u7a0b\u76ee\u6807<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7406\u89e3\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u7684\u91cd\u8981\u6027<\/strong>: \u672c\u6559\u7a0b\u65e8\u5728\u8ba9\u4f60\u660e\u767d\u4e3a\u4ec0\u4e48\u5728Angular\u524d\u7aef\u5f00\u53d1\u4e2d\uff0c\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002<\/li>\n\n\n\n<li><strong>\u638c\u63e1Angular\u7684\u5b89\u5168\u6027\u7279\u6027<\/strong>: \u4f60\u5c06\u5b66\u4e60\u5230Angular\u6846\u67b6\u5185\u7f6e\u7684\u5404\u79cd\u5b89\u5168\u6027\u673a\u5236\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u6765\u4fdd\u62a4\u4f60\u7684\u5e94\u7528\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u73b0\u8ba4\u8bc1\u673a\u5236<\/strong>: \u672c\u6559\u7a0b\u5c06\u6559\u4f60\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u591a\u79cd\u8ba4\u8bc1\u673a\u5236\uff0c\u5305\u62ecJWT\u3001OAuth 2.0\u548cFirebase\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406<\/strong>: \u4f60\u5c06\u4e86\u89e3\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u7ba1\u7406\u7528\u6237\u89d2\u8272\u548c\u6743\u9650\uff0c\u4ee5\u5b9e\u73b0\u66f4\u7ec6\u7c92\u5ea6\u7684\u8bbf\u95ee\u63a7\u5236\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5<\/strong>: \u6700\u540e\uff0c\u4f60\u5c06\u5b66\u4e60\u5982\u4f55\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u5e94\u7528\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">\u9884\u5907\u77e5\u8bc6<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u57fa\u7840\u7684\u524d\u7aef\u5f00\u53d1\u77e5\u8bc6<\/strong>: \u5305\u62ecHTML, CSS, \u548cJavaScript\u3002<\/li>\n\n\n\n<li><strong>Angular\u57fa\u7840<\/strong>: \u7531\u4e8e\u672c\u6559\u7a0b\u662f\u5728Angular\u524d\u7aef\u5f00\u53d1\u57fa\u7840\u6559\u7a0b\u4e4b\u540e\u7684\u8fdb\u9636\u5185\u5bb9\uff0c\u56e0\u6b64\u9700\u8981\u4f60\u5bf9Angular\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u3002<\/li>\n\n\n\n<li><strong>\u57fa\u7840\u7684\u7f51\u7edc\u548c\u5b89\u5168\u6982\u5ff5<\/strong>: \u4e86\u89e3HTTP\u534f\u8bae\u3001API\u4ea4\u4e92\u3001\u4ee5\u53ca\u57fa\u7840\u7684\u7f51\u7edc\u5b89\u5168\u6982\u5ff5\u5c06\u6709\u52a9\u4e8e\u4f60\u66f4\u597d\u5730\u7406\u89e3\u672c\u6559\u7a0b\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u7531\u4e8e\u8fd9\u4e00\u5c0f\u8282\u4e3b\u8981\u662f\u4ecb\u7ecd\u6027\u8d28\u7684\uff0c\u4ee3\u7801\u793a\u4f8b\u53ef\u80fd\u4e0d\u592a\u9002\u7528\u3002\u4f46\u4e3a\u4e86\u8ba9\u4f60\u6709\u4e00\u4e2a\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Angular\u767b\u5f55\u7ec4\u4ef6\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Component } from '@angular\/core'; import { AuthService } from '.\/auth.service'; @Component({ selector: 'app-login', template: ` &lt;form (ngSubmit)=\"login(username.value, password.value)\"&gt; &lt;input #username type=\"text\" placeholder=\"Username\"&gt; &lt;input #password type=\"password\" placeholder=\"Password\"&gt; &lt;button type=\"submit\"&gt;Login&lt;\/button&gt; &lt;\/form&gt; ` }) export class LoginComponent { constructor(private authService: AuthService) {} login(username: string, password: string) { this.authService.login(username, password).subscribe(response =&gt; { \/\/ Handle login success }, error =&gt; { \/\/ Handle login error }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u5355\u6765\u6536\u96c6\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5e76\u901a\u8fc7<code>AuthService<\/code>\u670d\u52a1\u6765\u5904\u7406\u767b\u5f55\u903b\u8f91\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u4e86\u89e3\u4e86\u672c\u6559\u7a0b\u7684\u76ee\u6807\u548c\u4f60\u9700\u8981\u51c6\u5907\u7684\u77e5\u8bc6\u3002\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5404\u79cd\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840\u76842.1 DOM\u7684\u5b89\u5168\u6027ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">2.1 DOM\u7684\u5b89\u5168\u6027<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>DOM\uff08Document Object Model\uff09\u662fWeb\u5f00\u53d1\u4e2d\u4e00\u4e2a\u975e\u5e38\u6838\u5fc3\u7684\u6982\u5ff5\uff0c\u5b83\u662f\u6d4f\u89c8\u5668\u4e2d\u9875\u9762\u5185\u5bb9\u7684\u7ed3\u6784\u5316\u8868\u793a\u3002\u7136\u800c\uff0cDOM\u4e5f\u662f\u524d\u7aef\u5b89\u5168\u4e2d\u6700\u5bb9\u6613\u88ab\u653b\u51fb\u7684\u90e8\u5206\uff0c\u5c24\u5176\u662f\u901a\u8fc7XSS\uff08\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff09\u3002<\/p>\n\n\n\n<p>\u5728Angular\u4e2d\uff0c\u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u5185\u7f6e\u673a\u5236\u6765\u589e\u5f3aDOM\u7684\u5b89\u5168\u6027\u3002\u4f8b\u5982\uff0cAngular\u4f1a\u81ea\u52a8\u5bf9\u63d2\u5165\u5230DOM\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u8f6c\u4e49\uff0c\u4ee5\u9632\u6b62XSS\u653b\u51fb\u3002\u6b64\u5916\uff0cAngular\u8fd8\u63d0\u4f9b\u4e86<code>DomSanitizer<\/code>\u670d\u52a1\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u5fc5\u8981\u65f6\u624b\u52a8\u6e05\u7406\u4e0d\u5b89\u5168\u7684HTML\u3001CSS\u548cURL\u3002<\/p>\n\n\n\n<p>\u7406\u89e3\u548c\u5229\u7528Angular\u7684\u8fd9\u4e9bDOM\u5b89\u5168\u6027\u7279\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u8fd9\u76f4\u63a5\u5173\u7cfb\u5230\u7528\u6237\u6570\u636e\u548c\u5e94\u7528\u5b89\u5168\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>DomSanitizer<\/code>\u6765\u6e05\u7406\u4e0d\u5b89\u5168\u7684HTML\u3002<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Component } from '@angular\/core'; import { DomSanitizer, SafeHtml } from '@angular\/platform-browser'; @Component({ selector: 'app-safe-dom', template: `&lt;div [innerHTML]=\"safeHtml\"&gt;&lt;\/div&gt;` }) export class SafeDomComponent { safeHtml: SafeHtml; constructor(private sanitizer: DomSanitizer) { const unsafeHtml = '&lt;script&gt;alert(\"XSS Attack!\");&lt;\/script&gt;&lt;p&gt;Some text.&lt;\/p&gt;'; this.safeHtml = this.sanitizer.bypassSecurityTrustHtml(unsafeHtml); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>DomSanitizer<\/code>\u548c<code>SafeHtml<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>DomSanitizer<\/code>\u7684<code>bypassSecurityTrustHtml<\/code>\u65b9\u6cd5\u6765\u6e05\u7406\u4e0d\u5b89\u5168\u7684HTML\u4ee3\u7801\u3002\u8fd9\u6837\uff0c\u5373\u4f7fHTML\u4ee3\u7801\u4e2d\u5305\u542b\u4e86\u6f5c\u5728\u7684XSS\u653b\u51fb\u811a\u672c\uff0c\u5b83\u4e5f\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cAngular\u5e2e\u52a9\u6211\u4eec\u589e\u5f3a\u4e86DOM\u7684\u5b89\u5168\u6027\uff0c\u4ece\u800c\u66f4\u6709\u6548\u5730\u9632\u8303\u4e86XSS\u7b49\u653b\u51fb\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u8ba8Angular\u5728\u5176\u4ed6\u65b9\u9762\u7684\u5b89\u5168\u6027\u7279\u6027\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840\u76842.2 HTTP\u5b89\u5168ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">2.2 HTTP\u5b89\u5168<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>HTTP\uff08HyperText Transfer Protocol\uff09\u662fWeb\u5e94\u7528\u4e2d\u7528\u4e8e\u6570\u636e\u4f20\u8f93\u7684\u57fa\u7840\u534f\u8bae\u3002\u7136\u800c\uff0cHTTP\u8bf7\u6c42\u548c\u54cd\u5e94\u4e5f\u662f\u5b89\u5168\u98ce\u9669\u7684\u5e38\u89c1\u6765\u6e90\uff0c\u4f8b\u5982\u4e2d\u95f4\u4eba\u653b\u51fb\uff08Man-in-the-Middle Attack\uff09\u548c\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff08CSRF\uff09\u3002<\/p>\n\n\n\n<p>Angular\u901a\u8fc7\u5176<code>HttpClient<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u5b89\u5168\u6027\u63aa\u65bd\u6765\u4fdd\u62a4HTTP\u901a\u4fe1\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>HTTPS\u652f\u6301<\/strong>: Angular\u63a8\u8350\u4f7f\u7528HTTPS\u800c\u4e0d\u662fHTTP\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u7684\u5b89\u5168\u6027\u3002<\/li>\n\n\n\n<li><strong>XSRF\u4fdd\u62a4<\/strong>: Angular\u7684<code>HttpClient<\/code>\u6a21\u5757\u5185\u7f6e\u4e86\u5bf9XSRF\uff08\u4e5f\u79f0\u4e3aCSRF\uff09\u7684\u9632\u62a4\u673a\u5236\u3002\u5b83\u4f1a\u81ea\u52a8\u4ece\u670d\u52a1\u5668\u7684\u54cd\u5e94\u4e2d\u83b7\u53d6\u4e00\u4e2a\u540d\u4e3a\u201cXSRF-TOKEN\u201d\u7684cookie\uff0c\u5e76\u5728\u540e\u7eed\u7684\u8bf7\u6c42\u4e2d\u5c06\u5176\u4f5c\u4e3aHTTP\u5934\u53d1\u9001\u56de\u53bb\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u7684HTTP\u5934<\/strong>: <code>HttpClient<\/code>\u5141\u8bb8\u4f60\u81ea\u5b9a\u4e49HTTP\u5934\uff0c\u8fd9\u6837\u4f60\u53ef\u4ee5\u6dfb\u52a0\u4f8b\u5982<code>Content-Security-Policy<\/code>\u8fd9\u6837\u7684\u5b89\u5168\u6027\u5934\u90e8\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u53ef\u53d8\u7684\u8bf7\u6c42\u5bf9\u8c61<\/strong>: Angular\u4e2d\u7684HTTP\u8bf7\u6c42\u5bf9\u8c61\u662f\u4e0d\u53ef\u53d8\u7684\uff08Immutable\uff09\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\uff0c\u4f60\u4e0d\u80fd\u66f4\u6539\u5b83\uff0c\u8fd9\u6709\u52a9\u4e8e\u9632\u6b62\u5728\u5e94\u7528\u8fd0\u884c\u65f6\u5bf9\u8bf7\u6c42\u8fdb\u884c\u7be1\u6539\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Angular\u7684<code>HttpClient<\/code>\u6a21\u5757\u53d1\u9001\u5b89\u5168HTTP\u8bf7\u6c42\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class SecureHttpService { constructor(private http: HttpClient) {} fetchData() { const headers = new HttpHeaders({ 'Content-Type': 'application\/json', 'Authorization': 'Bearer YOUR_ACCESS_TOKEN', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block' }); return this.http.get('\/api\/data', { headers }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>HttpClient<\/code>\u548c<code>HttpHeaders<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>SecureHttpService<\/code>\u7684\u670d\u52a1\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>fetchData<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f7f\u7528<code>HttpClient<\/code>\u7684<code>get<\/code>\u51fd\u6570\u53d1\u9001\u4e00\u4e2aHTTP GET\u8bf7\u6c42\uff0c\u5e76\u901a\u8fc7<code>HttpHeaders<\/code>\u8bbe\u7f6e\u4e86\u591a\u4e2a\u5b89\u5168\u76f8\u5173\u7684HTTP\u5934\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u6837\u7684\u8bbe\u7f6e\uff0cAngular\u5e2e\u52a9\u6211\u4eec\u5728HTTP\u5c42\u9762\u589e\u52a0\u4e86\u989d\u5916\u7684\u5b89\u5168\u9632\u62a4\uff0c\u4ece\u800c\u66f4\u6709\u6548\u5730\u9632\u6b62\u5404\u79cd\u7f51\u7edc\u653b\u51fb\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u8ba8Angular\u5728\u5176\u4ed6\u65b9\u9762\u7684\u5b89\u5168\u6027\u7279\u6027\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840\u76842.3 \u8def\u7531\u5b89\u5168ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">2.3 \u8def\u7531\u5b89\u5168<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728\u5355\u9875\u9762\u5e94\u7528\uff08SPA\uff09\u4e2d\uff0c\u8def\u7531\u662f\u975e\u5e38\u5173\u952e\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u51b3\u5b9a\u4e86\u7528\u6237\u53ef\u4ee5\u8bbf\u95ee\u54ea\u4e9b\u9875\u9762\u548c\u8d44\u6e90\u3002\u4e0d\u6b63\u786e\u6216\u4e0d\u5b89\u5168\u7684\u8def\u7531\u8bbe\u7f6e\u53ef\u80fd\u4f1a\u5bfc\u81f4\u672a\u7ecf\u6388\u6743\u7684\u7528\u6237\u8bbf\u95ee\u654f\u611f\u4fe1\u606f\u6216\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\uff0c\u56e0\u6b64\u8def\u7531\u5b89\u5168\u662f\u524d\u7aef\u5b89\u5168\u4e2d\u4e0d\u53ef\u5ffd\u89c6\u7684\u4e00\u73af\u3002<\/p>\n\n\n\n<p>Angular\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u8def\u7531\u5b89\u5168\u673a\u5236\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8def\u7531\u5b88\u536b\uff08Route Guards\uff09<\/strong>: Angular\u7684\u8def\u7531\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u7c7b\u578b\u7684\u5b88\u536b\uff0c\u5982<code>CanActivate<\/code>\u3001<code>CanDeactivate<\/code>\u548c<code>CanLoad<\/code>\u7b49\uff0c\u7528\u4e8e\u5728\u8def\u7531\u53d1\u751f\u4e4b\u524d\u6216\u4e4b\u540e\u6267\u884c\u7279\u5b9a\u7684\u903b\u8f91\u3002<\/li>\n\n\n\n<li><strong>\u61d2\u52a0\u8f7d\u548c\u9884\u52a0\u8f7d\u7b56\u7565<\/strong>: \u901a\u8fc7\u61d2\u52a0\u8f7d\uff08Lazy Loading\uff09\u548c\u9884\u52a0\u8f7d\uff08Preloading\uff09\u7b56\u7565\uff0c\u4f60\u53ef\u4ee5\u66f4\u7ec6\u81f4\u5730\u63a7\u5236\u54ea\u4e9b\u6a21\u5757\u548c\u8d44\u6e90\u5e94\u8be5\u88ab\u52a0\u8f7d\uff0c\u4ece\u800c\u51cf\u5c11\u5b89\u5168\u98ce\u9669\u3002<\/li>\n\n\n\n<li><strong>\u8def\u7531\u6570\u636e\u548c\u89e3\u6790\uff08Route Data and Resolvers\uff09<\/strong>: Angular\u5141\u8bb8\u4f60\u5728\u8def\u7531\u914d\u7f6e\u4e2d\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6570\u636e\u548c\u89e3\u6790\u5668\uff0c\u8fd9\u4e9b\u6570\u636e\u548c\u89e3\u6790\u5668\u53ef\u4ee5\u5728\u8def\u7531\u6fc0\u6d3b\u4e4b\u524d\u6267\u884c\uff0c\u7528\u4e8e\u9a8c\u8bc1\u7528\u6237\u6743\u9650\u6216\u9884\u5148\u83b7\u53d6\u6570\u636e\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Angular\u8def\u7531\u5b88\u536b\u6765\u5b9e\u73b0\u8def\u7531\u5b89\u5168\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Injectable } from '@angular\/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular\/router'; import { AuthService } from '.\/auth.service'; @Injectable({ providedIn: 'root' }) export class AuthGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { if (this.authService.isAuthenticated()) { return true; } else { this.router.navigate(['\/login']); return false; } } }<\/code><\/p>\n\n\n\n<p>\u5728\u8def\u7531\u914d\u7f6e\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u5b88\u536b\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>const routes: Routes = [ { path: 'secure', component: SecureComponent, canActivate: [AuthGuard] }, \/\/ other routes ];<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>AuthGuard<\/code>\u7684\u670d\u52a1\uff0c\u8be5\u670d\u52a1\u5b9e\u73b0\u4e86<code>CanActivate<\/code>\u63a5\u53e3\u3002\u5728<code>canActivate<\/code>\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u7528\u6237\u662f\u5426\u5df2\u7ecf\u901a\u8fc7<code>AuthService<\/code>\u8fdb\u884c\u4e86\u8eab\u4efd\u9a8c\u8bc1\u3002\u5982\u679c\u5df2\u7ecf\u8eab\u4efd\u9a8c\u8bc1\uff0c\u5219\u5141\u8bb8\u8def\u7531\uff0c\u5426\u5219\u91cd\u5b9a\u5411\u5230\u767b\u5f55\u9875\u9762\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cAngular\u63d0\u4f9b\u4e86\u591a\u5c42\u6b21\u3001\u7075\u6d3b\u7684\u8def\u7531\u5b89\u5168\u673a\u5236\uff0c\u5e2e\u52a9\u6211\u4eec\u66f4\u6709\u6548\u5730\u63a7\u5236\u7528\u6237\u5bf9\u5e94\u7528\u4e2d\u4e0d\u540c\u90e8\u5206\u7684\u8bbf\u95ee\u6743\u9650\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u6df1\u5165\u4e86\u89e3Angular\u5728\u5b89\u5168\u6027\u65b9\u9762\u7684\u5176\u4ed6\u7279\u6027\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840\u76842.3 \u8def\u7531\u5b89\u5168ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">2.4 Angular\u5b89\u5168\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728\u5f00\u53d1Angular\u5e94\u7528\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u5b89\u5168\u6700\u4f73\u5b9e\u8df5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u6709\u6548\u5730\u5229\u7528Angular\u7684\u5185\u7f6e\u5b89\u5168\u7279\u6027\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3a\u5e94\u7528\u7684\u6574\u4f53\u5b89\u5168\u6027\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u59cb\u7ec8\u4f7f\u7528HTTPS<\/strong>: \u5c3d\u91cf\u907f\u514d\u4f7f\u7528HTTP\uff0c\u56e0\u4e3aHTTPS\u53ef\u4ee5\u63d0\u4f9b\u989d\u5916\u7684\u5b89\u5168\u5c42\u3002<\/li>\n\n\n\n<li><strong>\u6700\u5c0f\u6743\u9650\u539f\u5219<\/strong>: \u4ec5\u7ed9\u4e88\u7528\u6237\uff08\u6216\u7cfb\u7edf\u7ec4\u4ef6\uff09\u5b8c\u6210\u4efb\u52a1\u6240\u5fc5\u9700\u7684\u6700\u5c0f\u6743\u9650\u3002<\/li>\n\n\n\n<li><strong>\u8f93\u5165\u9a8c\u8bc1\u548c\u8f93\u51fa\u7f16\u7801<\/strong>: \u5bf9\u6240\u6709\u7528\u6237\u8f93\u5165\u8fdb\u884c\u4e25\u683c\u7684\u9a8c\u8bc1\uff0c\u5e76\u5728\u5c06\u6570\u636e\u8f93\u51fa\u5230DOM\u4e4b\u524d\u8fdb\u884c\u9002\u5f53\u7684\u7f16\u7801\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528Content Security Policy (CSP)<\/strong>: CSP\u53ef\u4ee5\u5e2e\u52a9\u9632\u6b62\u591a\u79cd\u7c7b\u578b\u7684\u653b\u51fb\uff0c\u5305\u62ecXSS\u548c\u6570\u636e\u6ce8\u5165\u653b\u51fb\u3002<\/li>\n\n\n\n<li><strong>\u5b9a\u671f\u8fdb\u884c\u5b89\u5168\u5ba1\u8ba1<\/strong>: \u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\u548c\u624b\u52a8\u68c0\u67e5\u76f8\u7ed3\u5408\u7684\u65b9\u5f0f\uff0c\u5b9a\u671f\u5bf9\u4ee3\u7801\u548c\u4f9d\u8d56\u8fdb\u884c\u5b89\u5168\u5ba1\u8ba1\u3002<\/li>\n\n\n\n<li><strong>\u53ca\u65f6\u66f4\u65b0\u4f9d\u8d56\u548c\u6846\u67b6<\/strong>: \u4fdd\u6301Angular\u548c\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\u7684\u7248\u672c\u662f\u6700\u65b0\u7684\uff0c\u4ee5\u83b7\u5f97\u6700\u65b0\u7684\u5b89\u5168\u66f4\u65b0\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u867d\u7136\u8fd9\u4e00\u5c0f\u8282\u4e3b\u8981\u662f\u7406\u8bba\u6027\u7684\uff0c\u4f46\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Angular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Angular\u7684\u5185\u7f6e\u8868\u5355\u9a8c\u8bc1\u529f\u80fd\u6765\u5b9e\u73b0\u8f93\u5165\u9a8c\u8bc1\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Component } from '@angular\/core'; import { FormBuilder, FormGroup, Validators } from '@angular\/forms'; @Component({ selector: 'app-form-validation', template: ` &lt;form [formGroup]=\"form\"&gt; &lt;input formControlName=\"username\" placeholder=\"Username\"&gt; &lt;input formControlName=\"password\" type=\"password\" placeholder=\"Password\"&gt; &lt;button type=\"submit\" [disabled]=\"form.invalid\"&gt;Submit&lt;\/button&gt; &lt;\/form&gt; ` }) export class FormValidationComponent { form: FormGroup; constructor(private fb: FormBuilder) { this.form = this.fb.group({ username: ['', [Validators.required, Validators.minLength(3)]], password: ['', [Validators.required, Validators.minLength(8)]] }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Angular\u7684<code>FormBuilder<\/code>\u548c<code>Validators<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u8868\u5355\uff0c\u5e76\u6dfb\u52a0\u4e86\u5fc5\u586b\uff08required\uff09\u548c\u6700\u5c0f\u957f\u5ea6\uff08minLength\uff09\u7684\u9a8c\u8bc1\u89c4\u5219\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3aAngular\u5e94\u7528\u7684\u5b89\u5168\u6027\uff0c\u4ece\u800c\u66f4\u597d\u5730\u4fdd\u62a4\u7528\u6237\u6570\u636e\u548c\u7cfb\u7edf\u8d44\u6e90\u3002\u8fd9\u4e5f\u662f\u672c\u7ae0\u7684\u6700\u540e\u4e00\u4e2a\u5c0f\u8282\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8fdb\u5165\u5230\u66f4\u4e13\u95e8\u7684\u8ba4\u8bc1\u673a\u5236\u548c\u6280\u672f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840\u76842.4 Angular\u5b89\u5168\u6700\u4f73\u5b9e\u8df5ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e8c\u7ae0\uff1aAngular\u5b89\u5168\u57fa\u7840<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">2.4 Angular\u5b89\u5168\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728\u5f00\u53d1Angular\u5e94\u7528\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u5b89\u5168\u6700\u4f73\u5b9e\u8df5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u6709\u6548\u5730\u5229\u7528Angular\u7684\u5185\u7f6e\u5b89\u5168\u7279\u6027\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3a\u5e94\u7528\u7684\u6574\u4f53\u5b89\u5168\u6027\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u59cb\u7ec8\u4f7f\u7528HTTPS<\/strong>: \u5c3d\u91cf\u907f\u514d\u4f7f\u7528HTTP\uff0c\u56e0\u4e3aHTTPS\u53ef\u4ee5\u63d0\u4f9b\u989d\u5916\u7684\u5b89\u5168\u5c42\u3002<\/li>\n\n\n\n<li><strong>\u6700\u5c0f\u6743\u9650\u539f\u5219<\/strong>: \u4ec5\u7ed9\u4e88\u7528\u6237\uff08\u6216\u7cfb\u7edf\u7ec4\u4ef6\uff09\u5b8c\u6210\u4efb\u52a1\u6240\u5fc5\u9700\u7684\u6700\u5c0f\u6743\u9650\u3002<\/li>\n\n\n\n<li><strong>\u8f93\u5165\u9a8c\u8bc1\u548c\u8f93\u51fa\u7f16\u7801<\/strong>: \u5bf9\u6240\u6709\u7528\u6237\u8f93\u5165\u8fdb\u884c\u4e25\u683c\u7684\u9a8c\u8bc1\uff0c\u5e76\u5728\u5c06\u6570\u636e\u8f93\u51fa\u5230DOM\u4e4b\u524d\u8fdb\u884c\u9002\u5f53\u7684\u7f16\u7801\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528Content Security Policy (CSP)<\/strong>: CSP\u53ef\u4ee5\u5e2e\u52a9\u9632\u6b62\u591a\u79cd\u7c7b\u578b\u7684\u653b\u51fb\uff0c\u5305\u62ecXSS\u548c\u6570\u636e\u6ce8\u5165\u653b\u51fb\u3002<\/li>\n\n\n\n<li><strong>\u5b9a\u671f\u8fdb\u884c\u5b89\u5168\u5ba1\u8ba1<\/strong>: \u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\u548c\u624b\u52a8\u68c0\u67e5\u76f8\u7ed3\u5408\u7684\u65b9\u5f0f\uff0c\u5b9a\u671f\u5bf9\u4ee3\u7801\u548c\u4f9d\u8d56\u8fdb\u884c\u5b89\u5168\u5ba1\u8ba1\u3002<\/li>\n\n\n\n<li><strong>\u53ca\u65f6\u66f4\u65b0\u4f9d\u8d56\u548c\u6846\u67b6<\/strong>: \u4fdd\u6301Angular\u548c\u5176\u4ed6\u7b2c\u4e09\u65b9\u5e93\u7684\u7248\u672c\u662f\u6700\u65b0\u7684\uff0c\u4ee5\u83b7\u5f97\u6700\u65b0\u7684\u5b89\u5168\u66f4\u65b0\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u867d\u7136\u8fd9\u4e00\u5c0f\u8282\u4e3b\u8981\u662f\u7406\u8bba\u6027\u7684\uff0c\u4f46\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Angular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Angular\u7684\u5185\u7f6e\u8868\u5355\u9a8c\u8bc1\u529f\u80fd\u6765\u5b9e\u73b0\u8f93\u5165\u9a8c\u8bc1\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Component } from '@angular\/core'; import { FormBuilder, FormGroup, Validators } from '@angular\/forms'; @Component({ selector: 'app-form-validation', template: ` &lt;form [formGroup]=\"form\"&gt; &lt;input formControlName=\"username\" placeholder=\"Username\"&gt; &lt;input formControlName=\"password\" type=\"password\" placeholder=\"Password\"&gt; &lt;button type=\"submit\" [disabled]=\"form.invalid\"&gt;Submit&lt;\/button&gt; &lt;\/form&gt; ` }) export class FormValidationComponent { form: FormGroup; constructor(private fb: FormBuilder) { this.form = this.fb.group({ username: ['', [Validators.required, Validators.minLength(3)]], password: ['', [Validators.required, Validators.minLength(8)]] }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Angular\u7684<code>FormBuilder<\/code>\u548c<code>Validators<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u8868\u5355\uff0c\u5e76\u6dfb\u52a0\u4e86\u5fc5\u586b\uff08required\uff09\u548c\u6700\u5c0f\u957f\u5ea6\uff08minLength\uff09\u7684\u9a8c\u8bc1\u89c4\u5219\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3aAngular\u5e94\u7528\u7684\u5b89\u5168\u6027\uff0c\u4ece\u800c\u66f4\u597d\u5730\u4fdd\u62a4\u7528\u6237\u6570\u636e\u548c\u7cfb\u7edf\u8d44\u6e90\u3002\u8fd9\u4e5f\u662f\u672c\u7ae0\u7684\u6700\u540e\u4e00\u4e2a\u5c0f\u8282\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8fdb\u5165\u5230\u66f4\u4e13\u95e8\u7684\u8ba4\u8bc1\u673a\u5236\u548c\u6280\u672f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e09\u7ae0\uff1a\u8ba4\u8bc1\u57fa\u7840\u76843.1 \u4ec0\u4e48\u662f\u8ba4\u8bc1ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e09\u7ae0\uff1a\u8ba4\u8bc1\u57fa\u7840<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">3.1 \u4ec0\u4e48\u662f\u8ba4\u8bc1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u8ba4\u8bc1\uff08Authentication\uff09\u662f\u4fe1\u606f\u5b89\u5168\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\uff0c\u5b83\u662f\u7528\u4e8e\u786e\u8ba4\u4e00\u4e2a\u5b9e\u4f53\uff08\u901a\u5e38\u662f\u7528\u6237\uff09\u7684\u8eab\u4efd\u7684\u8fc7\u7a0b\u3002\u7b80\u5355\u5730\u8bf4\uff0c\u8ba4\u8bc1\u5c31\u662f\u786e\u4fdd\u7528\u6237\u662f\u4ed6\u4eec\u58f0\u79f0\u7684\u90a3\u4e2a\u4eba\u3002\u5728Web\u5e94\u7528\u4e2d\uff0c\u8ba4\u8bc1\u901a\u5e38\u662f\u901a\u8fc7\u7528\u6237\u540d\u548c\u5bc6\u7801\u3001\u7535\u5b50\u90ae\u4ef6\u548c\u9a8c\u8bc1\u7801\u3001\u6216\u8005\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\uff08\u5982\u591a\u56e0\u7d20\u8ba4\u8bc1\uff09\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n\n\n\n<p>\u8ba4\u8bc1\u662f\u5b89\u5168\u4f53\u7cfb\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u73af\uff0c\u56e0\u4e3a\u53ea\u6709\u901a\u8fc7\u4e86\u8ba4\u8bc1\u7684\u7528\u6237\u624d\u80fd\u83b7\u5f97\u5bf9\u7279\u5b9a\u8d44\u6e90\u6216\u64cd\u4f5c\u7684\u8bbf\u95ee\u6743\u9650\u3002\u6ca1\u6709\u6709\u6548\u7684\u8ba4\u8bc1\u673a\u5236\uff0c\u5e94\u7528\u5c31\u5f88\u5bb9\u6613\u53d7\u5230\u672a\u7ecf\u6388\u6743\u8bbf\u95ee\u7684\u653b\u51fb\u3002<\/p>\n\n\n\n<p>\u5728Angular\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\u8ba4\u8bc1\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u57fa\u7840\u8ba4\u8bc1\uff08Basic Authentication\uff09<\/strong>: \u6700\u7b80\u5355\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u8ba4\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u4ee4\u724c\u8ba4\u8bc1\uff08Token-based Authentication\uff09<\/strong>: \u4f7f\u7528\u5982JWT\uff08JSON Web Token\uff09\u8fd9\u6837\u7684\u4ee4\u724c\u8fdb\u884c\u8ba4\u8bc1\u3002<\/li>\n\n\n\n<li><strong>OAuth\u548cOpenID Connect<\/strong>: \u4f7f\u7528\u7b2c\u4e09\u65b9\u670d\u52a1\u8fdb\u884c\u8ba4\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u591a\u56e0\u7d20\u8ba4\u8bc1\uff08Multi-Factor Authentication, MFA\uff09<\/strong>: \u901a\u8fc7\u591a\u4e2a\u8ba4\u8bc1\u6b65\u9aa4\u589e\u52a0\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Angular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u57fa\u7840\u8ba4\u8bc1\u6765\u8fdb\u884c\u767b\u5f55\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class AuthService { constructor(private http: HttpClient) {} login(username: string, password: string) { const headers = new HttpHeaders({ 'Authorization': 'Basic ' + btoa(username + ':' + password) }); return this.http.get('\/api\/login', { headers }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Angular\u7684<code>HttpClient<\/code>\u6a21\u5757\u6765\u53d1\u9001\u4e00\u4e2aHTTP GET\u8bf7\u6c42\u3002\u6211\u4eec\u901a\u8fc7HTTP\u5934\u4e2d\u7684<code>Authorization<\/code>\u5b57\u6bb5\u4f20\u9012\u7ecf\u8fc7Base64\u7f16\u7801\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u57fa\u7840\u8ba4\u8bc1\u3002\u5f53\u7136\uff0c\u8fd9\u53ea\u662f\u8ba4\u8bc1\u7684\u4f17\u591a\u65b9\u6cd5\u4e4b\u4e00\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u66f4\u591a\u9ad8\u7ea7\u548c\u5b89\u5168\u7684\u8ba4\u8bc1\u65b9\u6cd5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e09\u7ae0\uff1a\u8ba4\u8bc1\u57fa\u7840\u76843.2 \u8ba4\u8bc1\u7684\u79cd\u7c7bChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e09\u7ae0\uff1a\u8ba4\u8bc1\u57fa\u7840<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">3.2 \u8ba4\u8bc1\u7684\u79cd\u7c7b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728Web\u5e94\u7528\u548c\u670d\u52a1\u4e2d\uff0c\u6709\u591a\u79cd\u4e0d\u540c\u7c7b\u578b\u7684\u8ba4\u8bc1\u673a\u5236\uff0c\u6bcf\u79cd\u90fd\u6709\u5176\u7279\u5b9a\u7684\u7528\u9014\u548c\u5b89\u5168\u7ea7\u522b\u3002\u4e86\u89e3\u8fd9\u4e9b\u4e0d\u540c\u7c7b\u578b\u7684\u8ba4\u8bc1\u673a\u5236\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684Angular\u5e94\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u57fa\u7840\u8ba4\u8bc1\uff08Basic Authentication\uff09<\/strong>: \u8fd9\u662f\u6700\u7b80\u5355\u7684\u8ba4\u8bc1\u5f62\u5f0f\uff0c\u901a\u5e38\u53ea\u9700\u8981\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/li>\n\n\n\n<li><strong>\u6458\u8981\u8ba4\u8bc1\uff08Digest Authentication\uff09<\/strong>: \u76f8\u6bd4\u57fa\u7840\u8ba4\u8bc1\uff0c\u6458\u8981\u8ba4\u8bc1\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u5b89\u5168\u6027\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86\u4e00\u79cd\u6311\u6218-\u54cd\u5e94\u673a\u5236\u3002<\/li>\n\n\n\n<li><strong>\u4ee4\u724c\u8ba4\u8bc1\uff08Token-based Authentication\uff09<\/strong>: \u5982JWT\uff08JSON Web Tokens\uff09\uff0c\u8fd9\u79cd\u8ba4\u8bc1\u65b9\u5f0f\u66f4\u9002\u7528\u4e8e\u5206\u5e03\u5f0f\u6216\u5fae\u670d\u52a1\u67b6\u6784\u3002<\/li>\n\n\n\n<li><strong>OAuth 2.0\u548cOpenID Connect<\/strong>: \u8fd9\u4e9b\u662f\u5f00\u653e\u6807\u51c6\uff0c\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u7b2c\u4e09\u65b9\u8d26\u6237\uff08\u5982Google\u6216Facebook\uff09\u8fdb\u884c\u8ba4\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u591a\u56e0\u7d20\u8ba4\u8bc1\uff08Multi-Factor Authentication, MFA\uff09<\/strong>: \u8fd9\u662f\u4e00\u79cd\u9ad8\u7ea7\u8ba4\u8bc1\u65b9\u5f0f\uff0c\u9700\u8981\u4e24\u4e2a\u6216\u66f4\u591a\u7684\u9a8c\u8bc1\u56e0\u7d20\u3002<\/li>\n\n\n\n<li><strong>API\u5bc6\u94a5\uff08API Keys\uff09<\/strong>: \u901a\u5e38\u7528\u4e8e\u670d\u52a1\u5230\u670d\u52a1\uff08Server-to-Server\uff09\u7684\u8ba4\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u5ba2\u6237\u7aef\u8bc1\u4e66\uff08Client Certificates\uff09<\/strong>: \u8fd9\u662f\u4e00\u79cd\u57fa\u4e8eX.509\u6807\u51c6\u7684\u8ba4\u8bc1\u65b9\u5f0f\uff0c\u901a\u5e38\u7528\u4e8e\u9ad8\u5b89\u5168\u6027\u8981\u6c42\u7684\u73af\u5883\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528JWT\u4ee4\u724c\u8fdb\u884c\u8ba4\u8bc1\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class AuthService { constructor(private http: HttpClient) {} loginWithToken(jwtToken: string) { const headers = new HttpHeaders({ 'Authorization': 'Bearer ' + jwtToken }); return this.http.get('\/api\/secure', { headers }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>HttpClient<\/code>\u6a21\u5757\u6765\u53d1\u9001\u4e00\u4e2aHTTP GET\u8bf7\u6c42\u5230\u4e00\u4e2a\u9700\u8981\u8ba4\u8bc1\u7684API\u7aef\u70b9\u3002\u6211\u4eec\u901a\u8fc7HTTP\u5934\u4e2d\u7684<code>Authorization<\/code>\u5b57\u6bb5\u4f20\u9012\u4e86\u4e00\u4e2aJWT\u4ee4\u724c\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u4e86\u89e3\u4e86\u8ba4\u8bc1\u7684\u591a\u79cd\u7c7b\u578b\u548c\u5b83\u4eec\u5728Angular\u5e94\u7528\u4e2d\u7684\u5e94\u7528\u65b9\u5f0f\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u63a2\u8ba8\u8fd9\u4e9b\u8ba4\u8bc1\u673a\u5236\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u5b83\u4eec\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e09\u7ae0\uff1a\u8ba4\u8bc1\u57fa\u7840\u76843.3 \u5982\u4f55\u5728Angular\u4e2d\u5b9e\u73b0\u8ba4\u8bc1ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e09\u7ae0\uff1a\u8ba4\u8bc1\u57fa\u7840<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">3.3 \u5982\u4f55\u5728Angular\u4e2d\u5b9e\u73b0\u8ba4\u8bc1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u8ba4\u8bc1\u901a\u5e38\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\u548c\u7ec4\u4ef6\uff0c\u5305\u62ec\u524d\u7aefUI\u3001\u540e\u7aefAPI\u3001\u4ee5\u53ca\u53ef\u80fd\u7684\u7b2c\u4e09\u65b9\u670d\u52a1\u3002\u4e0b\u9762\u662f\u5728Angular\u4e2d\u5b9e\u73b0\u8ba4\u8bc1\u7684\u4e00\u822c\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u754c\u9762\uff08User Interface\uff09<\/strong>: \u521b\u5efa\u4e00\u4e2a\u767b\u5f55\u8868\u5355\uff0c\u7528\u4e8e\u6536\u96c6\u7528\u6237\u7684\u8ba4\u8bc1\u4fe1\u606f\uff08\u5982\u7528\u6237\u540d\u548c\u5bc6\u7801\uff09\u3002<\/li>\n\n\n\n<li><strong>\u8ba4\u8bc1\u670d\u52a1\uff08Authentication Service\uff09<\/strong>: \u521b\u5efa\u4e00\u4e2aAngular\u670d\u52a1\uff0c\u7528\u4e8e\u5904\u7406\u4e0e\u540e\u7aefAPI\u7684\u8ba4\u8bc1\u4ea4\u4e92\u3002<\/li>\n\n\n\n<li><strong>\u8def\u7531\u5b88\u536b\uff08Route Guards\uff09<\/strong>: \u4f7f\u7528Angular\u7684\u8def\u7531\u5b88\u536b\u6765\u4fdd\u62a4\u9700\u8981\u8ba4\u8bc1\u7684\u8def\u7531\u3002<\/li>\n\n\n\n<li><strong>\u72b6\u6001\u7ba1\u7406\uff08State Management\uff09<\/strong>: \u4f7f\u7528Angular\u670d\u52a1\u6216\u72b6\u6001\u7ba1\u7406\u5e93\uff08\u5982NgRx\uff09\u6765\u5b58\u50a8\u8ba4\u8bc1\u72b6\u6001\u548c\u7528\u6237\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>HTTP\u62e6\u622a\u5668\uff08HTTP Interceptors\uff09<\/strong>: \u4f7f\u7528Angular\u7684HTTP\u62e6\u622a\u5668\u6765\u81ea\u52a8\u6dfb\u52a0\u8ba4\u8bc1\u4fe1\u606f\u5230\u6bcf\u4e2aHTTP\u8bf7\u6c42\u3002<\/li>\n\n\n\n<li><strong>\u540e\u7aef\u9a8c\u8bc1\uff08Backend Validation\uff09<\/strong>: \u786e\u4fdd\u540e\u7aefAPI\u4e5f\u5b9e\u73b0\u4e86\u76f8\u5e94\u7684\u8ba4\u8bc1\u548c\u6388\u6743\u673a\u5236\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Angular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u5904\u7406\u8ba4\u8bc1\u7684\u670d\u52a1\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Injectable } from '@angular\/core'; import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { BehaviorSubject } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class AuthService { private isAuthenticatedSubject = new BehaviorSubject&lt;boolean&gt;(false); public isAuthenticated = this.isAuthenticatedSubject.asObservable(); constructor(private http: HttpClient) {} login(username: string, password: string) { const headers = new HttpHeaders({ 'Content-Type': 'application\/json' }); return this.http.post('\/api\/login', { username, password }, { headers }) .subscribe(response =&gt; { \/\/ Store authentication token and update authentication state localStorage.setItem('authToken', response['token']); this.isAuthenticatedSubject.next(true); }); } logout() { localStorage.removeItem('authToken'); this.isAuthenticatedSubject.next(false); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>AuthService<\/code>\u7684Angular\u670d\u52a1\u3002\u8fd9\u4e2a\u670d\u52a1\u6709\u4e00\u4e2a<code>BehaviorSubject<\/code>\u6765\u8ddf\u8e2a\u7528\u6237\u7684\u8ba4\u8bc1\u72b6\u6001\uff0c\u5e76\u63d0\u4f9b\u4e86<code>login<\/code>\u548c<code>logout<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u7528\u6237\u767b\u5f55\u548c\u6ce8\u9500\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u6837\u7684\u5b9e\u73b0\uff0c\u4f60\u53ef\u4ee5\u5728Angular\u5e94\u7528\u4e2d\u521b\u5efa\u4e00\u4e2a\u5b8c\u6574\u7684\u8ba4\u8bc1\u6d41\u7a0b\uff0c\u4ece\u7528\u6237\u754c\u9762\u5230\u540e\u7aef\u9a8c\u8bc1\uff0c\u6240\u6709\u7684\u6b65\u9aa4\u90fd\u53ef\u4ee5\u7528Angular\u7684\u5404\u79cd\u7279\u6027\u548c\u5de5\u5177\u6765\u5b9e\u73b0\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u8be6\u7ec6\u5730\u63a2\u8ba8\u5404\u79cd\u8ba4\u8bc1\u673a\u5236\u548c\u5b83\u4eec\u7684\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u56db\u7ae0\uff1a\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1\u76844.1 \u4ec0\u4e48\u662fJWTChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u56db\u7ae0\uff1a\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">4.1 \u4ec0\u4e48\u662fJWT<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>JWT\uff08JSON Web Token\uff09\u662f\u4e00\u79cd\u5f00\u653e\u6807\u51c6\uff08RFC 7519\uff09\uff0c\u7528\u4e8e\u5728\u4e24\u4e2a\u7cfb\u7edf\u4e4b\u95f4\u5b89\u5168\u5730\u4f20\u8f93\u4fe1\u606f\u3002JWT\u901a\u5e38\u7528\u4e8e\u8eab\u4efd\u9a8c\u8bc1\u548c\u4fe1\u606f\u4ea4\u6362\u3002\u4e00\u4e2aJWT\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u7b80\u77ed\u7684\u3001\u81ea\u5305\u542b\u7684\u63cf\u8ff0\u4fe1\u606f\uff0c\u5b83\u5305\u542b\u4e86\u58f0\u660e\uff08Claims\uff09\uff0c\u8fd9\u4e9b\u58f0\u660e\u901a\u5e38\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u7528\u6237\u7684\u8eab\u4efd\u548c\u6743\u9650\u3002<\/p>\n\n\n\n<p>JWT\u7531\u4e09\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5934\u90e8\uff08Header\uff09<\/strong>: \u5305\u542b\u4e86\u4ee4\u724c\u7684\u5143\u6570\u636e\uff0c\u5982\u7b7e\u540d\u7b97\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u8d1f\u8f7d\uff08Payload\uff09<\/strong>: \u5305\u542b\u4e86\u6240\u8c13\u7684\u201c\u58f0\u660e\u201d\uff0c\u5373\u5b58\u50a8\u5728\u4ee4\u724c\u4e2d\u7684\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u7b7e\u540d\uff08Signature\uff09<\/strong>: \u7528\u4e8e\u9a8c\u8bc1\u6d88\u606f\u7684\u5b8c\u6574\u6027\u548c\u6765\u6e90\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u4e09\u90e8\u5206\u901a\u8fc7\u70b9\uff08.\uff09\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9\u548c\u7528\u9014<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u81ea\u5305\u542b<\/strong>: JWT\u53ef\u4ee5\u5305\u542b\u6240\u6709\u7528\u6237\u9700\u8981\u7684\u4fe1\u606f\uff0c\u907f\u514d\u4e86\u591a\u6b21\u67e5\u8be2\u6570\u636e\u5e93\u3002<\/li>\n\n\n\n<li><strong>\u8f7b\u91cf\u7ea7<\/strong>: \u7531\u4e8e\u5176\u7ed3\u6784\u7b80\u5355\uff0cJWT\u975e\u5e38\u9002\u5408\u7528\u4e8eHTTP\u5934\u6216URL\u53c2\u6570\u3002<\/li>\n\n\n\n<li><strong>\u8de8\u57df<\/strong>: JWT\u662f\u81ea\u5305\u542b\u7684\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u8de8\u57df\u8eab\u4efd\u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Angular\u548cJWT\u8fdb\u884c\u8ba4\u8bc1\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class AuthService { constructor(private http: HttpClient) {} login(username: string, password: string) { return this.http.post('\/api\/login', { username, password }) .subscribe(response =&gt; { const jwtToken = response['token']; localStorage.setItem('jwtToken', jwtToken); }); } fetchSecureData() { const jwtToken = localStorage.getItem('jwtToken'); const headers = new HttpHeaders({ 'Authorization': 'Bearer ' + jwtToken }); return this.http.get('\/api\/secure', { headers }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7<code>\/api\/login<\/code>\u7aef\u70b9\u8fdb\u884c\u767b\u5f55\uff0c\u5e76\u4ece\u54cd\u5e94\u4e2d\u83b7\u53d6JWT\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2aJWT\u5b58\u50a8\u5728\u672c\u5730\u5b58\u50a8\uff08LocalStorage\uff09\u4e2d\u3002\u5f53\u6211\u4eec\u9700\u8981\u8bbf\u95ee\u53d7\u4fdd\u62a4\u7684\u8d44\u6e90\u65f6\uff0c\u6211\u4eec\u4ece\u672c\u5730\u5b58\u50a8\u4e2d\u53d6\u51faJWT\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230HTTP\u8bf7\u6c42\u7684<code>Authorization<\/code>\u5934\u4e2d\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cJWT\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\u6765\u5904\u7406Web\u5e94\u7528\u4e2d\u7684\u8ba4\u8bc1\u548c\u6388\u6743\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u5b89\u5168\u5730\u4f7f\u7528\u548c\u7ba1\u7406JWT\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u56db\u7ae0\uff1a\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1\u76844.2 JWT\u7684\u5de5\u4f5c\u539f\u7406ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u56db\u7ae0\uff1a\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">4.2 JWT\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>JWT\uff08JSON Web Token\uff09\u7684\u5de5\u4f5c\u539f\u7406\u76f8\u5bf9\u76f4\u63a5\u548c\u7b80\u5355\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u9ad8\u5ea6\u53ef\u914d\u7f6e\u548c\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8ba4\u8bc1\u548c\u6388\u6743\u3002\u4ee5\u4e0b\u662fJWT\u5728\u4e00\u4e2a\u5178\u578b\u7684Web\u5e94\u7528\u4e2d\u7684\u5de5\u4f5c\u6d41\u7a0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u767b\u5f55<\/strong>: \u7528\u6237\u901a\u8fc7\u63d0\u4f9b\u7528\u6237\u540d\u548c\u5bc6\u7801\u6216\u5176\u4ed6\u8ba4\u8bc1\u4fe1\u606f\u6765\u767b\u5f55\u5e94\u7528\u3002<\/li>\n\n\n\n<li><strong>\u751f\u6210JWT<\/strong>: \u4e00\u65e6\u7528\u6237\u8eab\u4efd\u5f97\u5230\u9a8c\u8bc1\uff0c\u670d\u52a1\u5668\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b\u7528\u6237\u4fe1\u606f\uff08\u5982ID\u3001\u89d2\u8272\u7b49\uff09\u548c\u4e00\u4e9b\u5143\u6570\u636e\u7684JWT\u3002<\/li>\n\n\n\n<li><strong>\u8fd4\u56deJWT<\/strong>: \u751f\u6210\u7684JWT\u4f1a\u4f5c\u4e3a\u54cd\u5e94\u7684\u4e00\u90e8\u5206\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u3002<\/li>\n\n\n\n<li><strong>\u5b58\u50a8JWT<\/strong>: \u5ba2\u6237\u7aef\uff08\u901a\u5e38\u662f\u524d\u7aef\u5e94\u7528\uff09\u4f1a\u5b89\u5168\u5730\u5b58\u50a8\u8be5JWT\uff0c\u5e38\u7528\u7684\u5b58\u50a8\u65b9\u5f0f\u6709LocalStorage\u3001SessionStorage\u6216Cookie\u3002<\/li>\n\n\n\n<li><strong>\u9644\u52a0JWT\u5230\u8bf7\u6c42<\/strong>: \u5f53\u9700\u8981\u8bbf\u95ee\u53d7\u4fdd\u62a4\u7684\u8d44\u6e90\u6216API\u65f6\uff0c\u5ba2\u6237\u7aef\u4f1a\u5c06\u5b58\u50a8\u7684JWT\u9644\u52a0\u5230HTTP\u8bf7\u6c42\u7684<code>Authorization<\/code>\u5934\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u670d\u52a1\u5668\u9a8c\u8bc1JWT<\/strong>: \u6536\u5230\u8bf7\u6c42\u540e\uff0c\u670d\u52a1\u5668\u4f1a\u9a8c\u8bc1JWT\u7684\u6709\u6548\u6027\u548c\u5b8c\u6574\u6027\u3002\u5982\u679c\u9a8c\u8bc1\u6210\u529f\uff0c\u670d\u52a1\u5668\u4f1a\u63d0\u4f9b\u8bf7\u6c42\u7684\u8d44\u6e90\u6216\u670d\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u9009\u7684JWT\u5237\u65b0<\/strong>: \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cJWT\u53ef\u80fd\u4f1a\u8fc7\u671f\uff0c\u9700\u8981\u5237\u65b0\u3002\u8fd9\u901a\u5e38\u901a\u8fc7\u4e00\u4e2a\u5355\u72ec\u7684\u5237\u65b0\u4ee4\u724c\uff08Refresh Token\uff09\u6765\u5b9e\u73b0\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Angular\u670d\u52a1\u4e2d\u6dfb\u52a0JWT\u5230HTTP\u8bf7\u6c42\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class DataService { constructor(private http: HttpClient) {} fetchProtectedData() { const jwtToken = localStorage.getItem('jwtToken'); const headers = new HttpHeaders({ 'Authorization': 'Bearer ' + jwtToken }); return this.http.get('\/api\/protected', { headers }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4eceLocalStorage\u4e2d\u83b7\u53d6JWT\uff0c\u5e76\u901a\u8fc7<code>HttpHeaders<\/code>\u5c06\u5176\u6dfb\u52a0\u5230HTTP GET\u8bf7\u6c42\u7684<code>Authorization<\/code>\u5934\u4e2d\u3002\u8fd9\u6837\uff0c\u5f53\u8bf7\u6c42\u5230\u8fbe\u670d\u52a1\u5668\u65f6\uff0c\u670d\u52a1\u5668\u5c31\u53ef\u4ee5\u89e3\u6790\u548c\u9a8c\u8bc1\u8fd9\u4e2aJWT\uff0c\u4ee5\u786e\u5b9a\u7528\u6237\u662f\u5426\u6709\u6743\u8bbf\u95ee\u8bf7\u6c42\u7684\u8d44\u6e90\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cJWT\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u4e00\u79cd\u5b89\u5168\u548c\u9ad8\u6548\u7684\u8ba4\u8bc1\u673a\u5236\uff0c\u8fd8\u5141\u8bb8\u6211\u4eec\u5728\u65e0\u72b6\u6001\u7684HTTP\u534f\u8bae\u4e0a\u5b9e\u73b0\u72b6\u6001\u7ba1\u7406\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u8ba8\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u5b89\u5168\u5730\u4f7f\u7528JWT\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u56db\u7ae0\uff1a\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1\u76844.3 \u5728Angular\u4e2d\u4f7f\u7528JWTChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u56db\u7ae0\uff1a\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">4.3 \u5728Angular\u4e2d\u4f7f\u7528JWT<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728Angular\u5e94\u7528\u4e2d\u4f7f\u7528JWT\uff08JSON Web Token\uff09\u8fdb\u884c\u8ba4\u8bc1\u548c\u6388\u6743\u6d89\u53ca\u5230\u591a\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u7528\u6237\u754c\u9762\u3001HTTP\u8bf7\u6c42\u548c\u72b6\u6001\u7ba1\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u6b65\u9aa4\u548c\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u767b\u5f55\u548cJWT\u83b7\u53d6<\/strong>: \u521b\u5efa\u4e00\u4e2a\u767b\u5f55\u8868\u5355\uff0c\u5e76\u5728\u7528\u6237\u6210\u529f\u767b\u5f55\u540e\u4ece\u670d\u52a1\u5668\u83b7\u53d6JWT\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u5b58\u50a8<\/strong>: \u4e00\u65e6\u83b7\u53d6\u4e86JWT\uff0c\u9700\u8981\u5c06\u5176\u5b89\u5168\u5730\u5b58\u50a8\u5728\u5ba2\u6237\u7aef\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528LocalStorage\u6216SessionStorage\uff0c\u4f46\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u80fd\u6709\u5b89\u5168\u9690\u60a3\u3002\u66f4\u5b89\u5168\u7684\u505a\u6cd5\u662f\u4f7f\u7528HttpOnly\u7684Cookie\u3002<\/li>\n\n\n\n<li><strong>\u9644\u52a0JWT\u5230\u8bf7\u6c42<\/strong>: \u4f7f\u7528Angular\u7684HTTP\u62e6\u622a\u5668\uff08Interceptor\uff09\u6765\u81ea\u52a8\u5c06JWT\u6dfb\u52a0\u5230\u6240\u6709\u51fa\u7ad9HTTP\u8bf7\u6c42\u7684<code>Authorization<\/code>\u5934\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u9a8c\u8bc1\u548c\u9519\u8bef\u5904\u7406<\/strong>: \u5728\u5e94\u7528\u4e2d\u6dfb\u52a0\u903b\u8f91\u6765\u5904\u7406\u56e0JWT\u8fc7\u671f\u6216\u65e0\u6548\u800c\u5bfc\u81f4\u7684\u8ba4\u8bc1\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u6ce8\u9500\u548cJWT\u5e9f\u5f03<\/strong>: \u5728\u7528\u6237\u6ce8\u9500\u65f6\uff0c\u786e\u4fdd\u4ece\u5ba2\u6237\u7aef\u5b58\u50a8\u4e2d\u79fb\u9664JWT\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5e9f\u5f03\u670d\u52a1\u5668\u4e0a\u7684JWT\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528HTTP\u62e6\u622a\u5668\u6765\u81ea\u52a8\u6dfb\u52a0JWT\u5230HTTP\u8bf7\u6c42\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Injectable } from '@angular\/core'; import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular\/common\/http'; import { Observable } from 'rxjs'; @Injectable() export class JwtInterceptor implements HttpInterceptor { intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; { const jwtToken = localStorage.getItem('jwtToken'); if (jwtToken) { request = request.clone({ setHeaders: { Authorization: `Bearer ${jwtToken}` } }); } return next.handle(request); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>JwtInterceptor<\/code>\u7684Angular HTTP\u62e6\u622a\u5668\u3002\u8be5\u62e6\u622a\u5668\u4f1a\u68c0\u67e5LocalStorage\u4e2d\u662f\u5426\u5b58\u5728JWT\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u4f1a\u5c06\u5176\u6dfb\u52a0\u5230\u6240\u6709\u51fa\u7ad9HTTP\u8bf7\u6c42\u7684<code>Authorization<\/code>\u5934\u4e2d\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u9700\u8981\u8ba4\u8bc1\u7684HTTP\u8bf7\u6c42\u90fd\u4f1a\u81ea\u52a8\u5305\u542bJWT\uff0c\u4ece\u800c\u7b80\u5316\u4e86\u5e94\u7528\u4e2d\u7684\u8ba4\u8bc1\u548c\u6388\u6743\u903b\u8f91\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u66f4\u591a\u5173\u4e8e\u5728Angular\u5e94\u7528\u4e2d\u5b89\u5168\u548c\u6709\u6548\u5730\u4f7f\u7528JWT\u7684\u7ec6\u8282\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u56db\u7ae0\uff1a\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1\u76844.4 JWT\u5b89\u5168\u6027\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u56db\u7ae0\uff1a\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">4.4 JWT\u5b89\u5168\u6027\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u867d\u7136JWT\uff08JSON Web Token\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u548c\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8ba4\u8bc1\u548c\u6388\u6743\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u6f5c\u5728\u7684\u5b89\u5168\u95ee\u9898\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b58\u50a8\u5b89\u5168\u6027<\/strong>: JWT\u901a\u5e38\u5b58\u50a8\u5728LocalStorage\u6216SessionStorage\u4e2d\uff0c\u8fd9\u4e9b\u5730\u65b9\u5bb9\u6613\u53d7\u5230XSS\uff08\u8de8\u7ad9\u811a\u672c\uff09\u653b\u51fb\u3002<\/li>\n\n\n\n<li><strong>\u4ee4\u724c\u6cc4\u9732<\/strong>: \u5982\u679cJWT\u6cc4\u9732\uff0c\u653b\u51fb\u8005\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8bbf\u95ee\u53d7\u4fdd\u62a4\u7684\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u4ee4\u724c\u8fc7\u671f<\/strong>: JWT\u4e00\u65e6\u751f\u6210\uff0c\u5c31\u4f1a\u4e00\u76f4\u6709\u6548\uff0c\u9664\u975e\u5b83\u6709\u4e00\u4e2a\u8fc7\u671f\u65f6\u95f4\u6216\u88ab\u670d\u52a1\u5668\u5e9f\u5f03\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u52a0\u5bc6\u7684\u8d1f\u8f7d<\/strong>: JWT\u7684\u8d1f\u8f7d\uff08Payload\uff09\u662fBase64\u7f16\u7801\u7684\uff0c\u800c\u4e0d\u662f\u52a0\u5bc6\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u89e3\u7801\u5b83\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b89\u5168\u5b58\u50a8<\/strong>: \u4f7f\u7528HttpOnly\u548cSecure\u6807\u5fd7\u7684Cookie\u6765\u5b58\u50a8JWT\uff0c\u4ee5\u51cf\u5c11XSS\u548cCSRF\uff08\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff09\u653b\u51fb\u7684\u98ce\u9669\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528HTTPS<\/strong>: \u603b\u662f\u4f7f\u7528HTTPS\u6765\u4f20\u8f93JWT\uff0c\u4ee5\u9632\u6b62\u4e2d\u95f4\u4eba\u653b\u51fb\u3002<\/li>\n\n\n\n<li><strong>\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/strong>: \u4e3aJWT\u8bbe\u7f6e\u4e00\u4e2a\u5408\u7406\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u5e76\u5b9e\u73b0\u81ea\u52a8\u5237\u65b0\u673a\u5236\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528JWE\uff08JSON Web Encryption\uff09<\/strong>: \u5982\u679c\u9700\u8981\u4fdd\u62a4JWT\u8d1f\u8f7d\u4e2d\u7684\u654f\u611f\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528JWE\u8fdb\u884c\u52a0\u5bc6\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728HTTP\u62e6\u622a\u5668\u4e2d\u68c0\u67e5JWT\u7684\u8fc7\u671f\u72b6\u6001\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Injectable } from '@angular\/core'; import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular\/common\/http'; import { Observable } from 'rxjs'; import { AuthService } from '.\/auth.service'; @Injectable() export class JwtInterceptor implements HttpInterceptor { constructor(private authService: AuthService) {} intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; { const jwtToken = localStorage.getItem('jwtToken'); const isExpired = this.authService.isTokenExpired(jwtToken); if (jwtToken &amp;&amp; !isExpired) { request = request.clone({ setHeaders: { Authorization: `Bearer ${jwtToken}` } }); } else if (isExpired) { \/\/ Handle token expiration logic here } return next.handle(request); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u540d\u4e3a<code>isTokenExpired<\/code>\u7684\u8f85\u52a9\u51fd\u6570\uff08\u8be5\u51fd\u6570\u53ef\u4ee5\u5728<code>AuthService<\/code>\u4e2d\u5b9e\u73b0\uff09\u6765\u68c0\u67e5JWT\u662f\u5426\u5df2\u8fc7\u671f\u3002\u5982\u679cJWT\u6709\u6548\u4e14\u672a\u8fc7\u671f\uff0c\u6211\u4eec\u5c06\u5176\u6dfb\u52a0\u5230HTTP\u8bf7\u6c42\u7684<code>Authorization<\/code>\u5934\u4e2d\u3002\u5982\u679cJWT\u5df2\u8fc7\u671f\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u6dfb\u52a0\u903b\u8f91\u6765\u5237\u65b0\u4ee4\u724c\u6216\u91cd\u65b0\u767b\u5f55\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u89e3\u51b3\u65b9\u6848\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u5728Angular\u5e94\u7528\u4e2d\u66f4\u5b89\u5168\u5730\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1\u548c\u6388\u6743\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u8ba8\u66f4\u591a\u9ad8\u7ea7\u4e3b\u9898\u548c\u6280\u672f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e94\u7ae0\uff1a\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1\u76845.1 \u4ec0\u4e48\u662fOAuth 2.0ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e94\u7ae0\uff1a\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">5.1 \u4ec0\u4e48\u662fOAuth 2.0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>OAuth 2.0\u662f\u4e00\u4e2a\u5f00\u653e\u6807\u51c6\uff0c\u7528\u4e8e\u8bbf\u95ee\u6388\u6743\u3002\u5b83\u5141\u8bb8\u7528\u6237\u6388\u6743\u7b2c\u4e09\u65b9\u5e94\u7528\u8bbf\u95ee\u7279\u5b9a\u8d44\u6e90\uff0c\u800c\u65e0\u9700\u5c06\u7528\u6237\u540d\u548c\u5bc6\u7801\u76f4\u63a5\u66b4\u9732\u7ed9\u7b2c\u4e09\u65b9\u5e94\u7528\u3002\u8fd9\u79cd\u673a\u5236\u7279\u522b\u9002\u7528\u4e8e\u90a3\u4e9b\u5e0c\u671b\u4f7f\u7528\u5982Google\u3001Facebook\u6216Twitter\u7b49\u7b2c\u4e09\u65b9\u670d\u52a1\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u7684\u5e94\u7528\u3002<\/p>\n\n\n\n<p>OAuth 2.0\u5b9a\u4e49\u4e86\u51e0\u79cd\u6388\u6743\u7c7b\u578b\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6388\u6743\u7801\uff08Authorization Code\uff09<\/strong>: \u9002\u7528\u4e8e\u670d\u52a1\u5668\u5230\u670d\u52a1\u5668\u7684\u4ea4\u4e92\u3002<\/li>\n\n\n\n<li><strong>\u9690\u5f0f\uff08Implicit\uff09<\/strong>: \u4e3b\u8981\u7528\u4e8e\u7eaf\u524d\u7aef\u5e94\u7528\u3002<\/li>\n\n\n\n<li><strong>\u5bc6\u7801\uff08Password\uff09<\/strong>: \u9002\u7528\u4e8e\u4fe1\u4efb\u7ea7\u522b\u975e\u5e38\u9ad8\u7684\u5e94\u7528\u3002<\/li>\n\n\n\n<li><strong>\u5ba2\u6237\u7aef\u51ed\u8bc1\uff08Client Credentials\uff09<\/strong>: \u7528\u4e8e\u5e94\u7528\u81ea\u8eab\u7684\u6388\u6743\uff0c\u800c\u4e0d\u662f\u4ee3\u8868\u7528\u6237\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9\u548c\u7528\u9014<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5206\u79bb\u8ba4\u8bc1\u4e0e\u5e94\u7528<\/strong>: \u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u5355\u4e00\u7684\u8ba4\u8bc1\u670d\u52a1\u8fdb\u884c\u591a\u4e2a\u5e94\u7528\u7684\u767b\u5f55\uff0c\u589e\u52a0\u4e86\u5b89\u5168\u6027\u548c\u4fbf\u5229\u6027\u3002<\/li>\n\n\n\n<li><strong>\u7ec6\u7c92\u5ea6\u7684\u6743\u9650\u63a7\u5236<\/strong>: \u53ef\u4ee5\u6388\u6743\u8bbf\u95ee\u7279\u5b9a\u7c7b\u578b\u7684\u8d44\u6e90\uff0c\u800c\u4e0d\u662f\u5168\u6743\u6388\u6743\u3002<\/li>\n\n\n\n<li><strong>\u591a\u5e73\u53f0\u652f\u6301<\/strong>: OAuth 2.0\u9002\u7528\u4e8e\u591a\u79cd\u7c7b\u578b\u7684\u5e94\u7528\uff0c\u5305\u62ecWeb\u5e94\u7528\u3001\u79fb\u52a8\u5e94\u7528\u548c\u684c\u9762\u5e94\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528OAuth 2.0\u7684\u6388\u6743\u7801\u6d41\u7a0b\u8fdb\u884c\u8ba4\u8bc1\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class AuthService { private token: string; constructor(private http: HttpClient) {} authenticateWithAuthorizationCode(code: string) { const headers = new HttpHeaders({ 'Content-Type': 'application\/x-www-form-urlencoded' }); const body = `grant_type=authorization_code&amp;code=${code}&amp;redirect_uri=YOUR_REDIRECT_URI&amp;client_id=YOUR_CLIENT_ID&amp;client_secret=YOUR_CLIENT_SECRET`; this.http.post('\/oauth\/token', body, { headers }) .subscribe(response =&gt; { this.token = response['access_token']; }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Angular\u7684<code>HttpClient<\/code>\u6a21\u5757\u6765\u53d1\u9001\u4e00\u4e2aHTTP POST\u8bf7\u6c42\u5230OAuth 2.0\u7684\u4ee4\u724c\u7aef\u70b9\u3002\u6211\u4eec\u4f20\u9012\u4e86\u6388\u6743\u7801\uff08\u901a\u5e38\u4ece\u91cd\u5b9a\u5411URI\u7684\u67e5\u8be2\u53c2\u6570\u4e2d\u83b7\u53d6\uff09\u548c\u5176\u4ed6\u5fc5\u8981\u7684\u4fe1\u606f\uff0c\u4ee5\u83b7\u53d6\u8bbf\u95ee\u4ee4\u724c\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cOAuth 2.0\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u5b89\u5168\u3001\u66f4\u7075\u6d3b\u7684\u8ba4\u8bc1\u548c\u6388\u6743\u673a\u5236\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u591a\u65b9\u53c2\u4e0e\u6216\u7b2c\u4e09\u65b9\u96c6\u6210\u7684\u590d\u6742\u5e94\u7528\u573a\u666f\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u548c\u4f7f\u7528OAuth 2.0\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e94\u7ae0\uff1a\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1\u76845.2 OAuth 2.0\u7684\u5de5\u4f5c\u539f\u7406ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e94\u7ae0\uff1a\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">5.2 OAuth 2.0\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>OAuth 2.0\u7684\u5de5\u4f5c\u539f\u7406\u6d89\u53ca\u591a\u4e2a\u53c2\u4e0e\u8005\u548c\u6b65\u9aa4\uff0c\u5305\u62ec\u8d44\u6e90\u6240\u6709\u8005\uff08\u901a\u5e38\u662f\u7528\u6237\uff09\u3001\u6388\u6743\u670d\u52a1\u5668\u3001\u5ba2\u6237\u7aef\uff08\u5e94\u7528\uff09\u548c\u8d44\u6e90\u670d\u52a1\u5668\u3002\u4e0b\u9762\u662fOAuth 2.0\u7684\u57fa\u672c\u5de5\u4f5c\u6d41\u7a0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u6388\u6743<\/strong>: \u7528\u6237\u9996\u5148\u9700\u8981\u6388\u6743\u5e94\u7528\u8bbf\u95ee\u5176\u5728\u8d44\u6e90\u670d\u52a1\u5668\u4e0a\u7684\u7279\u5b9a\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u83b7\u53d6\u6388\u6743\u7801<\/strong>: \u4e00\u65e6\u7528\u6237\u6388\u6743\uff0c\u6388\u6743\u670d\u52a1\u5668\u4f1a\u5411\u5e94\u7528\u53d1\u653e\u4e00\u4e2a\u6388\u6743\u7801\u3002<\/li>\n\n\n\n<li><strong>\u4ea4\u6362\u8bbf\u95ee\u4ee4\u724c<\/strong>: \u5e94\u7528\u4f7f\u7528\u8fd9\u4e2a\u6388\u6743\u7801\u5411\u6388\u6743\u670d\u52a1\u5668\u8bf7\u6c42\u8bbf\u95ee\u4ee4\u724c\u3002<\/li>\n\n\n\n<li><strong>\u8bbf\u95ee\u8d44\u6e90<\/strong>: \u4e00\u65e6\u83b7\u53d6\u4e86\u8bbf\u95ee\u4ee4\u724c\uff0c\u5e94\u7528\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u4ee4\u724c\u6765\u5411\u8d44\u6e90\u670d\u52a1\u5668\u8bf7\u6c42\u7528\u6237\u7684\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u5237\u65b0\u4ee4\u724c<\/strong>: \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8bbf\u95ee\u4ee4\u724c\u53ef\u80fd\u4f1a\u8fc7\u671f\uff0c\u5e94\u7528\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5237\u65b0\u4ee4\u724c\u6765\u83b7\u53d6\u4e00\u4e2a\u65b0\u7684\u8bbf\u95ee\u4ee4\u724c\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9\u548c\u7528\u9014<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5b89\u5168\u6027<\/strong>: \u7531\u4e8e\u7528\u6237\u51ed\u8bc1\u4e0d\u76f4\u63a5\u66b4\u9732\u7ed9\u7b2c\u4e09\u65b9\u5e94\u7528\uff0c\u56e0\u6b64\u5b89\u5168\u6027\u66f4\u9ad8\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u6027<\/strong>: OAuth 2.0\u652f\u6301\u591a\u79cd\u6388\u6743\u7c7b\u578b\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7c7b\u578b\u7684\u5e94\u7528\u548c\u573a\u666f\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u6269\u5c55\u6027<\/strong>: \u53ef\u4ee5\u901a\u8fc7\u4f5c\u7528\u57df\uff08Scopes\uff09\u6765\u5b9e\u73b0\u7ec6\u7c92\u5ea6\u7684\u6743\u9650\u63a7\u5236\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u6388\u6743\u7801\u6765\u83b7\u53d6\u8bbf\u95ee\u4ee4\u724c\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class AuthService { constructor(private http: HttpClient) {} getAccessTokenWithAuthorizationCode(code: string) { const headers = new HttpHeaders({ 'Content-Type': 'application\/x-www-form-urlencoded' }); const body = `grant_type=authorization_code&amp;code=${code}&amp;redirect_uri=YOUR_REDIRECT_URI&amp;client_id=YOUR_CLIENT_ID&amp;client_secret=YOUR_CLIENT_SECRET`; this.http.post('\/oauth\/token', body, { headers }) .subscribe(response =&gt; { const accessToken = response['access_token']; \/\/ Store the access token and use it for subsequent API requests localStorage.setItem('access_token', accessToken); }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Angular\u7684<code>HttpClient<\/code>\u6a21\u5757\u6765\u53d1\u9001\u4e00\u4e2aHTTP POST\u8bf7\u6c42\u5230OAuth 2.0\u7684\u4ee4\u724c\u7aef\u70b9\u3002\u6211\u4eec\u4f20\u9012\u4e86\u6388\u6743\u7801\u548c\u5176\u4ed6\u5fc5\u8981\u7684\u53c2\u6570\uff0c\u4ee5\u4ea4\u6362\u4e00\u4e2a\u8bbf\u95ee\u4ee4\u724c\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u80fd\u4e86\u89e3\u5230OAuth 2.0\u5982\u4f55\u5728\u591a\u4e2a\u53c2\u4e0e\u8005\u548c\u7ec4\u4ef6\u4e4b\u95f4\u8fdb\u884c\u4ea4\u4e92\uff0c\u4ee5\u5b9e\u73b0\u5b89\u5168\u548c\u7075\u6d3b\u7684\u8ba4\u8bc1\u548c\u6388\u6743\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u63a2\u8ba8\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u4f7f\u7528OAuth 2.0\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e94\u7ae0\uff1a\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1\u76845.3 \u5728Angular\u4e2d\u4f7f\u7528OAuth 2.0ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e94\u7ae0\uff1a\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">5.3 \u5728Angular\u4e2d\u4f7f\u7528OAuth 2.0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0OAuth 2.0\u8ba4\u8bc1\u548c\u6388\u6743\u6d89\u53ca\u5230\u591a\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u7528\u6237\u754c\u9762\u3001HTTP\u8bf7\u6c42\u3001\u72b6\u6001\u7ba1\u7406\uff0c\u4ee5\u53ca\u4e0e\u7b2c\u4e09\u65b9\u6388\u6743\u670d\u52a1\u5668\u7684\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u6b65\u9aa4\u548c\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u6388\u6743<\/strong>: \u521b\u5efa\u4e00\u4e2a\u767b\u5f55\u6309\u94ae\u6216\u94fe\u63a5\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u65f6\uff0c\u5c06\u4ed6\u4eec\u91cd\u5b9a\u5411\u5230\u7b2c\u4e09\u65b9\u6388\u6743\u670d\u52a1\u5668\u7684\u6388\u6743\u9875\u9762\u3002<\/li>\n\n\n\n<li><strong>\u83b7\u53d6\u6388\u6743\u7801<\/strong>: \u5728\u7528\u6237\u6210\u529f\u6388\u6743\u540e\uff0c\u6388\u6743\u670d\u52a1\u5668\u4f1a\u91cd\u5b9a\u5411\u56de\u5e94\u7528\uff0c\u5e76\u5728URL\u4e2d\u9644\u5e26\u4e00\u4e2a\u6388\u6743\u7801\u3002<\/li>\n\n\n\n<li><strong>\u4ea4\u6362\u8bbf\u95ee\u4ee4\u724c<\/strong>: \u4f7f\u7528\u83b7\u53d6\u5230\u7684\u6388\u6743\u7801\uff0c\u901a\u8fc7\u540e\u7aef\u6216\u5b89\u5168\u7684\u524d\u7aef\u903b\u8f91\uff0c\u8bf7\u6c42\u8bbf\u95ee\u4ee4\u724c\u3002<\/li>\n\n\n\n<li><strong>\u5b58\u50a8\u8bbf\u95ee\u4ee4\u724c<\/strong>: \u4e00\u65e6\u83b7\u53d6\u5230\u8bbf\u95ee\u4ee4\u724c\uff0c\u5c06\u5176\u5b89\u5168\u5730\u5b58\u50a8\u5728\u5ba2\u6237\u7aef\uff0c\u4ee5\u7528\u4e8e\u540e\u7eed\u7684API\u8bf7\u6c42\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u8bbf\u95ee\u4ee4\u724c<\/strong>: \u5728\u8fdb\u884cAPI\u8bf7\u6c42\u65f6\uff0c\u5c06\u8bbf\u95ee\u4ee4\u724c\u6dfb\u52a0\u5230HTTP\u8bf7\u6c42\u7684<code>Authorization<\/code>\u5934\u4e2d\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528OAuth 2.0\u7684\u6388\u6743\u7801\u6d41\u7a0b\u8fdb\u884c\u8ba4\u8bc1\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { HttpClient, HttpHeaders } from '@angular\/common\/http'; import { Injectable } from '@angular\/core'; import { Router } from '@angular\/router'; @Injectable({ providedIn: 'root' }) export class AuthService { constructor(private http: HttpClient, private router: Router) {} \/\/ Step 1: Redirect the user to the authorization server redirectToAuthorizationServer() { const authorizationUrl = 'https:\/\/auth-server.com\/oauth\/authorize'; const clientId = 'YOUR_CLIENT_ID'; const redirectUri = 'YOUR_REDIRECT_URI'; const scope = 'YOUR_SCOPE'; window.location.href = `${authorizationUrl}?response_type=code&amp;client_id=${clientId}&amp;redirect_uri=${redirectUri}&amp;scope=${scope}`; } \/\/ Step 2: Handle the authorization code received in the redirect URI handleAuthorizationCode() { const urlParams = new URLSearchParams(window.location.search); const authorizationCode = urlParams.get('code'); if (authorizationCode) { this.getAccessTokenWithAuthorizationCode(authorizationCode); } } \/\/ Step 3: Exchange the authorization code for an access token getAccessTokenWithAuthorizationCode(code: string) { const headers = new HttpHeaders({ 'Content-Type': 'application\/x-www-form-urlencoded' }); const body = `grant_type=authorization_code&amp;code=${code}&amp;redirect_uri=YOUR_REDIRECT_URI&amp;client_id=YOUR_CLIENT_ID&amp;client_secret=YOUR_CLIENT_SECRET`; this.http.post('\/oauth\/token', body, { headers }) .subscribe(response =&gt; { const accessToken = response['access_token']; localStorage.setItem('access_token', accessToken); this.router.navigate(['\/home']); }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>redirectToAuthorizationServer<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u91cd\u5b9a\u5411\u7528\u6237\u5230\u6388\u6743\u670d\u52a1\u5668\u7684\u6388\u6743\u9875\u9762\u3002\u7136\u540e\uff0c\u6211\u4eec\u5728\u5e94\u7528\u7684\u91cd\u5b9a\u5411URI\u4e2d\u4f7f\u7528<code>handleAuthorizationCode<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6388\u6743\u7801\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>getAccessTokenWithAuthorizationCode<\/code>\u65b9\u6cd5\u6765\u4ea4\u6362\u8bbf\u95ee\u4ee4\u724c\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0OAuth 2.0\u8ba4\u8bc1\u548c\u6388\u6743\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u5b89\u5168\u548c\u66f4\u7075\u6d3b\u7684\u7528\u6237\u4f53\u9a8c\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u66f4\u591a\u5173\u4e8e\u5728Angular\u5e94\u7528\u4e2d\u4f7f\u7528OAuth 2.0\u7684\u9ad8\u7ea7\u4e3b\u9898\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e94\u7ae0\uff1a\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1\u76845.4 OAuth 2.0\u5b89\u5168\u6027\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e94\u7ae0\uff1a\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">5.4 OAuth 2.0\u5b89\u5168\u6027\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5c3d\u7ba1OAuth 2.0\u63d0\u4f9b\u4e86\u4e00\u79cd\u76f8\u5bf9\u5b89\u5168\u548c\u7075\u6d3b\u7684\u8ba4\u8bc1\u548c\u6388\u6743\u673a\u5236\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u91cd\u5b9a\u5411URI\u52ab\u6301<\/strong>: \u5982\u679c\u91cd\u5b9a\u5411URI\u6ca1\u6709\u6b63\u786e\u914d\u7f6e\uff0c\u653b\u51fb\u8005\u53ef\u80fd\u4f1a\u52ab\u6301\u6388\u6743\u7801\u3002<\/li>\n\n\n\n<li><strong>\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff08CSRF\uff09<\/strong>: \u5728\u6388\u6743\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u4f7f\u7528\u72b6\u6001\u53c2\u6570\uff08state\uff09\uff0c\u5e94\u7528\u53ef\u80fd\u4f1a\u53d7\u5230CSRF\u653b\u51fb\u3002<\/li>\n\n\n\n<li><strong>\u8bbf\u95ee\u4ee4\u724c\u6cc4\u9732<\/strong>: \u8bbf\u95ee\u4ee4\u724c\u5982\u679c\u5b58\u50a8\u4e0d\u5f53\u6216\u5728\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u6cc4\u9732\uff0c\u53ef\u80fd\u4f1a\u88ab\u4e0d\u5f53\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>\u7b2c\u4e09\u65b9\u4f9d\u8d56<\/strong>: \u7531\u4e8eOAuth 2.0\u901a\u5e38\u4f9d\u8d56\u7b2c\u4e09\u65b9\u6388\u6743\u670d\u52a1\u5668\uff0c\u56e0\u6b64\u9700\u8981\u786e\u4fdd\u7b2c\u4e09\u65b9\u670d\u52a1\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9a8c\u8bc1\u91cd\u5b9a\u5411URI<\/strong>: \u603b\u662f\u786e\u4fdd\u91cd\u5b9a\u5411URI\u662f\u9884\u5148\u6ce8\u518c\u5e76\u9a8c\u8bc1\u7684\uff0c\u4ee5\u9632\u6b62\u52ab\u6301\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u72b6\u6001\u53c2\u6570<\/strong>: \u5728\u6388\u6743\u8bf7\u6c42\u4e2d\u4f7f\u7528\u72b6\u6001\u53c2\u6570\uff08state\uff09\u4ee5\u9632\u6b62CSRF\u653b\u51fb\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u5b58\u50a8\u548c\u4f20\u8f93<\/strong>: \u4f7f\u7528HTTPS\u548c\u5b89\u5168\u7684\u5ba2\u6237\u7aef\u5b58\u50a8\u673a\u5236\u6765\u4fdd\u62a4\u8bbf\u95ee\u4ee4\u724c\u3002<\/li>\n\n\n\n<li><strong>\u4ee4\u724c\u8fc7\u671f\u548c\u5237\u65b0<\/strong>: \u8bbe\u7f6e\u8bbf\u95ee\u4ee4\u724c\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u5e76\u4f7f\u7528\u5237\u65b0\u4ee4\u724c\u6765\u83b7\u53d6\u65b0\u7684\u8bbf\u95ee\u4ee4\u724c\u3002<\/li>\n\n\n\n<li><strong>\u5ba1\u67e5\u7b2c\u4e09\u65b9\u670d\u52a1<\/strong>: \u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u6388\u6743\u670d\u52a1\u5668\u65f6\uff0c\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u67e5\u548c\u6301\u7eed\u76d1\u63a7\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728OAuth 2.0\u6388\u6743\u8bf7\u6c42\u4e2d\u4f7f\u7528\u72b6\u6001\u53c2\u6570\uff08state\uff09\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class AuthService { \/\/ Generate a random state value private state = Math.random().toString(36).substring(7); \/\/ Step 1: Redirect the user to the authorization server redirectToAuthorizationServer() { const authorizationUrl = 'https:\/\/auth-server.com\/oauth\/authorize'; const clientId = 'YOUR_CLIENT_ID'; const redirectUri = 'YOUR_REDIRECT_URI'; const scope = 'YOUR_SCOPE'; window.location.href = `${authorizationUrl}?response_type=code&amp;client_id=${clientId}&amp;redirect_uri=${redirectUri}&amp;scope=${scope}&amp;state=${this.state}`; } \/\/ Step 2: Handle the authorization code and state received in the redirect URI handleAuthorizationResponse() { const urlParams = new URLSearchParams(window.location.search); const receivedState = urlParams.get('state'); const authorizationCode = urlParams.get('code'); \/\/ Validate the state parameter to mitigate CSRF attacks if (receivedState === this.state &amp;&amp; authorizationCode) { \/\/ Proceed to exchange the authorization code for an access token } } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u751f\u6210\u4e86\u4e00\u4e2a\u968f\u673a\u7684\u72b6\u6001\u503c\uff08state\uff09\u5e76\u5c06\u5176\u5305\u542b\u5728\u6388\u6743\u8bf7\u6c42\u4e2d\u3002\u5f53\u7528\u6237\u88ab\u91cd\u5b9a\u5411\u56de\u5e94\u7528\u65f6\uff0c\u6211\u4eec\u9a8c\u8bc1\u8fd4\u56de\u7684\u72b6\u6001\u503c\u662f\u5426\u4e0e\u53d1\u9001\u7684\u72b6\u6001\u503c\u5339\u914d\uff0c\u4ee5\u9632\u6b62CSRF\u653b\u51fb\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u89e3\u51b3\u65b9\u6848\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u5728Angular\u5e94\u7528\u4e2d\u66f4\u5b89\u5168\u5730\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1\u548c\u6388\u6743\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u8ba8\u66f4\u591a\u9ad8\u7ea7\u4e3b\u9898\u548c\u6280\u672f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u516d\u7ae0\uff1a\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1\u76846.1 \u4ec0\u4e48\u662fFirebaseChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u516d\u7ae0\uff1a\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">6.1 \u4ec0\u4e48\u662fFirebase<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>Firebase\u662f\u4e00\u4e2a\u7531Google\u5f00\u53d1\u548c\u7ef4\u62a4\u7684\u79fb\u52a8\u548cWeb\u5e94\u7528\u5f00\u53d1\u5e73\u53f0\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u4e30\u5bcc\u7684\u670d\u52a1\u548c\u5de5\u5177\uff0c\u5305\u62ec\u5b9e\u65f6\u6570\u636e\u5e93\u3001\u6587\u4ef6\u5b58\u50a8\u3001\u5206\u6790\u3001\u63a8\u9001\u901a\u77e5\u7b49\u3002\u5176\u4e2d\uff0cFirebase Authentication\u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u5b89\u5168\u8ba4\u8bc1\u7cfb\u7edf\u7684\u670d\u52a1\uff0c\u652f\u6301\u591a\u79cd\u8ba4\u8bc1\u65b9\u5f0f\uff0c\u5982\u7535\u5b50\u90ae\u4ef6\/\u5bc6\u7801\u3001\u793e\u4ea4\u5a92\u4f53\u767b\u5f55\u3001\u624b\u673a\u53f7\u548c\u533f\u540d\u767b\u5f55\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9\u548c\u7528\u9014<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7b80\u5355\u6613\u7528<\/strong>: Firebase\u63d0\u4f9b\u4e86\u6613\u4e8e\u4f7f\u7528\u7684SDK\u548cAPI\uff0c\u53ef\u4ee5\u5feb\u901f\u96c6\u6210\u5230\u5404\u79cd\u5e94\u7528\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u591a\u5e73\u53f0\u652f\u6301<\/strong>: Firebase\u652f\u6301Web\u3001iOS\u3001Android\u548c\u5176\u4ed6\u5e73\u53f0\uff0c\u4f7f\u5f97\u8de8\u5e73\u53f0\u5f00\u53d1\u66f4\u4e3a\u4fbf\u6377\u3002<\/li>\n\n\n\n<li><strong>\u591a\u79cd\u8ba4\u8bc1\u65b9\u5f0f<\/strong>: Firebase Authentication\u652f\u6301\u591a\u79cd\u8ba4\u8bc1\u65b9\u5f0f\uff0c\u5305\u62ec\u7b2c\u4e09\u65b9OAuth\u63d0\u4f9b\u5546\u3001\u624b\u673a\u53f7\u9a8c\u8bc1\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u9ad8\u53ef\u6269\u5c55\u6027<\/strong>: \u9664\u4e86\u8ba4\u8bc1\uff0cFirebase\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u591a\u79cd\u670d\u52a1\uff0c\u5982\u6570\u636e\u5e93\u3001\u4e91\u51fd\u6570\u7b49\uff0c\u65b9\u4fbf\u8fdb\u884c\u540e\u7eed\u6269\u5c55\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Firebase\u8fdb\u884c\u7535\u5b50\u90ae\u4ef6\u548c\u5bc6\u7801\u7684\u8ba4\u8bc1\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { AngularFireAuth } from '@angular\/fire\/auth'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class AuthService { constructor(private afAuth: AngularFireAuth) {} \/\/ Sign up with email and password signUp(email: string, password: string) { this.afAuth.createUserWithEmailAndPassword(email, password) .then(userCredential =&gt; { \/\/ Handle successful sign up }) .catch(error =&gt; { \/\/ Handle sign up error }); } \/\/ Sign in with email and password signIn(email: string, password: string) { this.afAuth.signInWithEmailAndPassword(email, password) .then(userCredential =&gt; { \/\/ Handle successful sign in }) .catch(error =&gt; { \/\/ Handle sign in error }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Angular\u7684Firebase\u5e93\uff08AngularFire\uff09\u6765\u8fdb\u884c\u7535\u5b50\u90ae\u4ef6\u548c\u5bc6\u7801\u7684\u8ba4\u8bc1\u3002<code>createUserWithEmailAndPassword<\/code>\u548c<code>signInWithEmailAndPassword<\/code>\u65b9\u6cd5\u7528\u4e8e\u6ce8\u518c\u548c\u767b\u5f55\u7528\u6237\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528Firebase\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u5730\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u5f3a\u5927\u548c\u5b89\u5168\u7684\u8ba4\u8bc1\u7cfb\u7edf\uff0c\u800c\u65e0\u9700\u4ece\u5934\u5f00\u59cb\u6784\u5efa\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u63a2\u8ba8\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u4f7f\u7528Firebase\u8fdb\u884c\u5404\u79cd\u7c7b\u578b\u7684\u8ba4\u8bc1\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u516d\u7ae0\uff1a\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1\u76846.2 Firebase\u7684\u5de5\u4f5c\u539f\u7406ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u516d\u7ae0\uff1a\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">6.2 Firebase\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>Firebase Authentication\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5168\u9762\u7684\u8ba4\u8bc1\u89e3\u51b3\u65b9\u6848\uff0c\u5b83\u80cc\u540e\u7684\u5de5\u4f5c\u539f\u7406\u76f8\u5bf9\u76f4\u89c2\u548c\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e3b\u8981\u7684\u5de5\u4f5c\u6d41\u7a0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521d\u59cb\u5316Firebase<\/strong>: \u5728\u5e94\u7528\u4e2d\u5f15\u5165Firebase SDK\uff0c\u5e76\u4f7f\u7528Firebase\u63a7\u5236\u53f0\u63d0\u4f9b\u7684\u914d\u7f6e\u4fe1\u606f\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/li>\n\n\n\n<li><strong>\u7528\u6237\u4ea4\u4e92<\/strong>: \u7528\u6237\u901a\u8fc7\u5e94\u7528\u754c\u9762\u8fdb\u884c\u6ce8\u518c\u6216\u767b\u5f55\u3002\u8fd9\u4e9b\u64cd\u4f5c\u4f1a\u901a\u8fc7Firebase SDK\u8f6c\u5316\u4e3a\u4e0eFirebase Authentication\u670d\u52a1\u7684API\u8c03\u7528\u3002<\/li>\n\n\n\n<li><strong>\u670d\u52a1\u5668\u9a8c\u8bc1<\/strong>: Firebase\u670d\u52a1\u5668\u8d1f\u8d23\u9a8c\u8bc1\u7528\u6237\u63d0\u4f9b\u7684\u51ed\u636e\uff08\u5982\u7535\u5b50\u90ae\u4ef6\u3001\u5bc6\u7801\u3001\u793e\u4ea4\u5a92\u4f53\u4ee4\u724c\u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong>\u72b6\u6001\u7ba1\u7406<\/strong>: \u4e00\u65e6\u8ba4\u8bc1\u6210\u529f\uff0cFirebase\u4f1a\u5728\u5ba2\u6237\u7aef\u751f\u6210\u4e00\u4e2aJSON Web Token\uff08JWT\uff09\u4f5c\u4e3a\u7528\u6237\u7684\u8eab\u4efd\u6807\u8bc6\uff0c\u5e76\u53ef\u7528\u4e8e\u540e\u7eed\u7684\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u65f6\u540c\u6b65<\/strong>: Firebase Authentication\u72b6\u6001\uff08\u5982\u767b\u5f55\u3001\u767b\u51fa\u3001\u8d26\u6237\u4fe1\u606f\u66f4\u6539\u7b49\uff09\u53ef\u4ee5\u5728\u591a\u4e2a\u8bbe\u5907\u548c\u5e73\u53f0\u4e4b\u95f4\u5b9e\u65f6\u540c\u6b65\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9\u548c\u7528\u9014<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u65e0\u670d\u52a1\u5668<\/strong>: \u7531\u4e8eFirebase\u8d1f\u8d23\u6240\u6709\u7684\u8ba4\u8bc1\u903b\u8f91\uff0c\u56e0\u6b64\u4f60\u65e0\u9700\u7ef4\u62a4\u4e00\u4e2a\u540e\u7aef\u670d\u52a1\u5668\u6765\u5904\u7406\u8ba4\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u591a\u79cd\u8ba4\u8bc1\u65b9\u5f0f<\/strong>: \u652f\u6301\u7535\u5b50\u90ae\u4ef6\u3001\u5bc6\u7801\u3001\u793e\u4ea4\u5a92\u4f53\u3001\u624b\u673a\u53f7\u7b49\u591a\u79cd\u8ba4\u8bc1\u65b9\u5f0f\u3002<\/li>\n\n\n\n<li><strong>\u96c6\u6210\u5176\u4ed6Firebase\u670d\u52a1<\/strong>: \u8ba4\u8bc1\u72b6\u6001\u53ef\u4ee5\u4e0eFirebase\u7684\u5176\u4ed6\u670d\u52a1\uff08\u5982Firestore\u3001Storage\u7b49\uff09\u8f7b\u677e\u96c6\u6210\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u521d\u59cb\u5316Firebase\u5e76\u76d1\u542c\u8ba4\u8bc1\u72b6\u6001\u7684\u53d8\u5316\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Component, OnInit } from '@angular\/core'; import { AngularFireAuth } from '@angular\/fire\/auth'; import firebase from 'firebase\/app'; @Component({ selector: 'app-root', templateUrl: '.\/app.component.html', styleUrls: ['.\/app.component.css'] }) export class AppComponent implements OnInit { user: firebase.User | null = null; constructor(private afAuth: AngularFireAuth) {} ngOnInit() { \/\/ Initialize Firebase const firebaseConfig = { apiKey: 'YOUR_API_KEY', authDomain: 'YOUR_AUTH_DOMAIN', \/\/ ... other config options }; firebase.initializeApp(firebaseConfig); \/\/ Listen for authentication state changes this.afAuth.authState.subscribe(user =&gt; { this.user = user; }); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7<code>firebase.initializeApp()<\/code>\u65b9\u6cd5\u521d\u59cb\u5316Firebase\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528AngularFire\u5e93\u7684<code>authState<\/code>\u53ef\u89c2\u5bdf\u5bf9\u8c61\u6765\u76d1\u542c\u7528\u6237\u7684\u8ba4\u8bc1\u72b6\u6001\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u5e94\u7528\u4e2d\u8f7b\u677e\u5730\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u4fe1\u606f\u548c\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4e86\u89e3Firebase\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u5404\u79cd\u8ba4\u8bc1\u548c\u6388\u6743\u573a\u666f\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u66f4\u591a\u9ad8\u7ea7\u4e3b\u9898\u548cFirebase\u8ba4\u8bc1\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u516d\u7ae0\uff1a\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1\u76846.3 \u5728Angular\u4e2d\u4f7f\u7528FirebaseChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u516d\u7ae0\uff1a\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">6.3 \u5728Angular\u4e2d\u4f7f\u7528Firebase<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728Angular\u5e94\u7528\u4e2d\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1\u662f\u4e00\u4e2a\u76f8\u5bf9\u7b80\u5355\u548c\u76f4\u63a5\u7684\u8fc7\u7a0b\u3002AngularFire\u5e93\u63d0\u4f9b\u4e86\u4e00\u7ec4Angular\u53cb\u597d\u7684API\u548c\u670d\u52a1\uff0c\u4f7f\u5f97\u4e0eFirebase\u7684\u96c6\u6210\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002\u4ee5\u4e0b\u662f\u5728Angular\u4e2d\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1\u7684\u4e3b\u8981\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b89\u88c5\u4f9d\u8d56<\/strong>: \u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Firebase\u548cAngularFire\u5e93\u3002<\/li>\n\n\n\n<li><strong>\u914d\u7f6eFirebase<\/strong>: \u5728Angular\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u5728\u73af\u5883\u6587\u4ef6\u6216AppModule\u4e2d\u914d\u7f6eFirebase\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528AngularFireAuth<\/strong>: AngularFire\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>AngularFireAuth<\/code>\u670d\u52a1\uff0c\u7528\u4e8e\u5904\u7406\u6240\u6709\u4e0e\u8ba4\u8bc1\u76f8\u5173\u7684\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u5b9e\u73b0\u8ba4\u8bc1UI<\/strong>: \u4f60\u53ef\u4ee5\u4f7f\u7528Angular\u7ec4\u4ef6\u548c\u670d\u52a1\u6765\u6784\u5efa\u8ba4\u8bc1\u76f8\u5173\u7684\u7528\u6237\u754c\u9762\u3002<\/li>\n\n\n\n<li><strong>\u72b6\u6001\u7ba1\u7406<\/strong>: \u4f7f\u7528AngularFire\u7684<code>authState<\/code>\u53ef\u89c2\u5bdf\u5bf9\u8c61\u6765\u7ba1\u7406\u548c\u76d1\u542c\u7528\u6237\u7684\u8ba4\u8bc1\u72b6\u6001\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Firebase\u8fdb\u884c\u7535\u5b50\u90ae\u4ef6\u548c\u5bc6\u7801\u7684\u8ba4\u8bc1\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { AngularFireAuth } from '@angular\/fire\/auth'; import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class AuthService { constructor(private afAuth: AngularFireAuth) {} \/\/ Sign up with email and password async signUp(email: string, password: string) { try { const userCredential = await this.afAuth.createUserWithEmailAndPassword(email, password); \/\/ Handle successful sign up console.log('User successfully signed up:', userCredential); } catch (error) { \/\/ Handle sign up error console.error('Sign up error:', error); } } \/\/ Sign in with email and password async signIn(email: string, password: string) { try { const userCredential = await this.afAuth.signInWithEmailAndPassword(email, password); \/\/ Handle successful sign in console.log('User successfully signed in:', userCredential); } catch (error) { \/\/ Handle sign in error console.error('Sign in error:', error); } } \/\/ Sign out async signOut() { try { await this.afAuth.signOut(); \/\/ Handle successful sign out console.log('User successfully signed out'); } catch (error) { \/\/ Handle sign out error console.error('Sign out error:', error); } } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>AngularFireAuth<\/code>\u670d\u52a1\u7684<code>createUserWithEmailAndPassword<\/code>\u3001<code>signInWithEmailAndPassword<\/code>\u548c<code>signOut<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u7528\u6237\u7684\u6ce8\u518c\u3001\u767b\u5f55\u548c\u767b\u51fa\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728Angular\u5e94\u7528\u4e2d\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1\uff0c\u540c\u65f6\u4eab\u53d7\u5230Firebase\u548cAngular\u4e24\u4e2a\u5e73\u53f0\u7684\u4f18\u52bf\u548c\u4fbf\u5229\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u63a2\u8ba8\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u4f7f\u7528Firebase\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u8ba4\u8bc1\u548c\u6388\u6743\u64cd\u4f5c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u516d\u7ae0\uff1a\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1\u76846.4 Firebase\u5b89\u5168\u6027\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u516d\u7ae0\uff1a\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">6.4 Firebase\u5b89\u5168\u6027\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u867d\u7136Firebase\u63d0\u4f9b\u4e86\u4e00\u79cd\u76f8\u5bf9\u7b80\u5355\u548c\u5b89\u5168\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5e94\u7528\u7684\u8ba4\u8bc1\uff0c\u4f46\u4ecd\u7136\u5b58\u5728\u4e00\u4e9b\u5b89\u5168\u6027\u95ee\u9898\u548c\u6311\u6218\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u6cc4\u9732<\/strong>: \u5982\u679cFirebase\u6570\u636e\u5e93\u7684\u5b89\u5168\u89c4\u5219\u6ca1\u6709\u6b63\u786e\u8bbe\u7f6e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u6cc4\u9732\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u5b89\u5168\u7684\u5ba2\u6237\u7aef\u5b58\u50a8<\/strong>: \u5982\u679c\u8bbf\u95ee\u4ee4\u724c\u6216\u5176\u4ed6\u654f\u611f\u4fe1\u606f\u5728\u5ba2\u6237\u7aef\u5b58\u50a8\u4e0d\u5f53\uff0c\u53ef\u80fd\u4f1a\u88ab\u653b\u51fb\u8005\u83b7\u53d6\u3002<\/li>\n\n\n\n<li><strong>\u8d26\u6237\u679a\u4e3e<\/strong>: \u5982\u679c\u5e94\u7528\u7684\u9519\u8bef\u6d88\u606f\u8fc7\u4e8e\u8be6\u7ec6\uff0c\u653b\u51fb\u8005\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u679a\u4e3e\u6709\u6548\u7684\u7528\u6237\u8d26\u6237\u3002<\/li>\n\n\n\n<li><strong>\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff08XSS\uff09<\/strong>: \u5982\u679c\u5e94\u7528\u6ca1\u6709\u6b63\u786e\u5730\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u53ef\u80fd\u4f1a\u53d7\u5230XSS\u653b\u51fb\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8bbe\u7f6e\u5b89\u5168\u89c4\u5219<\/strong>: \u5728Firebase\u63a7\u5236\u53f0\u4e2d\uff0c\u4e3a\u4f60\u7684\u6570\u636e\u5e93\u548c\u5176\u4ed6\u8d44\u6e90\u8bbe\u7f6e\u4e25\u683c\u7684\u5b89\u5168\u89c4\u5219\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u5ba2\u6237\u7aef\u5b58\u50a8<\/strong>: \u4f7f\u7528\u5b89\u5168\u7684\u65b9\u6cd5\uff08\u5982HttpOnly cookies\u6216\u5b89\u5168\u7684\u672c\u5730\u5b58\u50a8\uff09\u6765\u5b58\u50a8\u654f\u611f\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u9650\u5236\u9519\u8bef\u4fe1\u606f<\/strong>: \u5728\u5e94\u7528\u4e2d\uff0c\u9650\u5236\u9519\u8bef\u4fe1\u606f\u7684\u8be6\u7ec6\u7a0b\u5ea6\uff0c\u4ee5\u9632\u6b62\u8d26\u6237\u679a\u4e3e\u3002<\/li>\n\n\n\n<li><strong>\u8f93\u5165\u9a8c\u8bc1\u548c\u8f93\u51fa\u7f16\u7801<\/strong>: \u5bf9\u6240\u6709\u7528\u6237\u8f93\u5165\u8fdb\u884c\u9a8c\u8bc1\uff0c\u5e76\u5728\u5448\u73b0\u4e4b\u524d\u8fdb\u884c\u9002\u5f53\u7684\u8f93\u51fa\u7f16\u7801\uff0c\u4ee5\u9632\u6b62XSS\u653b\u51fb\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b89\u5168\u5730\u5904\u7406\u7528\u6237\u8f93\u5165\u4ee5\u9632\u6b62XSS\u653b\u51fb\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Component } from '@angular\/core'; import { DomSanitizer, SafeHtml } from '@angular\/platform-browser'; @Component({ selector: 'app-safe-output', template: `&lt;div [innerHTML]=\"safeHtml\"&gt;&lt;\/div&gt;` }) export class SafeOutputComponent { safeHtml: SafeHtml; constructor(private sanitizer: DomSanitizer) {} updateOutput(userInput: string) { \/\/ Sanitize user input to prevent XSS attacks this.safeHtml = this.sanitizer.bypassSecurityTrustHtml(userInput); } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Angular\u7684<code>DomSanitizer<\/code>\u670d\u52a1\u6765\u5904\u7406\u7528\u6237\u8f93\u5165\u3002\u8fd9\u6837\uff0c\u5373\u4f7f\u7528\u6237\u8f93\u5165\u5305\u542b\u6f5c\u5728\u7684\u6076\u610f\u4ee3\u7801\uff0c\u4e5f\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u89e3\u51b3\u65b9\u6848\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u5728\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1\u65f6\u589e\u52a0\u5e94\u7528\u7684\u5b89\u5168\u6027\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u8ba8\u66f4\u591a\u9ad8\u7ea7\u4e3b\u9898\u548cFirebase\u8ba4\u8bc1\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e03\u7ae0\uff1a\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\u76847.1 \u4ec0\u4e48\u662f\u89d2\u8272\u548c\u6743\u9650ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e03\u7ae0\uff1a\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">7.1 \u4ec0\u4e48\u662f\u89d2\u8272\u548c\u6743\u9650<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\u662f\u5e94\u7528\u5b89\u5168\u6027\u7684\u4e00\u4e2a\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u5728\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e2d\uff0c&#8221;\u89d2\u8272&#8221;\u901a\u5e38\u6307\u7684\u662f\u4e00\u7ec4\u4e0e\u7279\u5b9a\u7528\u6237\u6216\u7528\u6237\u7ec4\u5173\u8054\u7684\u6743\u9650\uff0c\u800c&#8221;\u6743\u9650&#8221;\u5219\u662f\u5bf9\u5e94\u7528\u5185\u7279\u5b9a\u8d44\u6e90\u6216\u64cd\u4f5c\u7684\u8bbf\u95ee\u63a7\u5236\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89d2\u8272\uff08Role\uff09<\/strong>: \u89d2\u8272\u662f\u4e00\u79cd\u62bd\u8c61\u7684\u6982\u5ff5\uff0c\u7528\u4e8e\u5c06\u4e00\u7ec4\u6743\u9650\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u201c\u7ba1\u7406\u5458\u201d\u89d2\u8272\u53ef\u80fd\u5177\u6709\u521b\u5efa\u3001\u7f16\u8f91\u548c\u5220\u9664\u7528\u6237\u7684\u6743\u9650\uff0c\u800c\u4e00\u4e2a\u201c\u8bbf\u5ba2\u201d\u89d2\u8272\u53ef\u80fd\u53ea\u6709\u67e5\u770b\u5185\u5bb9\u7684\u6743\u9650\u3002<\/li>\n\n\n\n<li><strong>\u6743\u9650\uff08Permission\uff09<\/strong>: \u6743\u9650\u662f\u5bf9\u5355\u4e00\u64cd\u4f5c\u6216\u8d44\u6e90\u7684\u8bbf\u95ee\u63a7\u5236\u3002\u4f8b\u5982\uff0c\u201c\u8bfb\u53d6\u6587\u7ae0\u201d\u6216\u201c\u7f16\u8f91\u7528\u6237\u8d44\u6599\u201d\u90fd\u662f\u5355\u4e00\u7684\u6743\u9650\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9\u548c\u7528\u9014<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027<\/strong>: \u901a\u8fc7\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u54ea\u4e9b\u7528\u6237\u53ef\u4ee5\u8bbf\u95ee\u54ea\u4e9b\u8d44\u6e90\u6216\u6267\u884c\u54ea\u4e9b\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u6027<\/strong>: \u6b63\u786e\u5730\u5b9e\u65bd\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5e94\u7528\u7684\u5b89\u5168\u6027\u3002<\/li>\n\n\n\n<li><strong>\u6613\u4e8e\u7ef4\u62a4<\/strong>: \u901a\u8fc7\u5c06\u6743\u9650\u5206\u7ec4\u4e3a\u89d2\u8272\uff0c\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u548c\u66f4\u65b0\u7528\u6237\u7684\u8bbf\u95ee\u6743\u9650\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Angular\u670d\u52a1\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u57fa\u4e8e\u89d2\u8272\u548c\u6743\u9650\u8fdb\u884c\u8bbf\u95ee\u63a7\u5236\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class RolePermissionService { private rolesPermissions = { 'admin': ['create', 'read', 'update', 'delete'], 'editor': ['read', 'update'], 'viewer': ['read'] }; \/\/ Check if a user with a given role has a specific permission hasPermission(role: string, permission: string): boolean { const permissions = this.rolesPermissions[role]; return permissions ? permissions.includes(permission) : false; } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>rolesPermissions<\/code>\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e0d\u540c\u89d2\u8272\u548c\u5bf9\u5e94\u7684\u6743\u9650\u3002\u7136\u540e\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>hasPermission<\/code>\u65b9\u6cd5\uff0c\u7528\u4e8e\u68c0\u67e5\u7ed9\u5b9a\u89d2\u8272\u662f\u5426\u5177\u6709\u7279\u5b9a\u7684\u6743\u9650\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u5b9e\u73b0\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\uff0c\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u7528\u6237\u5bf9\u5e94\u7528\u8d44\u6e90\u548c\u64cd\u4f5c\u7684\u8bbf\u95ee\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7684\u5b89\u5168\u6027\u548c\u53ef\u7528\u6027\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u66f4\u6df1\u5165\u5730\u63a2\u8ba8\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u9ad8\u7ea7\u7684\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\u7b56\u7565\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e03\u7ae0\uff1a\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\u76847.2 \u5982\u4f55\u5728Angular\u4e2d\u7ba1\u7406\u89d2\u8272\u548c\u6743\u9650ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e03\u7ae0\uff1a\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">7.2 \u5982\u4f55\u5728Angular\u4e2d\u7ba1\u7406\u89d2\u8272\u548c\u6743\u9650<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728Angular\u5e94\u7528\u4e2d\u7ba1\u7406\u89d2\u8272\u548c\u6743\u9650\u901a\u5e38\u6d89\u53ca\u51e0\u4e2a\u5173\u952e\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49\u89d2\u8272\u548c\u6743\u9650<\/strong>: \u9996\u5148\uff0c\u4f60\u9700\u8981\u660e\u786e\u5b9a\u4e49\u5e94\u7528\u4e2d\u5c06\u4f7f\u7528\u7684\u5404\u79cd\u89d2\u8272\u548c\u6743\u9650\u3002<\/li>\n\n\n\n<li><strong>\u7528\u6237\u8eab\u4efd\u8bc6\u522b<\/strong>: \u901a\u8fc7\u8ba4\u8bc1\u673a\u5236\uff08\u5982JWT\u3001OAuth\u7b49\uff09\u6765\u8bc6\u522b\u7528\u6237\uff0c\u5e76\u83b7\u53d6\u5176\u89d2\u8272\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u8bbf\u95ee\u63a7\u5236<\/strong>: \u5728\u8def\u7531\u3001\u7ec4\u4ef6\u6216\u670d\u52a1\u4e2d\u5b9e\u65bd\u57fa\u4e8e\u89d2\u8272\u6216\u6743\u9650\u7684\u8bbf\u95ee\u63a7\u5236\u3002<\/li>\n\n\n\n<li><strong>\u52a8\u6001UI\u8c03\u6574<\/strong>: \u6839\u636e\u7528\u6237\u7684\u89d2\u8272\u6216\u6743\u9650\u52a8\u6001\u5730\u663e\u793a\u6216\u9690\u85cfUI\u5143\u7d20\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u8def\u7531\u4e2d\u5b9e\u65bd\u57fa\u4e8e\u89d2\u8272\u7684\u8bbf\u95ee\u63a7\u5236\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Injectable } from '@angular\/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular\/router'; import { AuthService } from '.\/auth.service'; @Injectable({ providedIn: 'root' }) export class RoleGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { const expectedRole = route.data.expectedRole; const currentRole = this.authService.getUserRole(); \/\/ Assume this method returns the current user's role if (currentRole !== expectedRole) { \/\/ Navigate to a 'not authorized' page or redirect to login this.router.navigate(['not-authorized']); return false; } return true; } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>RoleGuard<\/code>\u7684\u8def\u7531\u5b88\u536b\uff0c\u8be5\u5b88\u536b\u4f7f\u7528<code>AuthService<\/code>\u670d\u52a1\u6765\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u89d2\u8272\uff0c\u5e76\u4e0e\u8def\u7531\u5143\u6570\u636e\u4e2d\u5b9a\u4e49\u7684<code>expectedRole<\/code>\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679c\u89d2\u8272\u4e0d\u5339\u914d\uff0c\u7528\u6237\u5c06\u88ab\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u201c\u672a\u6388\u6743\u201d\u9875\u9762\u3002<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>\/\/ In your routing module const routes: Routes = [ { path: 'admin', component: AdminComponent, canActivate: [RoleGuard], data: { expectedRole: 'admin' } }, \/\/ ... other routes ];<\/code><\/p>\n\n\n\n<p>\u5728\u8def\u7531\u914d\u7f6e\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>canActivate<\/code>\u5c5e\u6027\u6765\u5e94\u7528<code>RoleGuard<\/code>\uff0c\u5e76\u901a\u8fc7<code>data<\/code>\u5c5e\u6027\u6765\u6307\u5b9a\u9884\u671f\u7684\u89d2\u8272\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\u3002\u8fd9\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7684\u5b89\u5168\u6027\uff0c\u8fd8\u53ef\u4ee5\u63d0\u4f9b\u66f4\u4e2a\u6027\u5316\u7684\u7528\u6237\u4f53\u9a8c\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u66f4\u591a\u9ad8\u7ea7\u4e3b\u9898\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e03\u7ae0\uff1a\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\u76847.3 \u89d2\u8272\u548c\u6743\u9650\u7684\u6700\u4f73\u5b9e\u8df5ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e03\u7ae0\uff1a\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">7.3 \u89d2\u8272\u548c\u6743\u9650\u7684\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5f53\u5b9e\u65bd\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\u65f6\uff0c\u6709\u51e0\u4e2a\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6784\u5efa\u66f4\u5b89\u5168\u3001\u66f4\u53ef\u7ef4\u62a4\u7684\u5e94\u7528\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6700\u5c0f\u6743\u9650\u539f\u5219<\/strong>: \u53ea\u6388\u4e88\u7528\u6237\u5b8c\u6210\u4efb\u52a1\u6240\u9700\u7684\u6700\u5c0f\u6743\u9650\uff0c\u4ee5\u51cf\u5c11\u6f5c\u5728\u7684\u5b89\u5168\u98ce\u9669\u3002<\/li>\n\n\n\n<li><strong>\u89d2\u8272\u7ee7\u627f<\/strong>: \u4f7f\u7528\u89d2\u8272\u7ee7\u627f\u6765\u7b80\u5316\u6743\u9650\u7ba1\u7406\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u201c\u7ba1\u7406\u5458\u201d\u89d2\u8272\u53ef\u4ee5\u7ee7\u627f\u201c\u7f16\u8f91\u8005\u201d\u89d2\u8272\u7684\u6240\u6709\u6743\u9650\u3002<\/li>\n\n\n\n<li><strong>\u52a8\u6001\u6743\u9650\u5206\u914d<\/strong>: \u5c3d\u91cf\u907f\u514d\u786c\u7f16\u7801\u89d2\u8272\u548c\u6743\u9650\uff0c\u800c\u662f\u901a\u8fc7\u6570\u636e\u5e93\u6216\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u52a8\u6001\u5206\u914d\u3002<\/li>\n\n\n\n<li><strong>\u5ba1\u8ba1\u548c\u76d1\u63a7<\/strong>: \u5b9a\u671f\u5ba1\u67e5\u6743\u9650\u8bbe\u7f6e\uff0c\u5e76\u76d1\u63a7\u5f02\u5e38\u8bbf\u95ee\u884c\u4e3a\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u5df2\u6709\u89e3\u51b3\u65b9\u6848<\/strong>: \u5982\u679c\u53ef\u80fd\uff0c\u4f7f\u7528\u6210\u719f\u7684\u5e93\u6216\u6846\u67b6\u6765\u7ba1\u7406\u89d2\u8272\u548c\u6743\u9650\uff0c\u4ee5\u51cf\u5c11\u9519\u8bef\u7684\u53ef\u80fd\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2aAngular\u670d\u52a1\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u65bd\u6700\u5c0f\u6743\u9650\u539f\u5219\u548c\u89d2\u8272\u7ee7\u627f\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>import { Injectable } from '@angular\/core'; @Injectable({ providedIn: 'root' }) export class RolePermissionService { private baseRolesPermissions = { 'viewer': ['read'], 'editor': ['read', 'update'], 'admin': ['read', 'update', 'create', 'delete'] }; private roleInheritance = { 'editor': ['viewer'], 'admin': ['editor'] }; \/\/ Get all permissions for a role, including inherited permissions getAllPermissions(role: string): string[] { let permissions = [...this.baseRolesPermissions[role] || []]; const inheritedRoles = this.roleInheritance[role] || []; for (const inheritedRole of inheritedRoles) { permissions = [...permissions, ...this.getAllPermissions(inheritedRole)]; } return Array.from(new Set(permissions)); \/\/ Remove duplicates } }<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>baseRolesPermissions<\/code>\u5bf9\u8c61\u6765\u5b58\u50a8\u6bcf\u4e2a\u89d2\u8272\u7684\u57fa\u7840\u6743\u9650\uff0c\u4ee5\u53ca\u4e00\u4e2a<code>roleInheritance<\/code>\u5bf9\u8c61\u6765\u5b9a\u4e49\u89d2\u8272\u7ee7\u627f\u5173\u7cfb\u3002\u7136\u540e\uff0c\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>getAllPermissions<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u89d2\u8272\u7684\u6240\u6709\u6743\u9650\uff0c\u5305\u62ec\u4ece\u5176\u4ed6\u89d2\u8272\u7ee7\u627f\u7684\u6743\u9650\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u65e2\u5b89\u5168\u53c8\u6613\u4e8e\u7ba1\u7406\u7684\u89d2\u8272\u548c\u6743\u9650\u7cfb\u7edf\u3002\u8fd9\u5c06\u6709\u52a9\u4e8e\u4f60\u66f4\u6709\u6548\u5730\u63a7\u5236\u7528\u6237\u5bf9\u5e94\u7528\u8d44\u6e90\u548c\u64cd\u4f5c\u7684\u8bbf\u95ee\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7684\u6574\u4f53\u5b89\u5168\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u8ba8\u66f4\u591a\u9ad8\u7ea7\u4e3b\u9898\u548c\u5b9e\u7528\u6280\u5de7\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u516b\u7ae0\uff1a\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5\u76848.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u5b89\u5168\u6027\u5ba1\u8ba1ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u516b\u7ae0\uff1a\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">8.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u5b89\u5168\u6027\u5ba1\u8ba1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5b89\u5168\u6027\u5ba1\u8ba1\u662f\u4e00\u4e2a\u7cfb\u7edf\u6027\u7684\u8bc4\u4f30\u8fc7\u7a0b\uff0c\u65e8\u5728\u8bc6\u522b\u5e94\u7528\u4e2d\u7684\u6f5c\u5728\u5b89\u5168\u6f0f\u6d1e\u548c\u98ce\u9669\u3002\u8fd9\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u56e0\u4e3a\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8bc6\u522b\u9690\u60a3<\/strong>: \u4ee3\u7801\u548c\u914d\u7f6e\u4e2d\u53ef\u80fd\u5b58\u5728\u4e0d\u6613\u5bdf\u89c9\u7684\u5b89\u5168\u9690\u60a3\uff0c\u8fd9\u4e9b\u9690\u60a3\u5728\u672a\u7ecf\u5ba1\u8ba1\u7684\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u88ab\u5ffd\u89c6\u3002<\/li>\n\n\n\n<li><strong>\u5408\u89c4\u6027<\/strong>: \u5bf9\u4e8e\u8bb8\u591a\u884c\u4e1a\u548c\u5730\u533a\uff0c\u8fdb\u884c\u5b9a\u671f\u7684\u5b89\u5168\u6027\u5ba1\u8ba1\u662f\u6cd5\u5f8b\u6216\u89c4\u5b9a\u7684\u8981\u6c42\u3002<\/li>\n\n\n\n<li><strong>\u4fe1\u4efb\u548c\u4fe1\u8a89<\/strong>: \u4e00\u4e2a\u7ecf\u8fc7\u4e25\u683c\u5b89\u5168\u5ba1\u8ba1\u7684\u5e94\u7528\u66f4\u5bb9\u6613\u8d62\u5f97\u7528\u6237\u548c\u5ba2\u6237\u7684\u4fe1\u4efb\u3002<\/li>\n\n\n\n<li><strong>\u98ce\u9669\u7f13\u89e3<\/strong>: \u901a\u8fc7\u5ba1\u8ba1\uff0c\u4f60\u53ef\u4ee5\u8bc6\u522b\u5e76\u4f18\u5148\u89e3\u51b3\u6700\u9ad8\u98ce\u9669\u7684\u5b89\u5168\u95ee\u9898\uff0c\u4ece\u800c\u51cf\u5c11\u6f5c\u5728\u7684\u635f\u5bb3\u3002<\/li>\n\n\n\n<li><strong>\u6301\u7eed\u6539\u8fdb<\/strong>: \u5b89\u5168\u6027\u5ba1\u8ba1\u63d0\u4f9b\u4e86\u4e00\u4e2a\u673a\u4f1a\uff0c\u8ba9\u4f60\u53ef\u4ee5\u8bc4\u4f30\u73b0\u6709\u7684\u5b89\u5168\u63aa\u65bd\u5e76\u8fdb\u884c\u5fc5\u8981\u7684\u8c03\u6574\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9\u548c\u7528\u9014<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5168\u9762\u6027<\/strong>: \u5ba1\u8ba1\u901a\u5e38\u6db5\u76d6\u4ee3\u7801\u5ba1\u67e5\u3001\u914d\u7f6e\u68c0\u67e5\u3001\u4f9d\u8d56\u5206\u6790\u7b49\u591a\u4e2a\u65b9\u9762\u3002<\/li>\n\n\n\n<li><strong>\u4e13\u4e1a\u6027<\/strong>: \u4e13\u4e1a\u7684\u5b89\u5168\u5ba1\u8ba1\u901a\u5e38\u7531\u5177\u6709\u6df1\u5165\u5b89\u5168\u77e5\u8bc6\u7684\u4e13\u5bb6\u8fdb\u884c\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u884c\u6027\u5206\u6790<\/strong>: \u5ba1\u8ba1\u7ed3\u679c\u901a\u5e38\u5305\u62ec\u9488\u5bf9\u8bc6\u522b\u51fa\u7684\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u548c\u5efa\u8bae\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u867d\u7136\u5b89\u5168\u6027\u5ba1\u8ba1\u901a\u5e38\u662f\u4e00\u4e2a\u624b\u52a8\u8fc7\u7a0b\uff0c\u4f46\u4e00\u4e9b\u5de5\u5177\u548c\u5e93\u53ef\u4ee5\u5e2e\u52a9\u81ea\u52a8\u5316\u67d0\u4e9b\u65b9\u9762\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>npm audit<\/code>\u547d\u4ee4\u6765\u68c0\u67e5\u9879\u76ee\u4f9d\u8d56\u4e2d\u7684\u5df2\u77e5\u5b89\u5168\u6f0f\u6d1e\u3002<\/p>\n\n\n\n<p>bashCopy code<\/p>\n\n\n\n<p><code>npm audit<\/code><\/p>\n\n\n\n<p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u751f\u6210\u4e00\u4e2a\u62a5\u544a\uff0c\u5217\u51fa\u6240\u6709\u5df2\u77e5\u7684\u5b89\u5168\u95ee\u9898\uff0c\u5e76\u63d0\u4f9b\u4fee\u590d\u5efa\u8bae\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4e86\u89e3\u4e3a\u4ec0\u4e48\u9700\u8981\u5b89\u5168\u6027\u5ba1\u8ba1\uff0c\u4f60\u53ef\u4ee5\u66f4\u5168\u9762\u5730\u8bc4\u4f30\u548c\u63d0\u9ad8\u5e94\u7528\u7684\u5b89\u5168\u6027\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u8ba1\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u5e38\u89c1\u7684\u5b89\u5168\u95ee\u9898\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u516b\u7ae0\uff1a\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5\u76848.2 \u5982\u4f55\u8fdb\u884c\u5b89\u5168\u6027\u6d4b\u8bd5ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u516b\u7ae0\uff1a\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">8.2 \u5982\u4f55\u8fdb\u884c\u5b89\u5168\u6027\u6d4b\u8bd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5b89\u5168\u6027\u6d4b\u8bd5\u662f\u4e00\u79cd\u4e13\u95e8\u9488\u5bf9\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6f0f\u6d1e\u8fdb\u884c\u7684\u6d4b\u8bd5\u65b9\u6cd5\u3002\u8fd9\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5a01\u80c1\u5efa\u6a21<\/strong>: \u5728\u5f00\u59cb\u6d4b\u8bd5\u4e4b\u524d\uff0c\u8bc6\u522b\u53ef\u80fd\u7684\u5b89\u5168\u5a01\u80c1\u548c\u653b\u51fb\u5411\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u6d4b\u8bd5\u8ba1\u5212<\/strong>: \u6839\u636e\u5a01\u80c1\u5efa\u6a21\u7684\u7ed3\u679c\uff0c\u5236\u5b9a\u8be6\u7ec6\u7684\u5b89\u5168\u6d4b\u8bd5\u8ba1\u5212\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u5316\u626b\u63cf<\/strong>: \u4f7f\u7528\u81ea\u52a8\u5316\u5de5\u5177\uff08\u5982OWASP ZAP\u3001Burp Suite\u7b49\uff09\u6765\u626b\u63cf\u5e38\u89c1\u7684\u5b89\u5168\u6f0f\u6d1e\u3002<\/li>\n\n\n\n<li><strong>\u624b\u52a8\u6d4b\u8bd5<\/strong>: \u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u6216\u7279\u5b9a\u7684\u5b89\u5168\u95ee\u9898\uff0c\u8fdb\u884c\u624b\u52a8\u6d4b\u8bd5\u3002<\/li>\n\n\n\n<li><strong>\u4ee3\u7801\u5ba1\u67e5<\/strong>: \u76f4\u63a5\u68c0\u67e5\u4ee3\u7801\u4ee5\u8bc6\u522b\u53ef\u80fd\u7684\u5b89\u5168\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u62a5\u544a\u548c\u4fee\u590d<\/strong>: \u5c06\u6d4b\u8bd5\u7ed3\u679c\u6574\u7406\u6210\u62a5\u544a\uff0c\u5e76\u6839\u636e\u62a5\u544a\u8fdb\u884c\u4fee\u590d\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<a href=\"https:\/\/jestjs.io\/\">Jest<\/a>\u548c<a href=\"https:\/\/github.com\/visionmedia\/supertest\">Supertest<\/a>\u8fdb\u884cAPI\u5b89\u5168\u6027\u6d4b\u8bd5\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p>typescriptCopy code<\/p>\n\n\n\n<p><code>const request = require('supertest'); const app = require('.\/app'); \/\/ Your Express app describe('Security tests', () =&gt; { it('should not allow SQL injection', async () =&gt; { const response = await request(app) .get('\/api\/items?name=anything\\' OR \\'1\\'=\\'1') .expect(400); expect(response.body.error).toBe('Invalid input'); }); it('should enforce authentication', async () =&gt; { const response = await request(app) .get('\/api\/secure-endpoint') .expect(401); expect(response.body.error).toBe('Unauthorized'); }); });<\/code><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Supertest\u5e93\u6765\u6a21\u62dfAPI\u8bf7\u6c42\uff0c\u5e76\u4f7f\u7528Jest\u4f5c\u4e3a\u6d4b\u8bd5\u8fd0\u884c\u5668\u3002\u7b2c\u4e00\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u5c1d\u8bd5\u8fdb\u884cSQL\u6ce8\u5165\u653b\u51fb\uff0c\u800c\u7b2c\u4e8c\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u68c0\u67e5\u662f\u5426\u5b9e\u65bd\u4e86\u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u8fdb\u884c\u5168\u9762\u548c\u7ec6\u81f4\u7684\u5b89\u5168\u6027\u6d4b\u8bd5\uff0c\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u8bc6\u522b\u548c\u4fee\u590d\u5e94\u7528\u4e2d\u7684\u5b89\u5168\u6f0f\u6d1e\uff0c\u4ece\u800c\u63d0\u9ad8\u5e94\u7528\u7684\u6574\u4f53\u5b89\u5168\u6027\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u66f4\u591a\u5173\u4e8e\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5\u7684\u9ad8\u7ea7\u4e3b\u9898\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u516b\u7ae0\uff1a\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5\u76848.3 \u4f7f\u7528\u5de5\u5177\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u8ba1ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u516b\u7ae0\uff1a\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">8.3 \u4f7f\u7528\u5de5\u5177\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u8ba1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u4f7f\u7528\u5de5\u5177\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u8ba1\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5ba1\u8ba1\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u5206\u4e3a\u51e0\u4e2a\u7c7b\u522b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f9d\u8d56\u626b\u63cf\u5de5\u5177<\/strong>: \u5982<code>npm audit<\/code>\u6216<a href=\"https:\/\/snyk.io\/\">Snyk<\/a>, \u7528\u4e8e\u68c0\u67e5\u9879\u76ee\u4f9d\u8d56\u4e2d\u7684\u5df2\u77e5\u5b89\u5168\u6f0f\u6d1e\u3002<\/li>\n\n\n\n<li><strong>\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177<\/strong>: \u5982<a href=\"https:\/\/www.checkmarx.com\/\">Checkmarx<\/a>\u6216<a href=\"https:\/\/www.microfocus.com\/documentation\/fortify-static-code-analyzer\/\">Fortify<\/a>, \u7528\u4e8e\u626b\u63cf\u6e90\u4ee3\u7801\u4ee5\u8bc6\u522b\u6f5c\u5728\u7684\u5b89\u5168\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u52a8\u6001\u626b\u63cf\u5de5\u5177<\/strong>: \u5982<a href=\"https:\/\/www.zaproxy.org\/\">OWASP ZAP<\/a>\u6216<a href=\"https:\/\/portswigger.net\/burp\">Burp Suite<\/a>, \u7528\u4e8e\u5728\u8fd0\u884c\u65f6\u68c0\u6d4b\u5e94\u7528\u7684\u5b89\u5168\u6f0f\u6d1e\u3002<\/li>\n\n\n\n<li><strong>\u5bb9\u5668\u5b89\u5168\u5de5\u5177<\/strong>: \u5982<a href=\"https:\/\/www.aquasec.com\/\">Aqua<\/a>\u6216<a href=\"https:\/\/sysdig.com\/\">Sysdig<\/a>, \u7528\u4e8e\u626b\u63cfDocker\u5bb9\u5668\u548cKubernetes\u96c6\u7fa4\u7684\u5b89\u5168\u914d\u7f6e\u3002<\/li>\n\n\n\n<li><strong>\u6301\u7eed\u76d1\u63a7\u548c\u54cd\u5e94\u5de5\u5177<\/strong>: \u5982<a href=\"https:\/\/www.splunk.com\/\">Splunk<\/a>\u6216<a href=\"https:\/\/www.datadoghq.com\/\">Datadog<\/a>, \u7528\u4e8e\u5b9e\u65f6\u76d1\u63a7\u5e94\u7528\u5e76\u5728\u68c0\u6d4b\u5230\u5b89\u5168\u4e8b\u4ef6\u65f6\u53d1\u51fa\u8b66\u62a5\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<p>\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528<code>npm audit<\/code>\u548c<code>OWASP ZAP<\/code>\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u8ba1\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528npm audit\u68c0\u67e5\u4f9d\u8d56<\/h4>\n\n\n\n<p>\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<p>bashCopy code<\/p>\n\n\n\n<p><code>npm audit<\/code><\/p>\n\n\n\n<p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u62a5\u544a\uff0c\u5217\u51fa\u6240\u6709\u5df2\u77e5\u7684\u5b89\u5168\u95ee\u9898\uff0c\u5e76\u63d0\u4f9b\u4fee\u590d\u5efa\u8bae\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528OWASP ZAP\u8fdb\u884c\u52a8\u6001\u626b\u63cf<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6253\u5f00OWASP ZAP\u5e94\u7528\u3002<\/li>\n\n\n\n<li>\u8f93\u5165\u4f60\u7684\u5e94\u7528URL\u3002<\/li>\n\n\n\n<li>\u70b9\u51fb\u201cAttack\u201d\u5f00\u59cb\u626b\u63cf\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u626b\u63cf\u5b8c\u6210\u540e\uff0cOWASP ZAP\u4f1a\u63d0\u4f9b\u4e00\u4e2a\u8be6\u7ec6\u7684\u62a5\u544a\uff0c\u5305\u62ec\u8bc6\u522b\u7684\u5b89\u5168\u6f0f\u6d1e\u548c\u4fee\u590d\u5efa\u8bae\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u66f4\u5168\u9762\u548c\u7cfb\u7edf\u5730\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u8ba1\uff0c\u4ece\u800c\u66f4\u6709\u6548\u5730\u8bc6\u522b\u548c\u4fee\u590d\u5b89\u5168\u6f0f\u6d1e\u3002\u8fd9\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7684\u5b89\u5168\u6027\uff0c\u8fd8\u53ef\u4ee5\u6ee1\u8db3\u5404\u79cd\u5408\u89c4\u8981\u6c42\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63a2\u8ba8\u66f4\u591a\u5173\u4e8e\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5\u7684\u9ad8\u7ea7\u4e3b\u9898\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e5d\u7ae0\uff1a\u603b\u7ed3\u548c\u672a\u6765\u5c55\u671b\u76849.1 \u672c\u6559\u7a0b\u603b\u7ed3ChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e5d\u7ae0\uff1a\u603b\u7ed3\u548c\u672a\u6765\u5c55\u671b<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">9.1 \u672c\u6559\u7a0b\u603b\u7ed3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u4e86Angular\u524d\u7aef\u5f00\u53d1\u4e2d\u7684\u5b89\u5168\u548c\u8ba4\u8bc1\u65b9\u9762\u3002\u4ece\u57fa\u7840\u7684DOM\u548cHTTP\u5b89\u5168\uff0c\u5230\u9ad8\u7ea7\u7684\u8ba4\u8bc1\u673a\u5236\u5982JWT\u548cOAuth 2.0\uff0c\u518d\u5230\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406\uff0c\u6211\u4eec\u5c3d\u91cf\u5168\u9762\u5730\u8986\u76d6\u4e86\u8fd9\u4e00\u4e3b\u9898\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5f15\u8a00<\/strong>: \u6211\u4eec\u8ba8\u8bba\u4e86\u4e3a\u4ec0\u4e48\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u5728\u73b0\u4ee3Web\u5e94\u7528\u4e2d\u662f\u5982\u6b64\u91cd\u8981\uff0c\u4ee5\u53caAngular\u5728\u8fd9\u65b9\u9762\u7684\u4f18\u52bf\u548c\u6311\u6218\u3002<\/li>\n\n\n\n<li><strong>Angular\u5b89\u5168\u57fa\u7840<\/strong>: \u6211\u4eec\u63a2\u8ba8\u4e86DOM\u7684\u5b89\u5168\u6027\u3001HTTP\u5b89\u5168\u3001\u8def\u7531\u5b89\u5168\u4ee5\u53caAngular\u7684\u5b89\u5168\u6700\u4f73\u5b9e\u8df5\u3002<\/li>\n\n\n\n<li><strong>\u8ba4\u8bc1\u57fa\u7840<\/strong>: \u6211\u4eec\u89e3\u91ca\u4e86\u4ec0\u4e48\u662f\u8ba4\u8bc1\uff0c\u4e0d\u540c\u7c7b\u578b\u7684\u8ba4\u8bc1\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Angular\u4e2d\u5b9e\u73b0\u8ba4\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528JWT\u8fdb\u884c\u8ba4\u8bc1<\/strong>: \u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86JWT\uff08JSON Web Token\uff09\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Angular\u4e2d\u4f7f\u7528JWT\u8fdb\u884c\u5b89\u5168\u8ba4\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528OAuth 2.0\u8fdb\u884c\u8ba4\u8bc1<\/strong>: \u6211\u4eec\u63a2\u8ba8\u4e86OAuth 2.0\u7684\u5de5\u4f5c\u673a\u5236\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Angular\u5e94\u7528\u4e2d\u5b9e\u73b0\u5b83\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1<\/strong>: \u6211\u4eec\u4ecb\u7ecd\u4e86Firebase\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528Firebase\u8fdb\u884c\u8ba4\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u89d2\u8272\u548c\u6743\u9650\u7ba1\u7406<\/strong>: \u6211\u4eec\u8ba8\u8bba\u4e86\u89d2\u8272\u548c\u6743\u9650\u7684\u91cd\u8981\u6027\uff0c\u4ee5\u53ca\u5728Angular\u4e2d\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u89d2\u8272\u548c\u6743\u9650\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5<\/strong>: \u6700\u540e\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86\u5982\u4f55\u8fdb\u884c\u5b89\u5168\u6027\u5ba1\u8ba1\u548c\u6d4b\u8bd5\uff0c\u4ee5\u786e\u4fdd\u5e94\u7528\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u672a\u6765\u5c55\u671b<\/h3>\n\n\n\n<p>\u867d\u7136\u672c\u6559\u7a0b\u5c3d\u91cf\u5168\u9762\uff0c\u4f46\u5b89\u5168\u548c\u8ba4\u8bc1\u662f\u4e00\u4e2a\u4e0d\u65ad\u53d1\u5c55\u7684\u9886\u57df\u3002\u968f\u7740\u65b0\u7684\u5b89\u5168\u5a01\u80c1\u548c\u89e3\u51b3\u65b9\u6848\u7684\u51fa\u73b0\uff0c\u6301\u7eed\u5b66\u4e60\u548c\u66f4\u65b0\u77e5\u8bc6\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p>\u901a\u8fc7\u672c\u6559\u7a0b\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u83b7\u5f97\u4e86\u4e00\u5957\u5168\u9762\u7684\u5de5\u5177\u548c\u77e5\u8bc6\uff0c\u4ee5\u5e2e\u52a9\u4f60\u6784\u5efa\u66f4\u5b89\u5168\u3001\u66f4\u53ef\u9760\u7684Angular\u5e94\u7528\u3002\u4f46\u8bf7\u8bb0\u4f4f\uff0c\u5b89\u5168\u6027\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u4f60\u4e0d\u65ad\u5730\u5b66\u4e60\u3001\u6d4b\u8bd5\u548c\u6539\u8fdb\u3002<\/p>\n\n\n\n<p>\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u63d0\u4f9b\u4e00\u4e9b\u8fdb\u4e00\u6b65\u5b66\u4e60\u548c\u6df1\u5165\u7814\u7a76\u7684\u8d44\u6e90\u548c\u5efa\u8bae\u3002\u5e0c\u671b\u4f60\u80fd\u4ece\u672c\u6559\u7a0b\u4e2d\u53d7\u76ca\uff0c\u5e76\u5728\u4f60\u7684Angular\u5f00\u53d1\u65c5\u7a0b\u4e2d\u5e94\u7528\u8fd9\u4e9b\u77e5\u8bc6\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u4e5d\u7ae0\uff1a\u603b\u7ed3\u548c\u672a\u6765\u5c55\u671b\u76849.2 \u672a\u6765\u7684\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u8d8b\u52bfChatGPT<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u4e5d\u7ae0\uff1a\u603b\u7ed3\u548c\u672a\u6765\u5c55\u671b<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">9.2 \u672a\u6765\u7684\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u8d8b\u52bf<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u968f\u7740\u6280\u672f\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u9886\u57df\u4e5f\u5728\u4e0d\u65ad\u6f14\u53d8\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u503c\u5f97\u5173\u6ce8\u7684\u672a\u6765\u8d8b\u52bf\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u96f6\u4fe1\u4efb\u67b6\u6784<\/strong>: \u4f20\u7edf\u7684\u5b89\u5168\u6a21\u578b\u901a\u5e38\u57fa\u4e8e\u7f51\u7edc\u8fb9\u754c\uff0c\u4f46\u96f6\u4fe1\u4efb\u67b6\u6784\u4e0d\u518d\u4f9d\u8d56\u4e8e\u7f51\u7edc\u4f4d\u7f6e\uff0c\u800c\u662f\u4f9d\u8d56\u4e8e\u4e25\u683c\u7684\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u3002<\/li>\n\n\n\n<li><strong>\u591a\u56e0\u7d20\u8ba4\u8bc1 (MFA)<\/strong>: \u5355\u4e00\u7684\u5bc6\u7801\u8ba4\u8bc1\u5df2\u7ecf\u4e0d\u518d\u8db3\u591f\u5b89\u5168\uff0c\u591a\u56e0\u7d20\u8ba4\u8bc1\u5c06\u66f4\u52a0\u666e\u904d\u3002<\/li>\n\n\n\n<li><strong>AI\u548c\u673a\u5668\u5b66\u4e60<\/strong>: \u7528\u4e8e\u68c0\u6d4b\u5f02\u5e38\u884c\u4e3a\u548c\u6f5c\u5728\u5a01\u80c1\u7684\u667a\u80fd\u7cfb\u7edf\u5c06\u8d8a\u6765\u8d8a\u591a\u3002<\/li>\n\n\n\n<li><strong>\u9690\u79c1\u4fdd\u62a4<\/strong>: \u968f\u7740GDPR\u548c\u5176\u4ed6\u9690\u79c1\u6cd5\u89c4\u7684\u5b9e\u65bd\uff0c\u5982\u4f55\u5728\u8ba4\u8bc1\u8fc7\u7a0b\u4e2d\u4fdd\u62a4\u7528\u6237\u9690\u79c1\u5c06\u6210\u4e3a\u4e00\u4e2a\u91cd\u8981\u8bae\u9898\u3002<\/li>\n\n\n\n<li><strong>\u5206\u5e03\u5f0f\u8eab\u4efd\u548c\u533a\u5757\u94fe<\/strong>: \u8fd9\u4e9b\u65b0\u6280\u672f\u6709\u53ef\u80fd\u6539\u53d8\u6211\u4eec\u5bf9\u8eab\u4efd\u9a8c\u8bc1\u548c\u6388\u6743\u7684\u4f20\u7edf\u89c2\u5ff5\u3002<\/li>\n\n\n\n<li><strong>API\u5b89\u5168<\/strong>: \u968f\u7740\u5fae\u670d\u52a1\u548c\u65e0\u670d\u52a1\u5668\u67b6\u6784\u7684\u5174\u8d77\uff0cAPI\u5b89\u5168\u5c06\u6210\u4e3a\u4e00\u4e2a\u8d8a\u6765\u8d8a\u91cd\u8981\u7684\u7126\u70b9\u3002<\/li>\n\n\n\n<li><strong>\u6301\u7eed\u8ba4\u8bc1<\/strong>: \u800c\u4e0d\u662f\u4e00\u6b21\u6027\u9a8c\u8bc1\uff0c\u6301\u7eed\u6216\u81ea\u9002\u5e94\u8ba4\u8bc1\u5c06\u6839\u636e\u7528\u6237\u884c\u4e3a\u548c\u4e0a\u4e0b\u6587\u4fe1\u606f\u5b9e\u65f6\u66f4\u65b0\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u672a\u6765\u5c55\u671b<\/h3>\n\n\n\n<p>\u8fd9\u4e9b\u8d8b\u52bf\u4e0d\u4ec5\u5f71\u54cd\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u7684\u6280\u672f\u5c42\u9762\uff0c\u8fd8\u5f71\u54cd\u6cd5\u89c4\u3001\u5408\u89c4\u6027\u548c\u7528\u6237\u671f\u671b\u3002\u56e0\u6b64\uff0c\u6301\u7eed\u5173\u6ce8\u8fd9\u4e9b\u8d8b\u52bf\uff0c\u5e76\u51c6\u5907\u76f8\u5e94\u5730\u8c03\u6574\u4f60\u7684\u5b89\u5168\u7b56\u7565\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p>\u672a\u6765\u7684\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u9886\u57df\u5c06\u66f4\u52a0\u590d\u6742\u548c\u591a\u6837\u5316\uff0c\u4f46\u4e5f\u5145\u6ee1\u4e86\u673a\u4f1a\u3002\u901a\u8fc7\u4e86\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b0\u8d8b\u52bf\uff0c\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u51c6\u5907\u81ea\u5df1\uff0c\u4ee5\u5e94\u5bf9\u4e0d\u65ad\u53d8\u5316\u7684\u5b89\u5168\u6311\u6218\u3002<\/p>\n\n\n\n<p>\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u63d0\u4f9b\u8fdb\u4e00\u6b65\u5b66\u4e60\u548c\u6df1\u5165\u7814\u7a76\u7684\u8d44\u6e90\u548c\u5efa\u8bae\u3002\u5e0c\u671b\u4f60\u80fd\u4ece\u672c\u6559\u7a0b\u4e2d\u53d7\u76ca\uff0c\u5e76\u5728\u4f60\u7684Angular\u5f00\u53d1\u65c5\u7a0b\u4e2d\u5e94\u7528\u8fd9\u4e9b\u77e5\u8bc6\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/a\/ACg8ocLh4B_kew_Ugyfq9egjobpny7AWdKDF1KOH64Elebx0ZhM=s96-c\" alt=\"User\"\/><\/figure>\n\n\n\n<p>\u8bf7\u8be6\u7ec6\u89e3\u91ca\u7b2c\u5341\u7ae0\uff1a\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u8d44\u6e90\u768410.1 \u6587\u6863\u548c\u6559\u7a0bChatGPT<\/p>\n\n\n\n<p>2 \/ 2<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u7b2c\u5341\u7ae0\uff1a\u8fdb\u4e00\u6b65\u7684\u5b66\u4e60\u8d44\u6e90<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">10.1 \u6587\u6863\u548c\u6559\u7a0b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u63cf\u8ff0<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u8fdb\u4e00\u6b65\u6df1\u5165\u4e86\u89e3Angular\u7684\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u975e\u5e38\u6709\u7528\u7684\u6587\u6863\u548c\u6559\u7a0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Angular\u5b98\u65b9\u6587\u6863<\/strong>: Angular\u7684<a href=\"https:\/\/angular.io\/docs\">\u5b98\u65b9\u6587\u6863<\/a>\u6709\u4e00\u4e2a\u4e13\u95e8\u7684\u5b89\u5168\u90e8\u5206\uff0c\u6db5\u76d6\u4e86XSS\u3001XSRF\u7b49\u4e3b\u9898\u3002<\/li>\n\n\n\n<li><strong>OWASP<\/strong>: <a href=\"https:\/\/www.owasp.org\/\">OWASP\u7f51\u7ad9<\/a>\u63d0\u4f9b\u4e86\u5927\u91cf\u5173\u4e8eWeb\u5b89\u5168\u7684\u8d44\u6e90\uff0c\u5305\u62ec\u9876\u7ea7\u7684\u5b89\u5168\u5a01\u80c1\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/li>\n\n\n\n<li><strong>Auth0\u535a\u5ba2<\/strong>: <a href=\"https:\/\/auth0.com\/blog\/\">Auth0\u535a\u5ba2<\/a>\u7ecf\u5e38\u53d1\u5e03\u5173\u4e8e\u8ba4\u8bc1\u548c\u6388\u6743\u7684\u9ad8\u8d28\u91cf\u6587\u7ae0\u3002<\/li>\n\n\n\n<li><strong>Pluralsight\u548cUdemy\u8bfe\u7a0b<\/strong>: \u8fd9\u4e9b\u5728\u7ebf\u6559\u80b2\u5e73\u53f0\u63d0\u4f9b\u4e86\u591a\u4e2a\u5173\u4e8eAngular\u5b89\u5168\u6027\u7684\u8bfe\u7a0b\u3002<\/li>\n\n\n\n<li><strong>YouTube<\/strong>: \u6709\u5f88\u591a\u4f18\u8d28\u7684YouTube\u9891\u9053\uff0c\u5982Academind\u548cTraversy Media\uff0c\u63d0\u4f9b\u4e86\u5173\u4e8eAngular\u5b89\u5168\u6027\u7684\u514d\u8d39\u6559\u7a0b\u3002<\/li>\n\n\n\n<li><strong>Stack Overflow<\/strong>: \u5982\u679c\u4f60\u9047\u5230\u5177\u4f53\u95ee\u9898\uff0c<a href=\"https:\/\/stackoverflow.com\/\">Stack Overflow<\/a>\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u8d44\u6e90\uff0c\u4f60\u53ef\u4ee5\u627e\u5230\u4e0eAngular\u5b89\u5168\u6027\u76f8\u5173\u7684\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n\n\n\n<li><strong>GitHub Repos<\/strong>: \u8bb8\u591a\u5f00\u6e90\u9879\u76ee\u548c\u793a\u4f8b\u4ee3\u7801\u5e93\u4e13\u95e8\u9488\u5bf9Angular\u5b89\u5168\u6027\uff0c\u8fd9\u4e9b\u662f\u5b9e\u8df5\u548c\u5b66\u4e60\u7684\u597d\u8d44\u6e90\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p>\u8fd9\u4e9b\u6587\u6863\u548c\u6559\u7a0b\u53ef\u4ee5\u4e3a\u4f60\u63d0\u4f9b\u66f4\u591a\u7684\u7406\u8bba\u77e5\u8bc6\u548c\u5b9e\u8df5\u7ecf\u9a8c\uff0c\u5e2e\u52a9\u4f60\u66f4\u5168\u9762\u5730\u7406\u89e3\u548c\u5e94\u7528Angular\u7684\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u673a\u5236\u3002\u65e0\u8bba\u4f60\u662f\u65b0\u624b\u8fd8\u662f\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0c\u8fd9\u4e9b\u8d44\u6e90\u90fd\u5c06\u662f\u4f60\u4e0d\u65ad\u5b66\u4e60\u548c\u63d0\u9ad8\u7684\u5b9d\u8d35\u8d44\u6599\u3002<\/p>\n\n\n\n<p>\u5728\u63a5\u4e0b\u6765\u7684\u5c0f\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u63d0\u4f9b\u66f4\u591a\u7684\u5b66\u4e60\u8d44\u6e90\uff0c\u5305\u62ec\u4e66\u7c4d\u3001\u5de5\u5177\u548c\u793e\u7fa4\uff0c\u4ee5\u52a9\u4f60\u66f4\u6df1\u5165\u5730\u638c\u63e1Angular\u5b89\u5168\u6027\u548c\u8ba4\u8bc1\u7684\u5404\u4e2a\u65b9\u9762\u3002\u5e0c\u671b\u8fd9\u4e9b\u8d44\u6e90\u80fd\u5e2e\u52a9\u4f60\u5728Angular\u5b89\u5168\u6027\u65b9\u9762\u8fbe\u5230\u4e00\u4e2a\u65b0\u7684\u6c34\u5e73\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular\u524d\u7aef\u5f00\u53d1\u5b89\u5168\u548c\u8ba4\u8bc1\u6559\u7a0b\u5927\u7eb2 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-3016","post","type-post","status-publish","format-standard","hentry","category-material"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts\/3016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3016"}],"version-history":[{"count":1,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts\/3016\/revisions"}],"predecessor-version":[{"id":3017,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=\/wp\/v2\/posts\/3016\/revisions\/3017"}],"wp:attachment":[{"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/edu.ujhb.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}