{"id":9357,"date":"2023-11-28T21:00:27","date_gmt":"2023-11-28T13:00:27","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9357"},"modified":"2023-12-06T16:59:45","modified_gmt":"2023-12-06T08:59:45","slug":"springcloud-zhong-shen-me-shi-oauth","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springcloud-zhong-shen-me-shi-oauth\/","title":{"rendered":"SpringCloud \u4e2d\u4ec0\u4e48\u662f OAuth?"},"content":{"rendered":"<p>OAuth\uff08\u5f00\u653e\u6388\u6743\uff09\u662f\u4e00\u79cd\u6388\u6743\u6846\u67b6\uff0c\u7528\u4e8e\u6388\u6743\u7b2c\u4e09\u65b9\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u7528\u6237\u7684\u8d44\u6e90\uff0c\u800c\u65e0\u9700\u63d0\u4f9b\u7528\u6237\u7684\u51ed\u636e\u3002OAuth\u7684\u76ee\u6807\u662f\u5141\u8bb8\u8d44\u6e90\u6240\u6709\u8005\uff08\u7528\u6237\uff09\u901a\u8fc7\u6388\u6743\u4ee3\u7406\uff08\u670d\u52a1\u63d0\u4f9b\u8005\uff09\u6388\u4e88\u7b2c\u4e09\u65b9\u5e94\u7528\u7a0b\u5e8f\u5bf9\u5176\u8d44\u6e90\u7684\u6709\u9650\u8bbf\u95ee\u3002\u5728Spring Cloud\u4e2d\uff0cOAuth\u5e38\u7528\u4e8e\u5b9e\u73b0\u5b89\u5168\u7684API\u8bbf\u95ee\u548c\u5fae\u670d\u52a1\u4e4b\u95f4\u7684\u8eab\u4efd\u9a8c\u8bc1\u6388\u6743\u3002<\/p>\n<p>OAuth\u7684\u6838\u5fc3\u6982\u5ff5\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u89d2\u8272\uff1a<\/p>\n<ol>\n<li>\n<strong>\u8d44\u6e90\u6240\u6709\u8005\uff08Resource Owner\uff09\uff1a<\/strong> \u901a\u5e38\u662f\u6307\u7528\u6237\uff0c\u662f\u8d44\u6e90\u7684\u62e5\u6709\u8005\uff0c\u53ef\u4ee5\u6388\u6743\u7b2c\u4e09\u65b9\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u5176\u8d44\u6e90\u3002\n<\/li>\n<li>\n<strong>\u5ba2\u6237\u7aef\uff08Client\uff09\uff1a<\/strong> \u7b2c\u4e09\u65b9\u5e94\u7528\u7a0b\u5e8f\uff0c\u5b83\u9700\u8981\u83b7\u53d6\u8d44\u6e90\u6240\u6709\u8005\u7684\u6388\u6743\u4ee5\u8bbf\u95ee\u8d44\u6e90\u3002\n<\/li>\n<li>\n<strong>\u6388\u6743\u670d\u52a1\u5668\uff08Authorization Server\uff09\uff1a<\/strong> \u8d1f\u8d23\u5bf9\u8d44\u6e90\u6240\u6709\u8005\u8fdb\u884c\u8ba4\u8bc1\u5e76\u9881\u53d1\u8bbf\u95ee\u4ee4\u724c\uff08Access Token\uff09\uff0c\u7528\u4e8e\u6388\u6743\u670d\u52a1\u5668\u5411\u7b2c\u4e09\u65b9\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u8bbf\u95ee\u8d44\u6e90\u7684\u6743\u9650\u3002\n<\/li>\n<li>\n<strong>\u8d44\u6e90\u670d\u52a1\u5668\uff08Resource Server\uff09\uff1a<\/strong> \u5b58\u50a8\u8d44\u6e90\u7684\u670d\u52a1\u5668\uff0c\u8d1f\u8d23\u9a8c\u8bc1\u8bbf\u95ee\u4ee4\u724c\u5e76\u63d0\u4f9b\u8d44\u6e90\u3002\n<\/li>\n<li>\n<strong>\u8bbf\u95ee\u4ee4\u724c\uff08Access Token\uff09\uff1a<\/strong> \u4ee3\u8868\u4e86\u8d44\u6e90\u6240\u6709\u8005\u6388\u6743\u7b2c\u4e09\u65b9\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u8d44\u6e90\u7684\u4ee4\u724c\uff0c\u7528\u4e8e\u5411\u8d44\u6e90\u670d\u52a1\u5668\u8bf7\u6c42\u8d44\u6e90\u3002\n<\/li>\n<\/ol>\n<p>Spring Cloud\u4e2d\u7684Spring Security OAuth\u63d0\u4f9b\u4e86OAuth 2.0\u7684\u5b9e\u73b0\uff0c\u5b83\u5305\u542b\u4e86\u6388\u6743\u670d\u52a1\u5668\u548c\u8d44\u6e90\u670d\u52a1\u5668\u7684\u652f\u6301\u3002Spring Security OAuth 2.0\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u6ce8\u89e3\u548c\u914d\u7f6e\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u76f8\u5bf9\u5bb9\u6613\u5730\u96c6\u6210OAuth\u529f\u80fd\u5230\u4ed6\u4eec\u7684Spring Cloud\u5e94\u7528\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5316\u7684Spring Cloud OAuth 2.0\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">@Configuration\n@EnableAuthorizationServer\npublic class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {\n\n    @Override\n    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {\n        clients\n            .inMemory()\n                .withClient(&quot;client-id&quot;)\n                .secret(&quot;client-secret&quot;)\n                .authorizedGrantTypes(&quot;authorization_code&quot;, &quot;password&quot;, &quot;refresh_token&quot;)\n                .scopes(&quot;read&quot;, &quot;write&quot;)\n                .redirectUris(&quot;http:\/\/localhost:8080\/callback&quot;);\n    }\n\n    @Override\n    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {\n        endpoints.authenticationManager(authenticationManager);\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c<code>@EnableAuthorizationServer<\/code>\u542f\u7528\u4e86OAuth 2.0\u7684\u6388\u6743\u670d\u52a1\u5668\u529f\u80fd\u3002\u914d\u7f6e\u4e2d\u6307\u5b9a\u4e86\u5ba2\u6237\u7aef\u7684\u4fe1\u606f\u3001\u652f\u6301\u7684\u6388\u6743\u65b9\u5f0f\uff08\u5982\u6388\u6743\u7801\u3001\u5bc6\u7801\u3001\u5237\u65b0\u4ee4\u724c\u7b49\uff09\u3001\u6388\u6743\u8303\u56f4\u7b49\u3002\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u66f4\u590d\u6742\u7684\u914d\u7f6e\u548c\u5b9a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>OAuth\uff08\u5f00\u653e\u6388\u6743\uff09\u662f\u4e00\u79cd\u6388\u6743\u6846\u67b6\uff0c\u7528\u4e8e\u6388\u6743\u7b2c\u4e09\u65b9\u5e94\u7528\u7a0b\u5e8f\u8bbf\u95ee\u7528\u6237\u7684\u8d44\u6e90\uff0c\u800c\u65e0\u9700\u63d0\u4f9b\u7528\u6237\u7684\u51ed\u636e\u3002OAuth\u7684 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[],"class_list":["post-9357","post","type-post","status-publish","format-standard","hentry","category-springcloud"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=9357"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9357\/revisions"}],"predecessor-version":[{"id":16715,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9357\/revisions\/16715"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9357"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}