{"id":174013,"date":"2020-03-18T08:48:23","date_gmt":"2020-03-18T00:48:23","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=174013"},"modified":"2020-03-03T16:50:37","modified_gmt":"2020-03-03T08:50:37","slug":"authentication-and-authorization","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/authentication-and-authorization.html","title":{"rendered":"\u8ba4\u8bc1\u6388\u6743\u95ee\u9898\u6982\u89c8"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u76f8\u4fe1\u5f88\u591a\u4eba\u5bf9\u8ba4\u8bc1\u6388\u6743\u65b9\u9762\u90fd\u4e0d\u662f\u7279\u522b\u4e86\u89e3\uff0c\u641e\u4e0d\u6e05Session\u8ba4\u8bc1\u3001JWT\u4ee5\u53ca Cookie \u8fd9\u4e9b\u6982\u5ff5\u3002\u6240\u4ee5\uff0c\u6211\u6839\u636e\u65e5\u5e38\u5bf9\u8fd9\u90e8\u5206\u5b66\u4e60\u5df2\u7ecf\u5728\u9879\u76ee\u4e2d\u7684\u5b9e\u9645\u8fd0\u7528\u603b\u7ed3\u4e86\u8fd98 \u4e2a\u76f8\u5173\u7684\u95ee\u9898\u5e76\u4e14\u9644\u4e0a\u4e86\u8be6\u7ec6\u7684\u56de\u7b54<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6240\u6709\u95ee\u9898\u5982\u4e0b\uff0c\u5f00\u59cb\u770b\u7b54\u6848\u4e4b\u524d\u4e0d\u59a8\u81ea\u5df1\u6d4b\u9a8c\u4e00\u4e0b\u81ea\u5df1\u7a76\u7adf\u80fd\u56de\u7b54\u51e0\u4e2a\uff1a<\/p>\n

    \u8ba4\u8bc1 (Authentication) \u548c\u6388\u6743 (Authorization)\u7684\u533a\u522b\u662f\u4ec0\u4e48?<\/ol>\n
      \u4ec0\u4e48\u662fCookie ? Cookie\u7684\u4f5c\u7528\u662f\u4ec0\u4e48?\u5982\u4f55\u5728\u670d\u52a1\u7aef\u4f7f\u7528 Cookie ?<\/ol>\n
        Cookie \u548c Session \u6709\u4ec0\u4e48\u533a\u522b?\u5982\u4f55\u4f7f\u7528Session\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1?<\/ol>\n
          \u5982\u679c\u6ca1\u6709Cookie\u7684\u8bddSession\u8fd8\u80fd\u7528\u5417?<\/ol>\n
            \u4e3a\u4ec0\u4e48Cookie \u65e0\u6cd5\u9632\u6b62CSRF\u653b\u51fb\uff0c\u800ctoken\u53ef\u4ee5?<\/ol>\n
              \u4ec0\u4e48\u662f Token?\u4ec0\u4e48\u662f JWT?\u5982\u4f55\u57fa\u4e8eToken\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1?<\/ol>\n
                \u4ec0\u4e48\u662fOAuth 2.0?<\/ol>\n
                  \u4ec0\u4e48\u662f SSO?<\/ol>\n
                  1. \u8ba4\u8bc1 (Authentication) \u548c\u6388\u6743 (Authorization)\u7684\u533a\u522b\u662f\u4ec0\u4e48?<\/strong><\/div>\n

                  \u9996\u5148\u5148\u4ece\u8bfb\u97f3\u4e0a\u6765\u8ba4\u8bc6\u8fd9\u4e24\u4e2a\u540d\u8bcd\uff0c\u5f88\u591a\u4eba\u90fd\u4f1a\u628a\u5b83\u4fe9\u7684\u8bfb\u97f3\u641e\u6df7\uff0c\u6240\u4ee5\u6211\u5efa\u8bae\u4f60\u5148\u5148\u53bb\u67e5\u4e00\u67e5\u8fd9\u4e24\u4e2a\u5355\u8bcd\u5230\u5e95\u8be5\u600e\u4e48\u8bfb\uff0c\u4ed6\u4eec\u7684\u5177\u4f53\u542b\u4e49\u662f\u4ec0\u4e48\u3002<\/p>\n

                  \u8bf4\u7b80\u5355\u70b9\u5c31\u662f\uff1a<\/p>\n

                  \u8ba4\u8bc1 (Authentication)\uff1a \u4f60\u662f\u8c01\u3002
                  \n\"\"<\/p>\n

                  \u6388\u6743 (Authorization)\uff1a \u4f60\u6709\u6743\u9650\u5e72\u4ec0\u4e48\u3002<\/p>\n

                  \"\"<\/p>\n

                  \u7a0d\u5fae\u6b63\u5f0f\u70b9(\u5570\u55e6\u70b9)\u7684\u8bf4\u6cd5\u5c31\u662f\uff1a<\/p>\n

                    Authentication(\u8ba4\u8bc1) \u662f\u9a8c\u8bc1\u60a8\u7684\u8eab\u4efd\u7684\u51ed\u636e(\u4f8b\u5982\u7528\u6237\u540d\/\u7528\u6237ID\u548c\u5bc6\u7801)\uff0c\u901a\u8fc7\u8fd9\u4e2a\u51ed\u636e\uff0c\u7cfb\u7edf\u5f97\u4ee5\u77e5\u9053\u4f60\u5c31\u662f\u4f60\uff0c\u4e5f\u5c31\u662f\u8bf4\u7cfb\u7edf\u5b58\u5728\u4f60\u8fd9\u4e2a\u7528\u6237\u3002\u6240\u4ee5\uff0cAuthentication \u88ab\u79f0\u4e3a\u8eab\u4efd\/\u7528\u6237\u9a8c\u8bc1\u3002<\/ol>\n
                      Authorization(\u6388\u6743) \u53d1\u751f\u5728 Authentication(\u8ba4\u8bc1) \u4e4b\u540e\u3002\u6388\u6743\u561b\uff0c\u5149\u770b\u610f\u601d\u5927\u5bb6\u5e94\u8be5\u5c31\u660e\u767d\uff0c\u5b83\u4e3b\u8981\u638c\u7ba1\u6211\u4eec\u8bbf\u95ee\u7cfb\u7edf\u7684\u6743\u9650\u3002\u6bd4\u5982\u6709\u4e9b\u7279\u5b9a\u8d44\u6e90\u53ea\u80fd\u5177\u6709\u7279\u5b9a\u6743\u9650\u7684\u4eba\u624d\u80fd\u8bbf\u95ee\u6bd4\u5982admin\uff0c\u6709\u4e9b\u5bf9\u7cfb\u7edf\u8d44\u6e90\u64cd\u4f5c\u6bd4\u5982\u5220\u9664\u3001\u6dfb\u52a0\u3001\u66f4\u65b0\u53ea\u80fd\u7279\u5b9a\u4eba\u624d\u5177\u6709\u3002<\/ol>\n

                      \u8fd9\u4e24\u4e2a\u4e00\u822c\u5728\u6211\u4eec\u7684\u7cfb\u7edf\u4e2d\u88ab\u7ed3\u5408\u5728\u4e00\u8d77\u4f7f\u7528\uff0c\u76ee\u7684\u5c31\u662f\u4e3a\u4e86\u4fdd\u62a4\u6211\u4eec\u7cfb\u7edf\u7684\u5b89\u5168\u6027\u3002<\/p>\n

                      2. Cookie\u662f\u4ec0\u4e48?&\u6709\u4ec0\u4e48\u4f5c\u7528\u4ee5\u53ca\u5982\u4f55\u5728\u670d\u52a1\u7aef\u4f7f\u7528Cookie ?<\/strong><\/div>\n

                      \"\"<\/p>\n

                      2.1 \u4ec0\u4e48\u662fCookie ? Cookie\u7684\u4f5c\u7528\u662f\u4ec0\u4e48?<\/strong><\/span><\/div>\n

                      Cookie \u548c Session\u90fd\u662f\u7528\u6765\u8ddf\u8e2a\u6d4f\u89c8\u5668\u7528\u6237\u8eab\u4efd\u7684\u4f1a\u8bdd\u65b9\u5f0f\uff0c\u4f46\u662f\u4e24\u8005\u7684\u5e94\u7528\u573a\u666f\u4e0d\u592a\u4e00\u6837\u3002<\/p>\n

                      \u7ef4\u57fa\u767e\u79d1\u662f\u8fd9\u6837\u5b9a\u4e49 Cookie \u7684\uff1aCookies\u662f\u67d0\u4e9b\u7f51\u7ad9\u4e3a\u4e86\u8fa8\u522b\u7528\u6237\u8eab\u4efd\u800c\u50a8\u5b58\u5728\u7528\u6237\u672c\u5730\u7ec8\u7aef\u4e0a\u7684\u6570\u636e(\u901a\u5e38\u7ecf\u8fc7\u52a0\u5bc6)\u3002\u7b80\u5355\u6765\u8bf4\uff1a Cookie \u5b58\u653e\u5728\u5ba2\u6237\u7aef\uff0c\u4e00\u822c\u7528\u6765\u4fdd\u5b58\u7528\u6237\u4fe1\u606f\u3002<\/p>\n

                      \u4e0b\u9762\u662f Cookie \u7684\u4e00\u4e9b\u5e94\u7528\u6848\u4f8b\uff1a<\/p>\n

                        \u6211\u4eec\u5728 Cookie \u4e2d\u4fdd\u5b58\u5df2\u7ecf\u767b\u5f55\u8fc7\u7684\u7528\u6237\u4fe1\u606f\uff0c\u4e0b\u6b21\u8bbf\u95ee\u7f51\u7ad9\u7684\u65f6\u5019\u9875\u9762\u53ef\u4ee5\u81ea\u52a8\u5e2e\u4f60\u767b\u5f55\u7684\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\u7ed9\u586b\u4e86\u3002\u9664\u6b64\u4e4b\u5916\uff0cCookie \u8fd8\u80fd\u4fdd\u5b58\u7528\u6237\u9996\u9009\u9879\uff0c\u4e3b\u9898\u548c\u5176\u4ed6\u8bbe\u7f6e\u4fe1\u606f\u3002<\/ol>\n
                          \u4f7f\u7528Cookie \u4fdd\u5b58 session \u6216\u8005 token \uff0c\u5411\u540e\u7aef\u53d1\u9001\u8bf7\u6c42\u7684\u65f6\u5019\u5e26\u4e0a Cookie\uff0c\u8fd9\u6837\u540e\u7aef\u5c31\u80fd\u53d6\u5230session\u6216\u8005token\u4e86\u3002\u8fd9\u6837\u5c31\u80fd\u8bb0\u5f55\u7528\u6237\u5f53\u524d\u7684\u72b6\u6001\u4e86\uff0c\u56e0\u4e3a HTTP \u534f\u8bae\u662f\u65e0\u72b6\u6001\u7684\u3002<\/ol>\n
                            Cookie \u8fd8\u53ef\u4ee5\u7528\u6765\u8bb0\u5f55\u548c\u5206\u6790\u7528\u6237\u884c\u4e3a\u3002\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4f60\u5728\u7f51\u4e0a\u8d2d\u7269\u7684\u65f6\u5019\uff0c\u56e0\u4e3aHTTP\u534f\u8bae\u662f\u6ca1\u6709\u72b6\u6001\u7684\uff0c\u5982\u679c\u670d\u52a1\u5668\u60f3\u8981\u83b7\u53d6\u4f60\u5728\u67d0\u4e2a\u9875\u9762\u7684\u505c\u7559\u72b6\u6001\u6216\u8005\u770b\u4e86\u54ea\u4e9b\u5546\u54c1\uff0c\u4e00\u79cd\u5e38\u7528\u7684\u5b9e\u73b0\u65b9\u5f0f\u5c31\u662f\u5c06\u8fd9\u4e9b\u4fe1\u606f\u5b58\u653e\u5728Cookie<\/ol>\n
                            2.2 \u5982\u4f55\u5728\u670d\u52a1\u7aef\u4f7f\u7528 Cookie \u5462?<\/strong><\/span><\/div>\n

                            \u8fd9\u90e8\u5206\u5185\u5bb9\u53c2\u8003\uff1ahttps:\/\/attacomsian.com\/blog\/cookies-spring-boot\uff0c\u66f4\u591a\u5982\u4f55\u5728Spring Boot\u4e2d\u4f7f\u7528Cookie \u7684\u5185\u5bb9\u53ef\u4ee5\u67e5\u770b\u8fd9\u7bc7\u6587\u7ae0\u3002<\/p>\n

                            1)\u8bbe\u7f6ecookie\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef<\/p>\n

                            @GetMapping(\"\/change-username\") \r\npublic String setCookie(HttpServletResponse response) { \r\n    \/\/ \u521b\u5efa\u4e00\u4e2a cookie \r\n    Cookie cookie = new Cookie(\"username\", \"Jovan\"); \r\n    \/\/\u8bbe\u7f6e cookie\u8fc7\u671f\u65f6\u95f4 \r\n    cookie.setMaxAge(7 * 24 * 60 * 60); \/\/ expires in 7 days \r\n    \/\/\u6dfb\u52a0\u5230 response \u4e2d \r\n    response.addCookie(cookie); \r\n \r\n    return\"Username is changed!\"; \r\n}<\/pre>\n

                            2) \u4f7f\u7528Spring\u6846\u67b6\u63d0\u4f9b\u7684@CookieValue\u6ce8\u89e3\u83b7\u53d6\u7279\u5b9a\u7684 cookie\u7684\u503c<\/p>\n

                            @GetMapping(\"\/\") \r\npublic String readCookie(@CookieValue(value = \"username\", defaultValue = \"Atta\") String username) { \r\n    return\"Hey! My username is \" + username; \r\n}<\/pre>\n

                            3) \u8bfb\u53d6\u6240\u6709\u7684 Cookie \u503c<\/p>\n

                            @GetMapping(\"\/all-cookies\") \r\npublic String readAllCookies(HttpServletRequest request) { \r\n \r\n    Cookie[] cookies = request.getCookies(); \r\n    if (cookies != null) { \r\n        return Arrays.stream(cookies) \r\n                .map(c -> c.getName() + \"=\" + c.getValue()).collect(Collectors.joining(\", \")); \r\n    } \r\n \r\n    return\"No cookies\"; \r\n}<\/pre>\n
                            3. Cookie \u548c Session \u6709\u4ec0\u4e48\u533a\u522b?\u5982\u4f55\u4f7f\u7528Session\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1?<\/strong><\/div>\n

                            Session \u7684\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u901a\u8fc7\u670d\u52a1\u7aef\u8bb0\u5f55\u7528\u6237\u7684\u72b6\u6001\u3002 \u5178\u578b\u7684\u573a\u666f\u662f\u8d2d\u7269\u8f66\uff0c\u5f53\u4f60\u8981\u6dfb\u52a0\u5546\u54c1\u5230\u8d2d\u7269\u8f66\u7684\u65f6\u5019\uff0c\u7cfb\u7edf\u4e0d\u77e5\u9053\u662f\u54ea\u4e2a\u7528\u6237\u64cd\u4f5c\u7684\uff0c\u56e0\u4e3a HTTP \u534f\u8bae\u662f\u65e0\u72b6\u6001\u7684\u3002\u670d\u52a1\u7aef\u7ed9\u7279\u5b9a\u7684\u7528\u6237\u521b\u5efa\u7279\u5b9a\u7684 Session \u4e4b\u540e\u5c31\u53ef\u4ee5\u6807\u8bc6\u8fd9\u4e2a\u7528\u6237\u5e76\u4e14\u8ddf\u8e2a\u8fd9\u4e2a\u7528\u6237\u4e86\u3002<\/p>\n

                            Cookie \u6570\u636e\u4fdd\u5b58\u5728\u5ba2\u6237\u7aef(\u6d4f\u89c8\u5668\u7aef)\uff0cSession \u6570\u636e\u4fdd\u5b58\u5728\u670d\u52a1\u5668\u7aef\u3002\u76f8\u5bf9\u6765\u8bf4 Session \u5b89\u5168\u6027\u66f4\u9ad8\u3002\u5982\u679c\u4f7f\u7528 Cookie \u7684\u4e00\u4e9b\u654f\u611f\u4fe1\u606f\u4e0d\u8981\u5199\u5165 Cookie \u4e2d\uff0c\u6700\u597d\u80fd\u5c06 Cookie \u4fe1\u606f\u52a0\u5bc6\u7136\u540e\u4f7f\u7528\u5230\u7684\u65f6\u5019\u518d\u53bb\u670d\u52a1\u5668\u7aef\u89e3\u5bc6\u3002<\/p>\n

                            \u90a3\u4e48\uff0c\u5982\u4f55\u4f7f\u7528Session\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1?<\/p>\n

                            \u5f88\u591a\u65f6\u5019\u6211\u4eec\u90fd\u662f\u901a\u8fc7 SessionID \u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u7528\u6237\uff0cSessionID \u4e00\u822c\u4f1a\u9009\u62e9\u5b58\u653e\u5728 Redis \u4e2d\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u7528\u6237\u6210\u529f\u767b\u9646\u7cfb\u7edf\uff0c\u7136\u540e\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u5177\u6709 SessionID \u7684 Cookie\uff0c\u5f53\u7528\u6237\u5411\u540e\u7aef\u53d1\u8d77\u8bf7\u6c42\u7684\u65f6\u5019\u4f1a\u628a SessionID \u5e26\u4e0a\uff0c\u8fd9\u6837\u540e\u7aef\u5c31\u77e5\u9053\u4f60\u7684\u8eab\u4efd\u72b6\u6001\u4e86\u3002\u5173\u4e8e\u8fd9\u79cd\u8ba4\u8bc1\u65b9\u5f0f\u66f4\u8be6\u7ec6\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a
                            \n\"\"
                            \nSession Based Authentication flow<\/p>\n

                              \u7528\u6237\u5411\u670d\u52a1\u5668\u53d1\u9001\u7528\u6237\u540d\u548c\u5bc6\u7801\u7528\u4e8e\u767b\u9646\u7cfb\u7edf\u3002<\/ol>\n
                                \u670d\u52a1\u5668\u9a8c\u8bc1\u901a\u8fc7\u540e\uff0c\u670d\u52a1\u5668\u4e3a\u7528\u6237\u521b\u5efa\u4e00\u4e2a Session\uff0c\u5e76\u5c06 Session\u4fe1\u606f\u5b58\u50a8 \u8d77\u6765\u3002<\/ol>\n
                                  \u670d\u52a1\u5668\u5411\u7528\u6237\u8fd4\u56de\u4e00\u4e2a SessionID\uff0c\u5199\u5165\u7528\u6237\u7684 Cookie\u3002<\/ol>\n
                                    \u5f53\u7528\u6237\u4fdd\u6301\u767b\u5f55\u72b6\u6001\u65f6\uff0cCookie \u5c06\u4e0e\u6bcf\u4e2a\u540e\u7eed\u8bf7\u6c42\u4e00\u8d77\u88ab\u53d1\u9001\u51fa\u53bb\u3002<\/ol>\n
                                      \u670d\u52a1\u5668\u53ef\u4ee5\u5c06\u5b58\u50a8\u5728 Cookie \u4e0a\u7684 Session ID \u4e0e\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u6216\u8005\u6570\u636e\u5e93\u4e2d\u7684 Session \u4fe1\u606f\u8fdb\u884c\u6bd4\u8f83\uff0c\u4ee5\u9a8c\u8bc1\u7528\u6237\u7684\u8eab\u4efd\uff0c\u8fd4\u56de\u7ed9\u7528\u6237\u5ba2\u6237\u7aef\u54cd\u5e94\u4fe1\u606f\u7684\u65f6\u5019\u4f1a\u9644\u5e26\u7528\u6237\u5f53\u524d\u7684\u72b6\u6001\u3002<\/ol>\n

                                      \u4f7f\u7528 Session \u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\u4e0b\u9762\u51e0\u4e2a\u70b9\uff1a<\/p>\n

                                        \u4f9d\u8d56Session\u7684\u5173\u952e\u4e1a\u52a1\u4e00\u5b9a\u8981\u786e\u4fdd\u5ba2\u6237\u7aef\u5f00\u542f\u4e86Cookie\u3002<\/ol>\n
                                          \u6ce8\u610fSession\u7684\u8fc7\u671f\u65f6\u95f4<\/ol>\n

                                          \u82b1\u4e86\u4e2a\u56fe\u7b80\u5355\u603b\u7ed3\u4e86\u4e00\u4e0bSession\u8ba4\u8bc1\u6d89\u53ca\u7684\u4e00\u4e9b\u4e1c\u897f\u3002
                                          \n\"\"<\/p>\n

                                          \u53e6\u5916\uff0cSpring Session\u63d0\u4f9b\u4e86\u4e00\u79cd\u8de8\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u6216\u5b9e\u4f8b\u7ba1\u7406\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f\u7684\u673a\u5236\u3002\u5982\u679c\u60f3\u8be6\u7ec6\u4e86\u89e3\u53ef\u4ee5\u67e5\u770b\u4e0b\u9762\u51e0\u7bc7\u5f88\u4e0d\u9519\u7684\u6587\u7ae0\uff1a<\/p>\n

                                            Getting Started with Spring Session<\/ol>\n
                                              Guide to Spring Session<\/ol>\n
                                                Sticky Sessions with Spring Session & Redis<\/ol>\n
                                                4.\u5982\u679c\u6ca1\u6709Cookie\u7684\u8bddSession\u8fd8\u80fd\u7528\u5417?\u8fd9\u662f\u4e00\u9053\u7ecf\u5178\u7684\u9762\u8bd5\u9898!<\/strong><\/div>\n

                                                \u4e00\u822c\u662f\u901a\u8fc7 Cookie \u6765\u4fdd\u5b58 SessionID \uff0c\u5047\u5982\u4f60\u4f7f\u7528\u4e86 Cookie \u4fdd\u5b58 SessionID\u7684\u65b9\u6848\u7684\u8bdd\uff0c \u5982\u679c\u5ba2\u6237\u7aef\u7981\u7528\u4e86Cookie\uff0c\u90a3\u4e48Seesion\u5c31\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n

                                                \u4f46\u662f\uff0c\u5e76\u4e0d\u662f\u6ca1\u6709 Cookie \u4e4b\u540e\u5c31\u4e0d\u80fd\u7528 Session \u4e86\uff0c\u6bd4\u5982\u4f60\u53ef\u4ee5\u5c06SessionID\u653e\u5728\u8bf7\u6c42\u7684 url \u91cc\u9762https:\/\/javaguide.cn\/?session_id=xxx \u3002\u8fd9\u79cd\u65b9\u6848\u7684\u8bdd\u53ef\u884c\uff0c\u4f46\u662f\u5b89\u5168\u6027\u548c\u7528\u6237\u4f53\u9a8c\u611f\u964d\u4f4e\u3002\u5f53\u7136\uff0c\u4e3a\u4e86\u4f60\u4e5f\u53ef\u4ee5\u5bf9 SessionID \u8fdb\u884c\u4e00\u6b21\u52a0\u5bc6\u4e4b\u540e\u518d\u4f20\u5165\u540e\u7aef\u3002<\/p>\n

                                                5.\u4e3a\u4ec0\u4e48Cookie \u65e0\u6cd5\u9632\u6b62CSRF\u653b\u51fb\uff0c\u800ctoken\u53ef\u4ee5?<\/strong><\/div>\n

                                                CSRF(Cross Site Request Forgery)\u4e00\u822c\u88ab\u7ffb\u8bd1\u4e3a \u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020 \u3002\u90a3\u4e48\u4ec0\u4e48\u662f \u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020 \u5462?\u8bf4\u7b80\u5355\u7528\u4f60\u7684\u8eab\u4efd\u53bb\u53d1\u9001\u4e00\u4e9b\u5bf9\u4f60\u4e0d\u53cb\u597d\u7684\u8bf7\u6c42\u3002\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n

                                                \u5c0f\u58ee\u767b\u5f55\u4e86\u67d0\u7f51\u4e0a\u94f6\u884c\uff0c\u4ed6\u6765\u5230\u4e86\u7f51\u4e0a\u94f6\u884c\u7684\u5e16\u5b50\u533a\uff0c\u770b\u5230\u4e00\u4e2a\u5e16\u5b50\u4e0b\u9762\u6709\u4e00\u4e2a\u94fe\u63a5\u5199\u7740\u201c\u79d1\u5b66\u7406\u8d22\uff0c\u5e74\u76c8\u5229\u7387\u8fc7\u4e07\u201d\uff0c\u5c0f\u58ee\u597d\u5947\u7684\u70b9\u5f00\u4e86\u8fd9\u4e2a\u94fe\u63a5\uff0c\u7ed3\u679c\u53d1\u73b0\u81ea\u5df1\u7684\u8d26\u6237\u5c11\u4e8610000\u5143\u3002\u8fd9\u662f\u8fd9\u4e48\u56de\u4e8b\u5462?\u539f\u6765\u9ed1\u5ba2\u5728\u94fe\u63a5\u4e2d\u85cf\u4e86\u4e00\u4e2a\u8bf7\u6c42\uff0c\u8fd9\u4e2a\u8bf7\u6c42\u76f4\u63a5\u5229\u7528\u5c0f\u58ee\u7684\u8eab\u4efd\u7ed9\u94f6\u884c\u53d1\u9001\u4e86\u4e00\u4e2a\u8f6c\u8d26\u8bf7\u6c42,\u4e5f\u5c31\u662f\u901a\u8fc7\u4f60\u7684 Cookie \u5411\u94f6\u884c\u53d1\u51fa\u8bf7\u6c42\u3002<\/p>\n

                                                \u4e0a\u9762\u4e5f\u63d0\u5230\u8fc7\uff0c\u8fdb\u884cSession \u8ba4\u8bc1\u7684\u65f6\u5019\uff0c\u6211\u4eec\u4e00\u822c\u4f7f\u7528 Cookie \u6765\u5b58\u50a8 SessionId,\u5f53\u6211\u4eec\u767b\u9646\u540e\u540e\u7aef\u751f\u6210\u4e00\u4e2aSessionId\u653e\u5728Cookie\u4e2d\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\uff0c\u670d\u52a1\u7aef\u901a\u8fc7Redis\u6216\u8005\u5176\u4ed6\u5b58\u50a8\u5de5\u5177\u8bb0\u5f55\u4fdd\u5b58\u7740\u8fd9\u4e2aSessionid\uff0c\u5ba2\u6237\u7aef\u767b\u5f55\u4ee5\u540e\u6bcf\u6b21\u8bf7\u6c42\u90fd\u4f1a\u5e26\u4e0a\u8fd9\u4e2aSessionId\uff0c\u670d\u52a1\u7aef\u901a\u8fc7\u8fd9\u4e2aSessionId\u6765\u6807\u793a\u4f60\u8fd9\u4e2a\u4eba\u3002\u5982\u679c\u522b\u4eba\u901a\u8fc7 cookie\u62ff\u5230\u4e86 SessionId \u540e\u5c31\u53ef\u4ee5\u4ee3\u66ff\u4f60\u7684\u8eab\u4efd\u8bbf\u95ee\u7cfb\u7edf\u4e86\u3002<\/p>\n

                                                Session \u8ba4\u8bc1\u4e2d Cookie \u4e2d\u7684 SessionId\u662f\u7531\u6d4f\u89c8\u5668\u53d1\u9001\u5230\u670d\u52a1\u7aef\u7684\uff0c\u501f\u52a9\u8fd9\u4e2a\u7279\u6027\uff0c\u653b\u51fb\u8005\u5c31\u53ef\u4ee5\u901a\u8fc7\u8ba9\u7528\u6237\u8bef\u70b9\u653b\u51fb\u94fe\u63a5\uff0c\u8fbe\u5230\u653b\u51fb\u6548\u679c\u3002<\/p>\n

                                                \u4f46\u662f\uff0c\u6211\u4eec\u4f7f\u7528 token \u7684\u8bdd\u5c31\u4e0d\u4f1a\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\uff0c\u5728\u6211\u4eec\u767b\u5f55\u6210\u529f\u83b7\u5f97 token \u4e4b\u540e\uff0c\u4e00\u822c\u4f1a\u9009\u62e9\u5b58\u653e\u5728 local storage \u4e2d\u3002\u7136\u540e\u6211\u4eec\u5728\u524d\u7aef\u901a\u8fc7\u67d0\u4e9b\u65b9\u5f0f\u4f1a\u7ed9\u6bcf\u4e2a\u53d1\u5230\u540e\u7aef\u7684\u8bf7\u6c42\u52a0\u4e0a\u8fd9\u4e2a token,\u8fd9\u6837\u5c31\u4e0d\u4f1a\u51fa\u73b0 CSRF \u6f0f\u6d1e\u7684\u95ee\u9898\u3002\u56e0\u4e3a\uff0c\u5373\u4f7f\u6709\u4e2a\u4f60\u70b9\u51fb\u4e86\u975e\u6cd5\u94fe\u63a5\u53d1\u9001\u4e86\u8bf7\u6c42\u5230\u670d\u52a1\u7aef\uff0c\u8fd9\u4e2a\u975e\u6cd5\u8bf7\u6c42\u662f\u4e0d\u4f1a\u643a\u5e26 token \u7684\uff0c\u6240\u4ee5\u8fd9\u4e2a\u8bf7\u6c42\u5c06\u662f\u975e\u6cd5\u7684\u3002<\/p>\n

                                                \u9700\u8981\u6ce8\u610f\u7684\u662f\u4e0d\u8bba\u662f Cookie \u8fd8\u662f token \u90fd\u65e0\u6cd5\u907f\u514d\u8de8\u7ad9\u811a\u672c\u653b\u51fb(Cross Site Scripting)XSS\u3002<\/p>\n

                                                \u201c\u8de8\u7ad9\u811a\u672c\u653b\u51fb(Cross Site Scripting)\u7f29\u5199\u4e3a CSS \u4f46\u8fd9\u4f1a\u4e0e\u5c42\u53e0\u6837\u5f0f\u8868(Cascading Style Sheets\uff0cCSS)\u7684\u7f29\u5199\u6df7\u6dc6\u3002\u56e0\u6b64\uff0c\u6709\u4eba\u5c06\u8de8\u7ad9\u811a\u672c\u653b\u51fb\u7f29\u5199\u4e3aXSS\u3002\u201d XSS\u4e2d\u653b\u51fb\u8005\u4f1a\u7528\u5404\u79cd\u65b9\u5f0f\u5c06\u6076\u610f\u4ee3\u7801\u6ce8\u5165\u5230\u5176\u4ed6\u7528\u6237\u7684\u9875\u9762\u4e2d\u3002\u5c31\u53ef\u4ee5\u901a\u8fc7\u811a\u672c\u76d7\u7528\u4fe1\u606f\u6bd4\u5982cookie\u3002<\/p>\n

                                                6. \u4ec0\u4e48\u662f Token?\u4ec0\u4e48\u662f JWT?\u5982\u4f55\u57fa\u4e8eToken\u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1?<\/strong><\/div>\n

                                                \u6211\u4eec\u5728\u4e0a\u4e00\u4e2a\u95ee\u9898\u4e2d\u63a2\u8ba8\u4e86\u4f7f\u7528 Session \u6765\u9274\u522b\u7528\u6237\u7684\u8eab\u4efd\uff0c\u5e76\u4e14\u7ed9\u51fa\u4e86\u51e0\u4e2a Spring Session \u7684\u6848\u4f8b\u5206\u4eab\u3002 \u6211\u4eec\u77e5\u9053 Session \u4fe1\u606f\u9700\u8981\u4fdd\u5b58\u4e00\u4efd\u5728\u670d\u52a1\u5668\u7aef\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5e26\u6765\u4e00\u4e9b\u9ebb\u70e6\uff0c\u6bd4\u5982\u9700\u8981\u6211\u4eec\u4fdd\u8bc1\u4fdd\u5b58 Session \u4fe1\u606f\u670d\u52a1\u5668\u7684\u53ef\u7528\u6027\u3001\u4e0d\u9002\u5408\u79fb\u52a8\u7aef(\u4f9d\u8d56Cookie)\u7b49\u7b49\u3002<\/p>\n

                                                \u6709\u6ca1\u6709\u4e00\u79cd\u4e0d\u9700\u8981\u81ea\u5df1\u5b58\u653e Session \u4fe1\u606f\u5c31\u80fd\u5b9e\u73b0\u8eab\u4efd\u9a8c\u8bc1\u7684\u65b9\u5f0f\u5462?\u4f7f\u7528 Token \u5373\u53ef!JWT (JSON Web Token) \u5c31\u662f\u8fd9\u79cd\u65b9\u5f0f\u7684\u5b9e\u73b0\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u670d\u52a1\u5668\u7aef\u5c31\u4e0d\u9700\u8981\u4fdd\u5b58 Session \u6570\u636e\u4e86\uff0c\u53ea\u7528\u5728\u5ba2\u6237\u7aef\u4fdd\u5b58\u670d\u52a1\u7aef\u8fd4\u56de\u7ed9\u5ba2\u6237\u7684 Token \u5c31\u53ef\u4ee5\u4e86\uff0c\u6269\u5c55\u6027\u5f97\u5230\u63d0\u5347\u3002<\/p>\n

                                                JWT \u672c\u8d28\u4e0a\u5c31\u4e00\u6bb5\u7b7e\u540d\u7684 JSON \u683c\u5f0f\u7684\u6570\u636e\u3002\u7531\u4e8e\u5b83\u662f\u5e26\u6709\u7b7e\u540d\u7684\uff0c\u56e0\u6b64\u63a5\u6536\u8005\u4fbf\u53ef\u4ee5\u9a8c\u8bc1\u5b83\u7684\u771f\u5b9e\u6027\u3002<\/p>\n

                                                \u4e0b\u9762\u662f RFC 7519 \u5bf9 JWT \u505a\u7684\u8f83\u4e3a\u6b63\u5f0f\u7684\u5b9a\u4e49\u3002<\/p>\n

                                                \u201cJSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties. The claims in a JWT are encoded as a JSON object that is used as the payload of a JSON Web Signature (JWS) structure or as the plaintext of a JSON Web Encryption (JWE) structure, enabling the claims to be digitally signed or integrity protected with a Message Authentication Code (MAC) and\/or encrypted. \u2014\u2014JSON Web Token (JWT)\u201d JWT \u7531 3 \u90e8\u5206\u6784\u6210:<\/p>\n

                                                Header :\u63cf\u8ff0 JWT \u7684\u5143\u6570\u636e\u3002\u5b9a\u4e49\u4e86\u751f\u6210\u7b7e\u540d\u7684\u7b97\u6cd5\u4ee5\u53ca Token \u7684\u7c7b\u578b\u3002<\/p>\n

                                                Payload(\u8d1f\u8f7d):\u7528\u6765\u5b58\u653e\u5b9e\u9645\u9700\u8981\u4f20\u9012\u7684\u6570\u636e<\/p>\n

                                                Signature(\u7b7e\u540d)\uff1a\u670d\u52a1\u5668\u901a\u8fc7Payload\u3001Header\u548c\u4e00\u4e2a\u5bc6\u94a5(secret)\u4f7f\u7528 Header \u91cc\u9762\u6307\u5b9a\u7684\u7b7e\u540d\u7b97\u6cd5(\u9ed8\u8ba4\u662f HMAC SHA256)\u751f\u6210\u3002<\/p>\n

                                                \u5728\u57fa\u4e8e Token \u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u7684\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u670d\u52a1\u5668\u901a\u8fc7Payload\u3001Header\u548c\u4e00\u4e2a\u5bc6\u94a5(secret)\u521b\u5efa\u4ee4\u724c(Token)\u5e76\u5c06 Token \u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\uff0c\u5ba2\u6237\u7aef\u5c06 Token \u4fdd\u5b58\u5728 Cookie \u6216\u8005 localStorage \u91cc\u9762\uff0c\u4ee5\u540e\u5ba2\u6237\u7aef\u53d1\u51fa\u7684\u6240\u6709\u8bf7\u6c42\u90fd\u4f1a\u643a\u5e26\u8fd9\u4e2a\u4ee4\u724c\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u653e\u5728 Cookie \u91cc\u9762\u81ea\u52a8\u53d1\u9001\uff0c\u4f46\u662f\u8fd9\u6837\u4e0d\u80fd\u8de8\u57df\uff0c\u6240\u4ee5\u66f4\u597d\u7684\u505a\u6cd5\u662f\u653e\u5728 HTTP Header \u7684 Authorization\u5b57\u6bb5\u4e2d\uff1aAuthorization: Bearer Token\u3002<\/p>\n

                                                \"\"<\/p>\n

                                                Token Based Authentication flow<\/p>\n

                                                \u7528\u6237\u5411\u670d\u52a1\u5668\u53d1\u9001\u7528\u6237\u540d\u548c\u5bc6\u7801\u7528\u4e8e\u767b\u9646\u7cfb\u7edf\u3002<\/p>\n

                                                \u8eab\u4efd\u9a8c\u8bc1\u670d\u52a1\u54cd\u5e94\u5e76\u8fd4\u56de\u4e86\u7b7e\u540d\u7684 JWT\uff0c\u4e0a\u9762\u5305\u542b\u4e86\u7528\u6237\u662f\u8c01\u7684\u5185\u5bb9\u3002<\/p>\n

                                                \u7528\u6237\u4ee5\u540e\u6bcf\u6b21\u5411\u540e\u7aef\u53d1\u8bf7\u6c42\u90fd\u5728Header\u4e2d\u5e26\u4e0a JWT\u3002<\/p>\n

                                                \u670d\u52a1\u7aef\u68c0\u67e5 JWT \u5e76\u4ece\u4e2d\u83b7\u53d6\u7528\u6237\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n

                                                7.\u4ec0\u4e48\u662fOAuth 2.0?<\/strong><\/div>\n

                                                OAuth \u662f\u4e00\u4e2a\u884c\u4e1a\u7684\u6807\u51c6\u6388\u6743\u534f\u8bae\uff0c\u4e3b\u8981\u7528\u6765\u6388\u6743\u7b2c\u4e09\u65b9\u5e94\u7528\u83b7\u53d6\u6709\u9650\u7684\u6743\u9650\u3002\u800c OAuth 2.0\u662f\u5bf9 OAuth 1.0 \u7684\u5b8c\u5168\u91cd\u65b0\u8bbe\u8ba1\uff0cOAuth 2.0\u66f4\u5feb\uff0c\u66f4\u5bb9\u6613\u5b9e\u73b0\uff0cOAuth 1.0 \u5df2\u7ecf\u88ab\u5e9f\u5f03\u3002\u8be6\u60c5\u8bf7\u89c1\uff1arfc6749\u3002<\/p>\n

                                                \u5b9e\u9645\u4e0a\u5b83\u5c31\u662f\u4e00\u79cd\u6388\u6743\u673a\u5236\uff0c\u5b83\u7684\u6700\u7ec8\u76ee\u7684\u662f\u4e3a\u7b2c\u4e09\u65b9\u5e94\u7528\u9881\u53d1\u4e00\u4e2a\u6709\u65f6\u6548\u6027\u7684\u4ee4\u724c token\uff0c\u4f7f\u5f97\u7b2c\u4e09\u65b9\u5e94\u7528\u80fd\u591f\u901a\u8fc7\u8be5\u4ee4\u724c\u83b7\u53d6\u76f8\u5173\u7684\u8d44\u6e90\u3002<\/p>\n

                                                OAuth 2.0 \u6bd4\u8f83\u5e38\u7528\u7684\u573a\u666f\u5c31\u662f\u7b2c\u4e09\u65b9\u767b\u5f55\uff0c\u5f53\u4f60\u7684\u7f51\u7ad9\u63a5\u5165\u4e86\u7b2c\u4e09\u65b9\u767b\u5f55\u7684\u65f6\u5019\u4e00\u822c\u5c31\u662f\u4f7f\u7528\u7684 OAuth 2.0 \u534f\u8bae\u3002<\/p>\n

                                                \u53e6\u5916\uff0c\u73b0\u5728OAuth 2.0\u4e5f\u5e38\u89c1\u4e8e\u652f\u4ed8\u573a\u666f(\u5fae\u4fe1\u652f\u4ed8\u3001\u652f\u4ed8\u5b9d\u652f\u4ed8)\u548c\u5f00\u53d1\u5e73\u53f0(\u5fae\u4fe1\u5f00\u653e\u5e73\u53f0\u3001\u963f\u91cc\u5f00\u653e\u5e73\u53f0\u7b49\u7b49)\u3002<\/p>\n

                                                \u5fae\u4fe1\u652f\u4ed8\u8d26\u6237\u76f8\u5173\u53c2\u6570\uff1a<\/p>\n

                                                \"\"<\/p>\n

                                                8.\u4ec0\u4e48\u662f SSO?<\/strong><\/div>\n

                                                SSO(Single Sign On)\u5373\u5355\u70b9\u767b\u5f55\u8bf4\u7684\u662f\u7528\u6237\u767b\u9646\u591a\u4e2a\u5b50\u7cfb\u7edf\u7684\u5176\u4e2d\u4e00\u4e2a\u5c31\u6709\u6743\u8bbf\u95ee\u4e0e\u5176\u76f8\u5173\u7684\u5176\u4ed6\u7cfb\u7edf\u3002\u4e3e\u4e2a\u4f8b\u5b50\u6211\u4eec\u5728\u767b\u9646\u4e86\u4eac\u4e1c\u91d1\u878d\u4e4b\u540e\uff0c\u6211\u4eec\u540c\u65f6\u4e5f\u6210\u529f\u767b\u9646\u4eac\u4e1c\u7684\u4eac\u4e1c\u8d85\u5e02\u3001\u4eac\u4e1c\u5bb6\u7535\u7b49\u5b50\u7cfb\u7edf\u3002<\/p>\n

                                                9.SSO\u4e0eOAuth2.0\u7684\u533a\u522b<\/strong><\/div>\n

                                                OAuth \u662f\u4e00\u4e2a\u884c\u4e1a\u7684\u6807\u51c6\u6388\u6743\u534f\u8bae\uff0c\u4e3b\u8981\u7528\u6765\u6388\u6743\u7b2c\u4e09\u65b9\u5e94\u7528\u83b7\u53d6\u6709\u9650\u7684\u6743\u9650\u3002SSO\u89e3\u51b3\u7684\u662f\u4e00\u4e2a\u516c\u53f8\u7684\u591a\u4e2a\u76f8\u5173\u7684\u81ea\u7cfb\u7edf\u7684\u4e4b\u95f4\u7684\u767b\u9646\u95ee\u9898\u6bd4\u5982\u4eac\u4e1c\u65d7\u4e0b\u76f8\u5173\u5b50\u7cfb\u7edf\u4eac\u4e1c\u91d1\u878d\u3001\u4eac\u4e1c\u8d85\u5e02\u3001\u4eac\u4e1c\u5bb6\u7535\u7b49\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

                                                \u6240\u6709\u95ee\u9898\u5982\u4e0b\uff0c\u5f00\u59cb\u770b\u7b54\u6848\u4e4b\u524d\u4e0d\u59a8\u81ea\u5df1\u6d4b\u9a8c\u4e00\u4e0b\u81ea\u5df1\u7a76\u7adf\u80fd\u56de\u7b54\u51e0\u4e2a\uff1a \u8ba4\u8bc1 (Authentication) \u548c\u6388 […]<\/p>\n","protected":false},"author":1329,"featured_media":174026,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-174013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/174013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=174013"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/174013\/revisions"}],"predecessor-version":[{"id":174027,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/174013\/revisions\/174027"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/174026"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=174013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=174013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=174013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}