{"id":72584,"date":"2026-02-06T02:24:59","date_gmt":"2026-02-05T18:24:59","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/72584.html"},"modified":"2026-02-06T02:24:59","modified_gmt":"2026-02-05T18:24:59","slug":"spring-security-oauth2%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8e%e6%8e%88%e6%9d%83%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%88%b0%e5%be%ae%e6%9c%8d%e5%8a%a1%e7%bd%91%e5%85%b3%e7%9a%84%e5%ae%8c%e6%95%b4%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/72584.html","title":{"rendered":"Spring Security OAuth2\u5b9e\u6218\uff1a\u4ece\u6388\u6743\u670d\u52a1\u5668\u5230\u5fae\u670d\u52a1\u7f51\u5173\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<h3>\u4e00\u3001Spring Authorization Server\u6982\u8ff0<\/h3>\n<h4>1.1 \u4ec0\u4e48\u662fSpring Authorization Server<\/h4>\n<p>Spring Authorization Server\u662fSpring\u5b98\u65b9\u63a8\u51fa\u7684\u65b0\u4e00\u4ee3\u8ba4\u8bc1\u6388\u6743\u6846\u67b6&#xff0c;\u63d0\u4f9b\u4e86OAuth 2.1\u548cOpenID Connect 1.0\u89c4\u8303\u7684\u5b8c\u6574\u5b9e\u73b0\u3002\u5b83\u5efa\u7acb\u5728Spring Security\u4e4b\u4e0a&#xff0c;\u4e3a\u6784\u5efa\u8eab\u4efd\u63d0\u4f9b\u8005\u548c\u6388\u6743\u670d\u52a1\u5668\u63d0\u4f9b\u4e86\u5b89\u5168\u3001\u8f7b\u91cf\u7ea7\u4e14\u53ef\u5b9a\u5236\u7684\u57fa\u7840\u3002<\/p>\n<p>\u6838\u5fc3\u7279\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5b8c\u5168\u652f\u6301OAuth 2.1\u548cOpenID Connect 1.0<\/p>\n<\/li>\n<li>\n<p>\u6a21\u5757\u5316\u8bbe\u8ba1&#xff0c;\u6613\u4e8e\u6269\u5c55\u548c\u5b9a\u5236<\/p>\n<\/li>\n<li>\n<p>\u5185\u7f6e\u591a\u79cd\u6388\u6743\u6a21\u5f0f\u652f\u6301<\/p>\n<\/li>\n<li>\n<p>\u4e0eSpring\u751f\u6001\u7cfb\u7edf\u65e0\u7f1d\u96c6\u6210<\/p>\n<\/li>\n<\/ul>\n<p>\u5b98\u65b9\u8d44\u6e90&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5b98\u7f51&#xff1a;https:\/\/spring.io\/projects\/spring-authorization-server<\/p>\n<\/li>\n<li>\n<p>\u7248\u672c\u8981\u6c42&#xff1a;<\/p>\n<ul>\n<li>\n<p>Spring Authorization Server: 1.1.2&#043;<\/p>\n<\/li>\n<li>\n<p>JDK: 17&#043;<\/p>\n<\/li>\n<li>\n<p>Spring Boot: 3.1.4&#043;<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>1.2 \u4e3a\u4ec0\u4e48\u9700\u8981Spring Authorization Server<\/h4>\n<p>\u968f\u7740\u7f51\u7edc\u548c\u8bbe\u5907\u7684\u53d1\u5c55&#xff0c;\u539f\u6709\u7684OAuth 2.0\u534f\u8bae\u5df2\u65e0\u6cd5\u6ee1\u8db3\u73b0\u4ee3\u5e94\u7528\u7684\u5b89\u5168\u9700\u6c42\u3002OAuth\u793e\u533a\u63a8\u51fa\u4e86OAuth 2.1\u534f\u8bae&#xff0c;\u5bf9\u539f\u6709\u6388\u6743\u6a21\u5f0f\u8fdb\u884c\u4e86\u4f18\u5316\u548c\u8c03\u6574&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u79fb\u9664\u4e86\u5bc6\u7801\u6a21\u5f0f&#xff08;password&#xff09;\u548c\u7b80\u5316\u6a21\u5f0f&#xff08;implicit&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u589e\u52a0\u4e86\u8bbe\u5907\u6388\u6743\u7801\u6a21\u5f0f<\/p>\n<\/li>\n<li>\n<p>\u4e3a\u6388\u6743\u7801\u6a21\u5f0f\u589e\u52a0\u4e86PKCE\u6269\u5c55<\/p>\n<\/li>\n<\/ul>\n<p>Spring Security\u56e2\u961f\u56e0\u6b64\u91cd\u65b0\u5f00\u53d1\u4e86Spring Authorization Server&#xff0c;\u4ee5\u66ff\u4ee3\u539f\u6709\u7684Spring Security OAuth 2.0\u9879\u76ee\u3002<\/p>\n<h3>\u4e8c\u3001OAuth 2.0\u534f\u8bae\u8be6\u89e3<\/h3>\n<h4>2.1 OAuth 2.0\u6838\u5fc3\u6982\u5ff5<\/h4>\n<p>\u56db\u4e2a\u5173\u952e\u89d2\u8272&#xff1a;<\/p>\n<li>\n<p>\u5ba2\u6237\u7aef&#xff08;Client&#xff09;&#xff1a;\u7b2c\u4e09\u65b9\u5e94\u7528&#xff0c;\u8bf7\u6c42\u8bbf\u95ee\u7528\u6237\u8d44\u6e90<\/p>\n<\/li>\n<li>\n<p>\u8d44\u6e90\u670d\u52a1\u5668&#xff08;Resource Server&#xff09;&#xff1a;\u5b58\u50a8\u53d7\u4fdd\u62a4\u8d44\u6e90\u7684\u670d\u52a1\u5668<\/p>\n<\/li>\n<li>\n<p>\u8d44\u6e90\u6240\u6709\u8005&#xff08;Resource Owner&#xff09;&#xff1a;\u62e5\u6709\u8d44\u6e90\u7684\u7528\u6237<\/p>\n<\/li>\n<li>\n<p>\u6388\u6743\u670d\u52a1\u5668&#xff08;Authorization Server&#xff09;&#xff1a;\u9a8c\u8bc1\u7528\u6237\u8eab\u4efd\u5e76\u9881\u53d1\u4ee4\u724c<\/p>\n<\/li>\n<h4 style=\"background-color:transparent\">2.2 OAuth 2.0\u5de5\u4f5c\u6d41\u7a0b<\/h4>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"659\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205182457-6984e0795e9fe.png\" width=\"235\" \/><\/p>\n<p>\u4ee4\u724c&#xff08;Token&#xff09;\u4e0e\u5bc6\u7801&#xff08;Password&#xff09;\u7684\u533a\u522b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4ee4\u724c\u662f\u77ed\u671f\u7684&#xff0c;\u5230\u671f\u81ea\u52a8\u5931\u6548<\/p>\n<\/li>\n<li>\n<p>\u4ee4\u724c\u53ef\u4ee5\u88ab\u8d44\u6e90\u6240\u6709\u8005\u968f\u65f6\u64a4\u9500<\/p>\n<\/li>\n<li>\n<p>\u4ee4\u724c\u6709\u6743\u9650\u8303\u56f4&#xff08;scope&#xff09;&#xff0c;\u5bc6\u7801\u62e5\u6709\u5b8c\u6574\u6743\u9650<\/p>\n<\/li>\n<\/ul>\n<h4>2.3 OAuth 2.0\u5e94\u7528\u573a\u666f<\/h4>\n<li>\n<p>\u793e\u4ea4\u5a92\u4f53\u767b\u5f55&#xff1a;\u4f7f\u7528\u5fae\u4fe1\u3001QQ\u7b49\u7b2c\u4e09\u65b9\u8d26\u53f7\u767b\u5f55<\/p>\n<\/li>\n<li>\n<p>\u7b2c\u4e09\u65b9\u5e94\u7528\u96c6\u6210&#xff1a;\u5e94\u7528\u95f4\u6570\u636e\u5171\u4eab\u548cAPI\u8c03\u7528<\/p>\n<\/li>\n<li>\n<p>\u79fb\u52a8\u5e94\u7528\u8bbf\u95eeAPI&#xff1a;\u79fb\u52a8\u7aef\u5e94\u7528\u8bbf\u95ee\u540e\u7aef\u670d\u52a1<\/p>\n<\/li>\n<li>\n<p>\u4e91\u670d\u52a1\u6388\u6743&#xff1a;\u8bbf\u95eeGoogle Drive\u3001Dropbox\u7b49\u4e91\u5b58\u50a8<\/p>\n<\/li>\n<li>\n<p>IoT\u8bbe\u5907\u8bbf\u95ee&#xff1a;\u7269\u8054\u7f51\u8bbe\u5907\u5b89\u5168\u8bbf\u95ee\u4e91\u670d\u52a1<\/p>\n<\/li>\n<h4>2.4 OAuth 2.0\u6388\u6743\u6a21\u5f0f<\/h4>\n<h5>2.4.1 \u5ba2\u6237\u7aef\u6a21\u5f0f&#xff08;Client Credentials Grant&#xff09;<\/h5>\n<p>text<\/p>\n<p>\u9002\u7528\u4e8e&#xff1a;\u670d\u52a1\u7aef\u5e94\u7528\u95f4\u7684\u901a\u4fe1<br \/>\n\u6d41\u7a0b&#xff1a;\u5ba2\u6237\u7aef\u76f4\u63a5\u4f7f\u7528client_id\u548cclient_secret\u83b7\u53d6\u4ee4\u724c <\/p>\n<p>\u8bf7\u6c42\u793a\u4f8b&#xff1a;<\/p>\n<p>text<\/p>\n<p> POST \/oauth2\/token<br \/>\ngrant_type&#061;client_credentials<br \/>\n&amp;client_id&#061;CLIENT_ID<br \/>\n&amp;client_secret&#061;CLIENT_SECRET <\/p>\n<h5>2.4.2 \u5bc6\u7801\u6a21\u5f0f&#xff08;Resource Owner Password Credentials Grant&#xff09;<\/h5>\n<p>text<\/p>\n<p> \u9002\u7528\u4e8e&#xff1a;\u9ad8\u5ea6\u4fe1\u4efb\u7684\u5185\u90e8\u5e94\u7528<br \/>\n\u6d41\u7a0b&#xff1a;\u7528\u6237\u63d0\u4f9b\u7528\u6237\u540d\u5bc6\u7801&#xff0c;\u5ba2\u6237\u7aef\u4ee3\u7406\u83b7\u53d6\u4ee4\u724c<br \/>\n\u6ce8\u610f&#xff1a;OAuth 2.1\u4e2d\u5df2\u79fb\u9664\u6b64\u6a21\u5f0f <\/p>\n<p>\u8bf7\u6c42\u793a\u4f8b&#xff1a;<\/p>\n<p>text<\/p>\n<p> POST \/oauth2\/token<br \/>\ngrant_type&#061;password<br \/>\n&amp;username&#061;USERNAME<br \/>\n&amp;password&#061;PASSWORD<br \/>\n&amp;client_id&#061;CLIENT_ID<br \/>\n&amp;client_secret&#061;CLIENT_SECRET <\/p>\n<h5>2.4.3 \u6388\u6743\u7801\u6a21\u5f0f&#xff08;Authorization Code Grant&#xff09;<\/h5>\n<p>text<\/p>\n<p> \u9002\u7528\u4e8e&#xff1a;Web\u5e94\u7528\u3001\u79fb\u52a8\u5e94\u7528<br \/>\n\u6d41\u7a0b&#xff1a;\u901a\u8fc7\u6388\u6743\u7801\u4e2d\u95f4\u6b65\u9aa4&#xff0c;\u5b89\u5168\u6027\u6700\u9ad8 <\/p>\n<p>\u6d41\u7a0b\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u5ba2\u6237\u7aef\u5f15\u5bfc\u7528\u6237\u5230\u6388\u6743\u670d\u52a1\u5668<\/p>\n<\/li>\n<li>\n<p>\u7528\u6237\u767b\u5f55\u5e76\u6388\u6743<\/p>\n<\/li>\n<li>\n<p>\u6388\u6743\u670d\u52a1\u5668\u8fd4\u56de\u6388\u6743\u7801<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u4f7f\u7528\u6388\u6743\u7801\u4ea4\u6362\u4ee4\u724c<\/p>\n<\/li>\n<p>\u8bf7\u6c42\u793a\u4f8b&#xff1a;<\/p>\n<p>text<\/p>\n<p> # 1. \u83b7\u53d6\u6388\u6743\u7801<br \/>\nGET \/oauth2\/authorize?<br \/>\nresponse_type&#061;code<br \/>\n&amp;client_id&#061;CLIENT_ID<br \/>\n&amp;redirect_uri&#061;CALLBACK_URL<br \/>\n&amp;scope&#061;read<\/p>\n<p># 2. \u4f7f\u7528\u6388\u6743\u7801\u83b7\u53d6\u4ee4\u724c<br \/>\nPOST \/oauth2\/token<br \/>\ngrant_type&#061;authorization_code<br \/>\n&amp;code&#061;AUTHORIZATION_CODE<br \/>\n&amp;client_id&#061;CLIENT_ID<br \/>\n&amp;client_secret&#061;CLIENT_SECRET<br \/>\n&amp;redirect_uri&#061;CALLBACK_URL <\/p>\n<h5>2.4.4 \u7b80\u5316\u6a21\u5f0f&#xff08;Implicit Grant&#xff09;<\/h5>\n<p>text<\/p>\n<p> \u9002\u7528\u4e8e&#xff1a;\u5355\u9875\u5e94\u7528&#xff08;SPA&#xff09;<br \/>\n\u6d41\u7a0b&#xff1a;\u76f4\u63a5\u8fd4\u56de\u4ee4\u724c&#xff0c;\u8df3\u8fc7\u6388\u6743\u7801\u6b65\u9aa4<br \/>\n\u6ce8\u610f&#xff1a;OAuth 2.1\u4e2d\u5df2\u79fb\u9664\u6b64\u6a21\u5f0f <\/p>\n<h5>2.4.5 \u5237\u65b0\u4ee4\u724c\u6a21\u5f0f&#xff08;Refresh Token Grant&#xff09;<\/h5>\n<p>text<\/p>\n<p> \u9002\u7528\u4e8e&#xff1a;\u4ee4\u724c\u7eed\u671f<br \/>\n\u6d41\u7a0b&#xff1a;\u4f7f\u7528refresh_token\u83b7\u53d6\u65b0\u7684access_token <\/p>\n<p>\u8bf7\u6c42\u793a\u4f8b&#xff1a;<\/p>\n<p>text<\/p>\n<p> POST \/oauth2\/token<br \/>\ngrant_type&#061;refresh_token<br \/>\n&amp;client_id&#061;CLIENT_ID<br \/>\n&amp;client_secret&#061;CLIENT_SECRET<br \/>\n&amp;refresh_token&#061;REFRESH_TOKEN <\/p>\n<h3>\u4e09\u3001OAuth 2.1\u534f\u8bae\u65b0\u7279\u6027<\/h3>\n<h4>3.1 \u6388\u6743\u7801\u6a21\u5f0f&#043;PKCE\u6269\u5c55<\/h4>\n<p>PKCE&#xff08;Proof Key for Code Exchange&#xff09;\u7528\u4e8e\u9632\u6b62\u6388\u6743\u7801\u88ab\u62e6\u622a\u653b\u51fb&#xff1a;<\/p>\n<p>\u5de5\u4f5c\u6d41\u7a0b&#xff1a;<\/p>\n<li>\n<p>\u5ba2\u6237\u7aef\u751f\u6210code_verifier\u548ccode_challenge<\/p>\n<\/li>\n<li>\n<p>\u6388\u6743\u8bf7\u6c42\u65f6\u53d1\u9001code_challenge<\/p>\n<\/li>\n<li>\n<p>\u4ea4\u6362\u4ee4\u724c\u65f6\u53d1\u9001code_verifier<\/p>\n<\/li>\n<li>\n<p>\u670d\u52a1\u5668\u9a8c\u8bc1\u4e24\u8005\u5339\u914d\u5173\u7cfb<\/p>\n<\/li>\n<h4>3.2 \u8bbe\u5907\u6388\u6743\u7801\u6a21\u5f0f<\/h4>\n<p>\u9002\u7528\u4e8e\u667a\u80fd\u7535\u89c6\u3001\u6253\u5370\u673a\u7b49\u8f93\u5165\u53d7\u9650\u8bbe\u5907&#xff1a;<\/p>\n<p>\u5de5\u4f5c\u6d41\u7a0b&#xff1a;<\/p>\n<li>\n<p>\u8bbe\u5907\u8bf7\u6c42\u8bbe\u5907\u7801\u548c\u7528\u6237\u7801<\/p>\n<\/li>\n<li>\n<p>\u7528\u6237\u5728\u53e6\u4e00\u8bbe\u5907\u8bbf\u95ee\u9a8c\u8bc1\u9875\u9762\u8f93\u5165\u7528\u6237\u7801<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u5907\u8f6e\u8be2\u83b7\u53d6\u4ee4\u724c<\/p>\n<\/li>\n<h4>3.3 \u62d3\u5c55\u6388\u6743\u6a21\u5f0f<\/h4>\n<p>\u867d\u7136OAuth 2.1\u79fb\u9664\u4e86\u5bc6\u7801\u6a21\u5f0f&#xff0c;\u4f46\u53ef\u901a\u8fc7\u62d3\u5c55\u6388\u6743\u6a21\u5f0f\u5b9e\u73b0\u7c7b\u4f3c\u529f\u80fd\u3002<\/p>\n<h3>\u56db\u3001OpenID Connect 1.0\u534f\u8bae<\/h3>\n<p>OpenID Connect\u662f\u5efa\u7acb\u5728OAuth 2.0\u4e4b\u4e0a\u7684\u8eab\u4efd\u5c42&#xff0c;\u4e3b\u8981\u589e\u52a0\u4e86id_token&#xff1a;<\/p>\n<p>\u6838\u5fc3\u7279\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u57fa\u4e8eJWT\u683c\u5f0f\u7684id_token<\/p>\n<\/li>\n<li>\n<p>\u7528\u6237\u4fe1\u606f\u7aef\u70b9&#xff08;UserInfo Endpoint&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u6807\u51c6\u5316\u58f0\u660e&#xff08;Claims&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>id_token\u793a\u4f8b&#xff1a;<\/p>\n<p>json<\/p>\n<p> {<br \/>\n  &#034;iss&#034;: &#034;https:\/\/server.example.com&#034;,<br \/>\n  &#034;sub&#034;: &#034;24400320&#034;,<br \/>\n  &#034;aud&#034;: &#034;s6BhdRkqt3&#034;,<br \/>\n  &#034;exp&#034;: 1311281970,<br \/>\n  &#034;iat&#034;: 1311280970,<br \/>\n  &#034;auth_time&#034;: 1311280969,<br \/>\n  &#034;nonce&#034;: &#034;n-0S6_WzA2Mj&#034;<br \/>\n} <\/p>\n<h3>\u4e94\u3001Spring Authorization Server\u5b9e\u6218<\/h3>\n<h4>5.1 \u6388\u6743\u670d\u52a1\u5668\u642d\u5efa<\/h4>\n<h5>5.1.1 \u9879\u76ee\u4f9d\u8d56<\/h5>\n<p>xml<\/p>\n<p> &lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter-oauth2-authorization-server&lt;\/artifactId&gt;<br \/>\n&lt;\/dependency&gt; <\/p>\n<h5>5.1.2 \u6838\u5fc3\u914d\u7f6e\u7c7b<\/h5>\n<p>java<\/p>\n<p> &#064;Configuration<br \/>\n&#064;EnableWebSecurity<br \/>\npublic class SecurityConfig {<\/p>\n<p>    \/\/ \u6388\u6743\u670d\u52a1\u5668\u5b89\u5168\u8fc7\u6ee4\u5668\u94fe<br \/>\n    &#064;Bean<br \/>\n    &#064;Order(1)<br \/>\n    public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)<br \/>\n            throws Exception {<br \/>\n        OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);<br \/>\n        http<br \/>\n            .getConfigurer(OAuth2AuthorizationServerConfigurer.class)<br \/>\n            .oidc(Customizer.withDefaults()); \/\/ \u5f00\u542fOpenID Connect<br \/>\n        return http.build();<br \/>\n    }<\/p>\n<p>    \/\/ \u9ed8\u8ba4\u5b89\u5168\u8fc7\u6ee4\u5668\u94fe<br \/>\n    &#064;Bean<br \/>\n    &#064;Order(2)<br \/>\n    public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)<br \/>\n            throws Exception {<br \/>\n        http<br \/>\n            .authorizeHttpRequests(authorize -&gt; authorize<br \/>\n                .anyRequest().authenticated()<br \/>\n            )<br \/>\n            .formLogin(Customizer.withDefaults());<br \/>\n        return http.build();<br \/>\n    }<\/p>\n<p>    \/\/ \u7528\u6237\u4fe1\u606f\u670d\u52a1<br \/>\n    &#064;Bean<br \/>\n    public UserDetailsService userDetailsService() {<br \/>\n        UserDetails userDetails &#061; User.withDefaultPasswordEncoder()<br \/>\n            .username(&#034;fox&#034;)<br \/>\n            .password(&#034;123456&#034;)<br \/>\n            .roles(&#034;USER&#034;)<br \/>\n            .build();<br \/>\n        return new InMemoryUserDetailsManager(userDetails);<br \/>\n    }<\/p>\n<p>    \/\/ \u5ba2\u6237\u7aef\u6ce8\u518c\u4fe1\u606f<br \/>\n    &#064;Bean<br \/>\n    public RegisteredClientRepository registeredClientRepository() {<br \/>\n        RegisteredClient oidcClient &#061; RegisteredClient.withId(UUID.randomUUID().toString())<br \/>\n            .clientId(&#034;oidc-client&#034;)<br \/>\n            .clientSecret(&#034;{noop}secret&#034;)<br \/>\n            .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)<br \/>\n            .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)<br \/>\n            .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)<br \/>\n            .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)<br \/>\n            .redirectUri(&#034;http:\/\/www.baidu.com&#034;)<br \/>\n            .scope(OidcScopes.OPENID)<br \/>\n            .scope(OidcScopes.PROFILE)<br \/>\n            .clientSettings(ClientSettings.builder()<br \/>\n                .requireAuthorizationConsent(true)<br \/>\n                .build())<br \/>\n            .build();<\/p>\n<p>        return new InMemoryRegisteredClientRepository(oidcClient);<br \/>\n    }<\/p>\n<p>    \/\/ JWT\u5bc6\u94a5\u914d\u7f6e<br \/>\n    &#064;Bean<br \/>\n    public JWKSource&lt;SecurityContext&gt; jwkSource() {<br \/>\n        KeyPair keyPair &#061; generateRsaKey();<br \/>\n        RSAPublicKey publicKey &#061; (RSAPublicKey) keyPair.getPublic();<br \/>\n        RSAPrivateKey privateKey &#061; (RSAPrivateKey) keyPair.getPrivate();<\/p>\n<p>        RSAKey rsaKey &#061; new RSAKey.Builder(publicKey)<br \/>\n            .privateKey(privateKey)<br \/>\n            .keyID(UUID.randomUUID().toString())<br \/>\n            .build();<\/p>\n<p>        JWKSet jwkSet &#061; new JWKSet(rsaKey);<br \/>\n        return new ImmutableJWKSet&lt;&gt;(jwkSet);<br \/>\n    }<\/p>\n<p>    \/\/ \u751f\u6210RSA\u5bc6\u94a5\u5bf9<br \/>\n    private static KeyPair generateRsaKey() {<br \/>\n        KeyPair keyPair;<br \/>\n        try {<br \/>\n            KeyPairGenerator keyPairGenerator &#061; KeyPairGenerator.getInstance(&#034;RSA&#034;);<br \/>\n            keyPairGenerator.initialize(2048);<br \/>\n            keyPair &#061; keyPairGenerator.generateKeyPair();<br \/>\n        } catch (Exception ex) {<br \/>\n            throw new IllegalArgumentException(ex);<br \/>\n        }<br \/>\n        return keyPair;<br \/>\n    }<br \/>\n} <\/p>\n<h5>5.1.3 \u6d4b\u8bd5\u7aef\u70b9<\/h5>\n<p>\u83b7\u53d6\u6388\u6743\u670d\u52a1\u5668\u914d\u7f6e\u4fe1\u606f&#xff1a;<\/p>\n<p>text<\/p>\n<p> GET http:\/\/127.0.0.1:9000\/.well-known\/openid-configuration <\/p>\n<p>\u6388\u6743\u7801\u6a21\u5f0f\u6d4b\u8bd5&#xff1a;<\/p>\n<li>\n<p>\u83b7\u53d6\u6388\u6743\u7801&#xff1a;<\/p>\n<\/li>\n<p>text<\/p>\n<p> GET http:\/\/localhost:9000\/oauth2\/authorize?<br \/>\nresponse_type&#061;code<br \/>\n&amp;client_id&#061;oidc-client<br \/>\n&amp;scope&#061;profile openid<br \/>\n&amp;redirect_uri&#061;http:\/\/www.baidu.com <\/p>\n<li>\n<p>\u4f7f\u7528\u6388\u6743\u7801\u83b7\u53d6\u4ee4\u724c&#xff1a;<\/p>\n<\/li>\n<p>bash<\/p>\n<p> curl -X POST http:\/\/localhost:9000\/oauth2\/token \\\\<br \/>\n  -H &#034;Content-Type: application\/x-www-form-urlencoded&#034; \\\\<br \/>\n  -u &#034;oidc-client:secret&#034; \\\\<br \/>\n  -d &#034;grant_type&#061;authorization_code&#034; \\\\<br \/>\n  -d &#034;code&#061;{\u6388\u6743\u7801}&#034; \\\\<br \/>\n  -d &#034;redirect_uri&#061;http:\/\/www.baidu.com&#034; <\/p>\n<h4>5.2 OAuth2\u5ba2\u6237\u7aef\u642d\u5efa<\/h4>\n<h5>5.2.1 \u9879\u76ee\u4f9d\u8d56<\/h5>\n<p>xml<\/p>\n<p> &lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter-oauth2-client&lt;\/artifactId&gt;<br \/>\n&lt;\/dependency&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;<br \/>\n&lt;\/dependency&gt; <\/p>\n<h5>5.2.2 \u914d\u7f6e\u6587\u4ef6<\/h5>\n<p>yaml<\/p>\n<p> server:<br \/>\n  port: 9001<\/p>\n<p>spring:<br \/>\n  application:<br \/>\n    name: spring-oauth-client<\/p>\n<p>  security:<br \/>\n    oauth2:<br \/>\n      client:<br \/>\n        provider:<br \/>\n          oauth-server:<br \/>\n            issuer-uri: http:\/\/spring-oauth-server:9000<br \/>\n            authorization-uri: http:\/\/spring-oauth-server:9000\/oauth2\/authorize<br \/>\n            token-uri: http:\/\/spring-oauth-server:9000\/oauth2\/token<\/p>\n<p>        registration:<br \/>\n          messaging-client-oidc:<br \/>\n            provider: oauth-server<br \/>\n            client-name: web\u5e73\u53f0<br \/>\n            client-id: web-client-id<br \/>\n            client-secret: secret<br \/>\n            client-authentication-method: client_secret_basic<br \/>\n            authorization-grant-type: authorization_code<br \/>\n            redirect-uri: http:\/\/spring-oauth-client:9001\/login\/oauth2\/code\/messaging-client-oidc<br \/>\n            scope:<br \/>\n              &#8211; profile<br \/>\n              &#8211; openid <\/p>\n<p>\u6ce8\u610f&#xff1a;\u9700\u8981\u5728hosts\u6587\u4ef6\u4e2d\u6dfb\u52a0\u57df\u540d\u6620\u5c04&#xff1a;<\/p>\n<p>text<\/p>\n<p> 127.0.0.1 spring-oauth-client spring-oauth-server <\/p>\n<h5>5.2.3 \u5ba2\u6237\u7aef\u63a7\u5236\u5668<\/h5>\n<p>java<\/p>\n<p> &#064;RestController<br \/>\npublic class AuthenticationController {<\/p>\n<p>    &#064;GetMapping(&#034;\/token&#034;)<br \/>\n    &#064;ResponseBody<br \/>\n    public OAuth2AuthorizedClient token(<br \/>\n            &#064;RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient oAuth2AuthorizedClient) {<br \/>\n        return oAuth2AuthorizedClient;<br \/>\n    }<br \/>\n} <\/p>\n<h4>5.3 \u8d44\u6e90\u670d\u52a1\u5668\u642d\u5efa<\/h4>\n<h5>5.3.1 \u9879\u76ee\u4f9d\u8d56<\/h5>\n<p>xml<\/p>\n<p> &lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter-oauth2-resource-server&lt;\/artifactId&gt;<br \/>\n&lt;\/dependency&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;<br \/>\n&lt;\/dependency&gt; <\/p>\n<h5>5.3.2 \u914d\u7f6e\u6587\u4ef6<\/h5>\n<p>yaml<\/p>\n<p> server:<br \/>\n  port: 9002<\/p>\n<p>spring:<br \/>\n  application:<br \/>\n    name: spring-oauth-resource<\/p>\n<p>  security:<br \/>\n    oauth2:<br \/>\n      resource-server:<br \/>\n        jwt:<br \/>\n          issuer-uri: http:\/\/spring-oauth-server:9000 <\/p>\n<h5>5.3.3 \u5b89\u5168\u914d\u7f6e<\/h5>\n<p>java<\/p>\n<p> &#064;Configuration<br \/>\n&#064;EnableWebSecurity<br \/>\n&#064;EnableMethodSecurity(jsr250Enabled &#061; true, securedEnabled &#061; true)<br \/>\npublic class ResourceServerConfig {<\/p>\n<p>    &#064;Bean<br \/>\n    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {<br \/>\n        http<br \/>\n            .authorizeHttpRequests(authorize -&gt; authorize<br \/>\n                .anyRequest().authenticated()<br \/>\n            )<br \/>\n            .oauth2ResourceServer(oauth2 -&gt; oauth2<br \/>\n                .jwt(Customizer.withDefaults())<br \/>\n            );<\/p>\n<p>        return http.build();<br \/>\n    }<br \/>\n} <\/p>\n<h5>5.3.4 \u8d44\u6e90\u63a5\u53e3<\/h5>\n<p>java<\/p>\n<p> &#064;RestController<br \/>\npublic class MessagesController {<\/p>\n<p>    &#064;GetMapping(&#034;\/messages1&#034;)<br \/>\n    public String getMessages1() {<br \/>\n        return &#034;hello Message 1&#034;;<br \/>\n    }<\/p>\n<p>    &#064;GetMapping(&#034;\/messages2&#034;)<br \/>\n    &#064;PreAuthorize(&#034;hasAuthority(&#039;SCOPE_profile&#039;)&#034;)<br \/>\n    public String getMessages2() {<br \/>\n        return &#034;hello Message 2&#034;;<br \/>\n    }<\/p>\n<p>    &#064;GetMapping(&#034;\/messages3&#034;)<br \/>\n    &#064;PreAuthorize(&#034;hasAuthority(&#039;SCOPE_Message&#039;)&#034;)<br \/>\n    public String getMessages3() {<br \/>\n        return &#034;hello Message 3&#034;;<br \/>\n    }<br \/>\n} <\/p>\n<h5>5.3.5 \u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406<\/h5>\n<p>java<\/p>\n<p> \/\/ \u8ba4\u8bc1\u5f02\u5e38\u5904\u7406<br \/>\n&#064;Component<br \/>\npublic class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {<\/p>\n<p>    &#064;Override<br \/>\n    public void commence(HttpServletRequest request, HttpServletResponse response,<br \/>\n                         AuthenticationException authException) throws IOException {<\/p>\n<p>        if (authException instanceof InvalidBearerTokenException) {<br \/>\n            ResponseResult.exceptionResponse(response, &#034;\u4ee4\u724c\u65e0\u6548\u6216\u5df2\u8fc7\u671f&#034;);<br \/>\n        } else {<br \/>\n            ResponseResult.exceptionResponse(response, &#034;\u9700\u8981\u5e26\u4e0a\u4ee4\u724c\u8fdb\u884c\u8bbf\u95ee&#034;);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u6388\u6743\u5f02\u5e38\u5904\u7406<br \/>\n&#064;Component<br \/>\npublic class MyAccessDeniedHandler implements AccessDeniedHandler {<\/p>\n<p>    &#064;Override<br \/>\n    public void handle(HttpServletRequest request, HttpServletResponse response,<br \/>\n                       AccessDeniedException accessDeniedException) throws IOException {<br \/>\n        ResponseResult.exceptionResponse(response, &#034;\u6743\u9650\u4e0d\u8db3&#034;);<br \/>\n    }<br \/>\n} <\/p>\n<p>\u914d\u7f6e\u5f02\u5e38\u5904\u7406\u5668&#xff1a;<\/p>\n<p>java<\/p>\n<p> http.oauth2ResourceServer(resourceServer -&gt; resourceServer<br \/>\n    .jwt(Customizer.withDefaults())<br \/>\n    .authenticationEntryPoint(new MyAuthenticationEntryPoint())<br \/>\n    .accessDeniedHandler(new MyAccessDeniedHandler())<br \/>\n); <\/p>\n<h3>\u516d\u3001\u57fa\u4e8e\u6570\u636e\u5e93\u5b58\u50a8\u6539\u9020<\/h3>\n<h4>6.1 \u6570\u636e\u5e93\u8868\u7ed3\u6784<\/h4>\n<h5>6.1.1 \u5ba2\u6237\u7aef\u4fe1\u606f\u8868<\/h5>\n<p>sql<\/p>\n<p> CREATE TABLE oauth2_registered_client (<br \/>\n    id VARCHAR(100) NOT NULL,<br \/>\n    client_id VARCHAR(100) NOT NULL,<br \/>\n    client_id_issued_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,<br \/>\n    client_secret VARCHAR(200) DEFAULT NULL,<br \/>\n    client_secret_expires_at TIMESTAMP DEFAULT NULL,<br \/>\n    client_name VARCHAR(200) NOT NULL,<br \/>\n    client_authentication_methods VARCHAR(1000) NOT NULL,<br \/>\n    authorization_grant_types VARCHAR(1000) NOT NULL,<br \/>\n    redirect_uris VARCHAR(1000) DEFAULT NULL,<br \/>\n    post_logout_redirect_uris VARCHAR(1000) DEFAULT NULL,<br \/>\n    scopes VARCHAR(1000) NOT NULL,<br \/>\n    client_settings VARCHAR(2000) NOT NULL,<br \/>\n    token_settings VARCHAR(2000) NOT NULL,<br \/>\n    PRIMARY KEY (id)<br \/>\n); <\/p>\n<h5>6.1.2 \u6388\u6743\u786e\u8ba4\u8868<\/h5>\n<p>sql<\/p>\n<p> CREATE TABLE oauth2_authorization_consent (<br \/>\n    registered_client_id VARCHAR(100) NOT NULL,<br \/>\n    principal_name VARCHAR(200) NOT NULL,<br \/>\n    authorities VARCHAR(1000) NOT NULL,<br \/>\n    PRIMARY KEY (registered_client_id, principal_name)<br \/>\n); <\/p>\n<h5>6.1.3 \u6388\u6743\u4fe1\u606f\u8868<\/h5>\n<p>sql<\/p>\n<p> CREATE TABLE oauth2_authorization (<br \/>\n    id VARCHAR(100) NOT NULL,<br \/>\n    registered_client_id VARCHAR(100) NOT NULL,<br \/>\n    principal_name VARCHAR(200) NOT NULL,<br \/>\n    authorization_grant_type VARCHAR(100) NOT NULL,<br \/>\n    authorized_scopes VARCHAR(1000) DEFAULT NULL,<br \/>\n    attributes BLOB DEFAULT NULL,<br \/>\n    state VARCHAR(500) DEFAULT NULL,<br \/>\n    authorization_code_value BLOB DEFAULT NULL,<br \/>\n    authorization_code_issued_at TIMESTAMP DEFAULT NULL,<br \/>\n    authorization_code_expires_at TIMESTAMP DEFAULT NULL,<br \/>\n    authorization_code_metadata BLOB DEFAULT NULL,<br \/>\n    access_token_value BLOB DEFAULT NULL,<br \/>\n    access_token_issued_at TIMESTAMP DEFAULT NULL,<br \/>\n    access_token_expires_at TIMESTAMP DEFAULT NULL,<br \/>\n    access_token_metadata BLOB DEFAULT NULL,<br \/>\n    access_token_type VARCHAR(100) DEFAULT NULL,<br \/>\n    access_token_scopes VARCHAR(1000) DEFAULT NULL,<br \/>\n    oidc_id_token_value BLOB DEFAULT NULL,<br \/>\n    oidc_id_token_issued_at TIMESTAMP DEFAULT NULL,<br \/>\n    oidc_id_token_expires_at TIMESTAMP DEFAULT NULL,<br \/>\n    oidc_id_token_metadata BLOB DEFAULT NULL,<br \/>\n    refresh_token_value BLOB DEFAULT NULL,<br \/>\n    refresh_token_issued_at TIMESTAMP DEFAULT NULL,<br \/>\n    refresh_token_expires_at TIMESTAMP DEFAULT NULL,<br \/>\n    refresh_token_metadata BLOB DEFAULT NULL,<br \/>\n    user_code_value BLOB DEFAULT NULL,<br \/>\n    user_code_issued_at TIMESTAMP DEFAULT NULL,<br \/>\n    user_code_expires_at TIMESTAMP DEFAULT NULL,<br \/>\n    user_code_metadata BLOB DEFAULT NULL,<br \/>\n    device_code_value BLOB DEFAULT NULL,<br \/>\n    device_code_issued_at TIMESTAMP DEFAULT NULL,<br \/>\n    device_code_expires_at TIMESTAMP DEFAULT NULL,<br \/>\n    device_code_metadata BLOB DEFAULT NULL,<br \/>\n    PRIMARY KEY (id)<br \/>\n); <\/p>\n<h5>6.1.4 \u7528\u6237\u8868<\/h5>\n<p>sql<\/p>\n<p> CREATE TABLE sys_user (<br \/>\n    id BIGINT NOT NULL AUTO_INCREMENT COMMENT &#039;id&#039;,<br \/>\n    username VARCHAR(20) NOT NULL DEFAULT &#039;&#039; COMMENT &#039;\u7528\u6237\u540d&#039;,<br \/>\n    password VARCHAR(255) NOT NULL DEFAULT &#039;&#039; COMMENT &#039;\u5bc6\u7801&#039;,<br \/>\n    name VARCHAR(50) DEFAULT NULL COMMENT &#039;\u59d3\u540d&#039;,<br \/>\n    description VARCHAR(255) DEFAULT NULL COMMENT &#039;\u63cf\u8ff0&#039;,<br \/>\n    status TINYINT DEFAULT NULL COMMENT &#039;\u72b6\u6001&#xff08;1&#xff1a;\u6b63\u5e38 0&#xff1a;\u505c\u7528&#xff09;&#039;,<br \/>\n    PRIMARY KEY (id),<br \/>\n    UNIQUE KEY idx_username (username)<br \/>\n) ENGINE&#061;InnoDB DEFAULT CHARSET&#061;utf8mb4 COMMENT&#061;&#039;\u7528\u6237\u8868&#039;; <\/p>\n<h4>6.2 \u6570\u636e\u5e93\u914d\u7f6e<\/h4>\n<h5>6.2.1 \u914d\u7f6e\u6587\u4ef6<\/h5>\n<p>yaml<\/p>\n<p> spring:<br \/>\n  application:<br \/>\n    name: spring-oauth-server<\/p>\n<p>  datasource:<br \/>\n    driver-class-name: com.mysql.cj.jdbc.Driver<br \/>\n    url: jdbc:mysql:\/\/localhost:3306\/oauth-server?serverTimezone&#061;UTC&amp;useUnicode&#061;true&amp;characterEncoding&#061;utf-8<br \/>\n    username: root<br \/>\n    password: root <\/p>\n<h5>6.2.2 \u6570\u636e\u5e93\u670d\u52a1\u914d\u7f6e<\/h5>\n<p>java<\/p>\n<p> &#064;Configuration<br \/>\npublic class DatabaseConfig {<\/p>\n<p>    \/\/ \u5ba2\u6237\u7aef\u4fe1\u606f\u5b58\u50a8<br \/>\n    &#064;Bean<br \/>\n    public RegisteredClientRepository registeredClientRepository(JdbcTemplate jdbcTemplate) {<br \/>\n        return new JdbcRegisteredClientRepository(jdbcTemplate);<br \/>\n    }<\/p>\n<p>    \/\/ \u6388\u6743\u4fe1\u606f\u5b58\u50a8<br \/>\n    &#064;Bean<br \/>\n    public OAuth2AuthorizationService authorizationService(<br \/>\n            JdbcTemplate jdbcTemplate,<br \/>\n            RegisteredClientRepository registeredClientRepository) {<br \/>\n        return new JdbcOAuth2AuthorizationService(jdbcTemplate, registeredClientRepository);<br \/>\n    }<\/p>\n<p>    \/\/ \u6388\u6743\u786e\u8ba4\u5b58\u50a8<br \/>\n    &#064;Bean<br \/>\n    public OAuth2AuthorizationConsentService authorizationConsentService(<br \/>\n            JdbcTemplate jdbcTemplate,<br \/>\n            RegisteredClientRepository registeredClientRepository) {<br \/>\n        return new JdbcOAuth2AuthorizationConsentService(jdbcTemplate, registeredClientRepository);<br \/>\n    }<\/p>\n<p>    \/\/ \u5bc6\u7801\u7f16\u7801\u5668<br \/>\n    &#064;Bean<br \/>\n    public PasswordEncoder passwordEncoder() {<br \/>\n        return new BCryptPasswordEncoder();<br \/>\n    }<br \/>\n} <\/p>\n<h5>6.2.3 \u81ea\u5b9a\u4e49\u7528\u6237\u8be6\u60c5\u670d\u52a1<\/h5>\n<p>java<\/p>\n<p> &#064;Service<br \/>\npublic class UserDetailsServiceImpl implements UserDetailsService {<\/p>\n<p>    &#064;Resource<br \/>\n    private SysUserService sysUserService;<\/p>\n<p>    &#064;Override<br \/>\n    public UserDetails loadUserByUsername(String username)<br \/>\n            throws UsernameNotFoundException {<\/p>\n<p>        SysUserEntity sysUserEntity &#061; sysUserService.selectByUsername(username);<\/p>\n<p>        List&lt;SimpleGrantedAuthority&gt; authorities &#061; Arrays.asList(&#034;USER&#034;).stream()<br \/>\n            .map(SimpleGrantedAuthority::new)<br \/>\n            .collect(Collectors.toList());<\/p>\n<p>        return new User(<br \/>\n            username,<br \/>\n            sysUserEntity.getPassword(),<br \/>\n            authorities<br \/>\n        );<br \/>\n    }<br \/>\n} <\/p>\n<h3>\u4e03\u3001\u5355\u70b9\u767b\u5f55&#xff08;SSO&#xff09;\u5b9e\u6218<\/h3>\n<h4>7.1 SSO\u67b6\u6784\u8bbe\u8ba1<\/h4>\n<p>text<\/p>\n<p> \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502   \u8ba2\u5355\u670d\u52a1   \u2502    \u2502   \u5546\u54c1\u670d\u52a1   \u2502    \u2502  \u8ba4\u8bc1\u670d\u52a1\u5668  \u2502<br \/>\n\u2502  (\u5ba2\u6237\u7aef)   \u2502    \u2502  (\u5ba2\u6237\u7aef)   \u2502    \u2502 (\u6388\u6743\u670d\u52a1\u5668) \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n       \u2502                  \u2502                  \u2502<br \/>\n       \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                          \u2502<br \/>\n                    \u250c\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502   \u7528\u6237    \u2502<br \/>\n                    \u2502 (\u6d4f\u89c8\u5668)  \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 <\/p>\n<h4>7.2 \u8ba2\u5355\u670d\u52a1\u914d\u7f6e<\/h4>\n<p>yaml<\/p>\n<p> server:<br \/>\n  ip: spring-oauth-client-order<br \/>\n  port: 9003<\/p>\n<p>spring:<br \/>\n  application:<br \/>\n    name: spring-oauth-client-order<\/p>\n<p>  security:<br \/>\n    oauth2:<br \/>\n      client:<br \/>\n        provider:<br \/>\n          oauth-server:<br \/>\n            issuer-uri: http:\/\/spring-oauth-server:9000<br \/>\n            authorization-uri: http:\/\/spring-oauth-server:9000\/oauth2\/authorize<br \/>\n            token-uri: http:\/\/spring-oauth-server:9000\/oauth2\/token<\/p>\n<p>        registration:<br \/>\n          messaging-client-oidc:<br \/>\n            provider: oauth-server<br \/>\n            client-name: web\u5e73\u53f0-SSO\u5ba2\u6237\u7aef-\u8ba2\u5355\u670d\u52a1<br \/>\n            client-id: web-client-id-order<br \/>\n            client-secret: secret<br \/>\n            client-authentication-method: client_secret_basic<br \/>\n            authorization-grant-type: authorization_code<br \/>\n            redirect-uri: http:\/\/spring-oauth-client-order:9003\/login\/oauth2\/code\/messaging-client-oidc<br \/>\n            scope:<br \/>\n              &#8211; profile<br \/>\n              &#8211; openid <\/p>\n<h4>7.3 \u5546\u54c1\u670d\u52a1\u914d\u7f6e<\/h4>\n<p>yaml<\/p>\n<p> server:<br \/>\n  ip: spring-oauth-client-product<br \/>\n  port: 9004<\/p>\n<p>spring:<br \/>\n  application:<br \/>\n    name: spring-oauth-client-product<\/p>\n<p>  security:<br \/>\n    oauth2:<br \/>\n      client:<br \/>\n        provider:<br \/>\n          oauth-server:<br \/>\n            issuer-uri: http:\/\/spring-oauth-server:9000<br \/>\n            authorization-uri: http:\/\/spring-oauth-server:9000\/oauth2\/authorize<br \/>\n            token-uri: http:\/\/spring-oauth-server:9000\/oauth2\/token<\/p>\n<p>        registration:<br \/>\n          messaging-client-oidc:<br \/>\n            provider: oauth-server<br \/>\n            client-name: web\u5e73\u53f0-SSO\u5ba2\u6237\u7aef-\u5546\u54c1\u670d\u52a1<br \/>\n            client-id: web-client-id-product<br \/>\n            client-secret: secret<br \/>\n            client-authentication-method: client_secret_basic<br \/>\n            authorization-grant-type: authorization_code<br \/>\n            redirect-uri: http:\/\/spring-oauth-client-product:9004\/login\/oauth2\/code\/messaging-client-oidc<br \/>\n            scope:<br \/>\n              &#8211; profile<br \/>\n              &#8211; openid <\/p>\n<h4>7.4 SSO\u6d4b\u8bd5\u6d41\u7a0b<\/h4>\n<li>\n<p>\u9996\u6b21\u8bbf\u95ee\u8ba2\u5355\u670d\u52a1&#xff1a;\u8df3\u8f6c\u5230\u8ba4\u8bc1\u670d\u52a1\u5668\u767b\u5f55<\/p>\n<\/li>\n<li>\n<p>\u767b\u5f55\u6210\u529f\u540e&#xff1a;\u8fd4\u56de\u8ba2\u5355\u670d\u52a1\u9875\u9762<\/p>\n<\/li>\n<li>\n<p>\u8df3\u8f6c\u5230\u5546\u54c1\u670d\u52a1&#xff1a;\u65e0\u9700\u91cd\u65b0\u767b\u5f55&#xff0c;\u76f4\u63a5\u8bbf\u95ee<\/p>\n<\/li>\n<li>\n<p>\u65e0\u611f\u6388\u6743\u914d\u7f6e&#xff1a;\u8bbe\u7f6erequire-authorization-consent&#061;false\u8df3\u8fc7\u6388\u6743\u786e\u8ba4<\/p>\n<\/li>\n<h3>\u516b\u3001\u5fae\u670d\u52a1\u7f51\u5173\u6574\u5408OAuth2<\/h3>\n<h4>8.1 \u7f51\u5173\u5b89\u5168\u67b6\u6784<\/h4>\n<p>text<\/p>\n<p> \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510    \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502    \u7528\u6237     \u2502\u2500\u2500\u2500\u25b6\u2502    \u7f51\u5173     \u2502\u2500\u2500\u2500\u25b6\u2502 \u8ba4\u8bc1\u670d\u52a1\u5668  \u2502<br \/>\n\u2502  (\u6d4f\u89c8\u5668)   \u2502    \u2502 (Gateway)   \u2502    \u2502 (OAuth2)    \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2518    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<br \/>\n                          \u2502<br \/>\n                    \u250c\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n                    \u2502 \u8d44\u6e90\u670d\u52a1   \u2502<br \/>\n                    \u2502 (\u5fae\u670d\u52a1)   \u2502<br \/>\n                    \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 <\/p>\n<h4>8.2 \u7f51\u5173\u914d\u7f6e<\/h4>\n<h5>8.2.1 \u9879\u76ee\u4f9d\u8d56<\/h5>\n<p>xml<\/p>\n<p> &lt;!&#8211; OAuth2\u5ba2\u6237\u7aef &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter-oauth2-client&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;3.1.4&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<\/p>\n<p>&lt;!&#8211; OAuth2\u8d44\u6e90\u670d\u52a1\u5668 &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter-oauth2-resource-server&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;3.1.4&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt; <\/p>\n<h5>8.2.2 \u914d\u7f6e\u6587\u4ef6<\/h5>\n<p>yaml<\/p>\n<p> server:<br \/>\n  port: 8888<\/p>\n<p>spring:<br \/>\n  application:<br \/>\n    name: mall-gateway<\/p>\n<p>  security:<br \/>\n    oauth2:<br \/>\n      # \u8d44\u6e90\u670d\u52a1\u5668\u914d\u7f6e<br \/>\n      resourceserver:<br \/>\n        jwt:<br \/>\n          issuer-uri: http:\/\/spring-oauth-server:9000<\/p>\n<p>      # \u5ba2\u6237\u7aef\u914d\u7f6e<br \/>\n      client:<br \/>\n        provider:<br \/>\n          oauth-server:<br \/>\n            issuer-uri: http:\/\/spring-oauth-server:9000<br \/>\n            authorization-uri: http:\/\/spring-oauth-server:9000\/oauth2\/authorize<br \/>\n            token-uri: http:\/\/spring-oauth-server:9000\/oauth2\/token<\/p>\n<p>        registration:<br \/>\n          messaging-client-oidc:<br \/>\n            provider: oauth-server<br \/>\n            client-name: \u7f51\u5173\u670d\u52a1<br \/>\n            client-id: mall-gateway-id<br \/>\n            client-secret: secret<br \/>\n            client-authentication-method: client_secret_basic<br \/>\n            authorization-grant-type: authorization_code<br \/>\n            redirect-uri: http:\/\/mall-gateway:8888\/login\/oauth2\/code\/messaging-client-oidc<br \/>\n            scope:<br \/>\n              &#8211; profile<br \/>\n              &#8211; openid<\/p>\n<p>  cloud:<br \/>\n    gateway:<br \/>\n      default-filters:<br \/>\n        # \u4ee4\u724c\u4e2d\u7ee7&#xff0c;\u81ea\u52a8\u4f20\u9012token\u5230\u4e0b\u6e38\u670d\u52a1<br \/>\n        &#8211; TokenRelay&#061; <\/p>\n<h5>8.2.3 \u5b89\u5168\u914d\u7f6e<\/h5>\n<p>java<\/p>\n<p> &#064;Configuration<br \/>\n&#064;EnableWebFluxSecurity<br \/>\n&#064;EnableReactiveMethodSecurity<br \/>\npublic class WebSecurityConfig {<\/p>\n<p>    &#064;Bean<br \/>\n    public SecurityWebFilterChain defaultSecurityFilterChain(ServerHttpSecurity http) {<br \/>\n        \/\/ \u6240\u6709\u8bf7\u6c42\u90fd\u9700\u8981\u8ba4\u8bc1<br \/>\n        http.authorizeExchange(authorize -&gt; authorize<br \/>\n            .anyExchange().authenticated()<br \/>\n        );<\/p>\n<p>        \/\/ \u5f00\u542fOAuth2\u767b\u5f55<br \/>\n        http.oauth2Login(Customizer.withDefaults());<\/p>\n<p>        \/\/ \u914d\u7f6e\u8d44\u6e90\u670d\u52a1\u5668<br \/>\n        http.oauth2ResourceServer(resourceServer -&gt; resourceServer<br \/>\n            .jwt(Customizer.withDefaults())<br \/>\n        );<\/p>\n<p>        \/\/ \u7981\u7528CSRF\u548cCORS<br \/>\n        http.csrf(csrf -&gt; csrf.disable());<br \/>\n        http.cors(cors -&gt; cors.disable());<\/p>\n<p>        return http.build();<br \/>\n    }<br \/>\n} <\/p>\n<h4>8.3 \u5fae\u670d\u52a1\u8d44\u6e90\u670d\u52a1\u5668\u914d\u7f6e<\/h4>\n<h5>8.3.1 \u9879\u76ee\u4f9d\u8d56<\/h5>\n<p>xml<\/p>\n<p> &lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter-oauth2-resource-server&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;3.1.4&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt; <\/p>\n<h5>8.3.2 \u914d\u7f6e\u6587\u4ef6<\/h5>\n<p>yaml<\/p>\n<p> spring:<br \/>\n  security:<br \/>\n    oauth2:<br \/>\n      resourceserver:<br \/>\n        jwt:<br \/>\n          issuer-uri: http:\/\/spring-oauth-server:9000 <\/p>\n<h5>8.3.3 \u5b89\u5168\u914d\u7f6e<\/h5>\n<p>java<\/p>\n<p> &#064;Configuration<br \/>\n&#064;EnableWebSecurity<br \/>\n&#064;EnableMethodSecurity(jsr250Enabled &#061; true, securedEnabled &#061; true)<br \/>\npublic class ResourceServerConfig {<\/p>\n<p>    &#064;Bean<br \/>\n    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {<br \/>\n        http<br \/>\n            .authorizeHttpRequests(authorize -&gt; authorize<br \/>\n                .anyRequest().authenticated()<br \/>\n            )<br \/>\n            .oauth2ResourceServer(oauth2 -&gt; oauth2<br \/>\n                .jwt(Customizer.withDefaults())<br \/>\n            );<\/p>\n<p>        return http.build();<br \/>\n    }<br \/>\n} <\/p>\n<h5>8.3.4 Feign\u62e6\u622a\u5668&#xff08;\u4ee4\u724c\u4f20\u9012&#xff09;<\/h5>\n<p>java<\/p>\n<p> &#064;Slf4j<br \/>\n&#064;Component<br \/>\npublic class FeignAuthRequestInterceptor implements RequestInterceptor {<\/p>\n<p>    &#064;Override<br \/>\n    public void apply(RequestTemplate template) {<br \/>\n        ServletRequestAttributes attributes &#061;<br \/>\n            (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();<\/p>\n<p>        if (attributes !&#061; null) {<br \/>\n            HttpServletRequest request &#061; attributes.getRequest();<br \/>\n            String accessToken &#061; request.getHeader(&#034;Authorization&#034;);<br \/>\n            log.info(&#034;\u4eceRequest\u4e2d\u89e3\u6790\u8bf7\u6c42\u5934: {}&#034;, accessToken);<\/p>\n<p>            \/\/ \u8bbe\u7f6etoken\u5230Feign\u8bf7\u6c42\u5934<br \/>\n            template.header(&#034;Authorization&#034;, accessToken);<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<h4>8.4 \u6d4b\u8bd5\u6d41\u7a0b<\/h4>\n<li>\n<p>\u8bbf\u95ee\u7f51\u5173\u53d7\u4fdd\u62a4\u63a5\u53e3&#xff1a;http:\/\/mall-gateway:8888\/user\/findOrderByUserId\/1<\/p>\n<\/li>\n<li>\n<p>\u7f51\u5173\u68c0\u6d4b\u672a\u8ba4\u8bc1&#xff1a;\u91cd\u5b9a\u5411\u5230\u8ba4\u8bc1\u670d\u52a1\u5668\u767b\u5f55\u9875\u9762<\/p>\n<\/li>\n<li>\n<p>\u7528\u6237\u767b\u5f55\u6388\u6743&#xff1a;\u8f93\u5165\u7528\u6237\u540d\u5bc6\u7801&#xff0c;\u786e\u8ba4\u6388\u6743<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u7f51\u5173\u9875\u9762&#xff1a;\u643a\u5e26token&#xff0c;\u6b63\u5e38\u8bbf\u95ee\u8d44\u6e90<\/p>\n<\/li>\n<li>\n<p>\u7f51\u5173\u8f6c\u53d1\u8bf7\u6c42&#xff1a;\u81ea\u52a8\u4f20\u9012token\u5230\u4e0b\u6e38\u5fae\u670d\u52a1<\/p>\n<\/li>\n<li>\n<p>\u5fae\u670d\u52a1\u9a8c\u8bc1token&#xff1a;\u9a8c\u8bc1\u901a\u8fc7&#xff0c;\u8fd4\u56de\u6570\u636e<\/p>\n<\/li>\n<h3>\u4e5d\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<h4>9.1 \u7248\u672c\u9009\u62e9\u5efa\u8bae<\/h4>\n<table>\n<tr>\u7ec4\u4ef6\u63a8\u8350\u7248\u672c\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>Spring Boot<\/td>\n<td>3.1.4&#043;<\/td>\n<td>\u652f\u6301Spring Authorization Server\u6700\u65b0\u7279\u6027<\/td>\n<\/tr>\n<tr>\n<td>Spring Authorization Server<\/td>\n<td>1.1.2&#043;<\/td>\n<td>\u7a33\u5b9a\u7248\u672c&#xff0c;\u529f\u80fd\u5b8c\u6574<\/td>\n<\/tr>\n<tr>\n<td>JDK<\/td>\n<td>17&#043;<\/td>\n<td>Spring Boot 3.x\u8981\u6c42<\/td>\n<\/tr>\n<tr>\n<td>MySQL<\/td>\n<td>8.0&#043;<\/td>\n<td>\u652f\u6301JSON\u5b57\u6bb5&#xff0c;\u6027\u80fd\u66f4\u597d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>9.2 \u5b89\u5168\u5efa\u8bae<\/h4>\n<li>\n<p>\u4ee4\u724c\u7ba1\u7406&#xff1a;<\/p>\n<ul>\n<li>\n<p>Access Token\u6709\u6548\u671f\u5efa\u8bae\u8bbe\u7f6e\u4e3a30\u5206\u949f<\/p>\n<\/li>\n<li>\n<p>Refresh Token\u6709\u6548\u671f\u5efa\u8bae\u8bbe\u7f6e\u4e3a7\u5929<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528HTTPS\u4f20\u8f93\u4ee4\u724c<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u5b89\u5168&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528BCrypt\u52a0\u5bc6\u5b58\u50a8client_secret<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u671f\u8f6e\u6362\u5ba2\u6237\u7aef\u5bc6\u94a5<\/p>\n<\/li>\n<li>\n<p>\u9650\u5236\u5ba2\u6237\u7aefIP\u767d\u540d\u5355<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6743\u9650\u63a7\u5236&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6700\u5c0f\u6743\u9650\u539f\u5219&#xff0c;\u6309\u9700\u5206\u914dscope<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528&#064;PreAuthorize\u6ce8\u89e3\u8fdb\u884c\u65b9\u6cd5\u7ea7\u6743\u9650\u63a7\u5236<\/p>\n<\/li>\n<li>\n<p>\u8bb0\u5f55\u654f\u611f\u64cd\u4f5c\u65e5\u5fd7<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<h4>9.3 \u6027\u80fd\u4f18\u5316<\/h4>\n<li>\n<p>\u7f13\u5b58\u7b56\u7565&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7f13\u5b58JWK\u516c\u94a5&#xff0c;\u51cf\u5c11\u7f51\u7edc\u8bf7\u6c42<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528Redis\u7f13\u5b58\u7528\u6237\u4fe1\u606f<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u4f18\u5316<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6570\u636e\u5e93\u4f18\u5316&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4e3aoauth2_authorization\u8868\u6dfb\u52a0\u7d22\u5f15<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u671f\u6e05\u7406\u8fc7\u671f\u4ee4\u724c\u8bb0\u5f55<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u8bfb\u5199\u5206\u79bb\u67b6\u6784<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<h4>9.4 \u76d1\u63a7\u4e0e\u544a\u8b66<\/h4>\n<li>\n<p>\u76d1\u63a7\u6307\u6807&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8ba4\u8bc1\u6210\u529f\/\u5931\u8d25\u7387<\/p>\n<\/li>\n<li>\n<p>\u4ee4\u724c\u53d1\u653e\u9891\u7387<\/p>\n<\/li>\n<li>\n<p>\u63a5\u53e3\u54cd\u5e94\u65f6\u95f4<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u544a\u8b66\u89c4\u5219&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5f02\u5e38\u767b\u5f55\u5c1d\u8bd5<\/p>\n<\/li>\n<li>\n<p>\u4ee4\u724c\u6ee5\u7528\u68c0\u6d4b<\/p>\n<\/li>\n<li>\n<p>\u7cfb\u7edf\u5f02\u5e38\u7387<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<h4>9.5 \u6269\u5c55\u529f\u80fd<\/h4>\n<li>\n<p>\u591a\u56e0\u7d20\u8ba4\u8bc1&#xff1a;\u96c6\u6210\u77ed\u4fe1\u3001\u90ae\u7bb1\u9a8c\u8bc1\u7801<\/p>\n<\/li>\n<li>\n<p>\u793e\u4ea4\u767b\u5f55&#xff1a;\u96c6\u6210\u5fae\u4fe1\u3001QQ\u7b49\u7b2c\u4e09\u65b9\u767b\u5f55<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u5907\u7ba1\u7406&#xff1a;\u7ba1\u7406\u5df2\u6388\u6743\u8bbe\u5907&#xff0c;\u652f\u6301\u4e00\u952e\u4e0b\u7ebf<\/p>\n<\/li>\n<li>\n<p>\u5ba1\u8ba1\u65e5\u5fd7&#xff1a;\u5b8c\u6574\u8bb0\u5f55\u8ba4\u8bc1\u6388\u6743\u64cd\u4f5c\u65e5\u5fd7<\/p>\n<\/li>\n<hr \/>\n<h3>\u5341\u3001\u5e38\u89c1\u95ee\u9898\u89e3\u51b3<\/h3>\n<h4>10.1 \u8de8\u57df\u95ee\u9898<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;<\/p>\n<p>java<\/p>\n<p> &#064;Bean<br \/>\npublic CorsConfigurationSource corsConfigurationSource() {<br \/>\n    CorsConfiguration configuration &#061; new CorsConfiguration();<br \/>\n    configuration.setAllowedOrigins(Arrays.asList(&#034;http:\/\/localhost:8080&#034;));<br \/>\n    configuration.setAllowedMethods(Arrays.asList(&#034;GET&#034;, &#034;POST&#034;, &#034;PUT&#034;, &#034;DELETE&#034;));<br \/>\n    configuration.setAllowedHeaders(Arrays.asList(&#034;Authorization&#034;, &#034;Content-Type&#034;));<\/p>\n<p>    UrlBasedCorsConfigurationSource source &#061; new UrlBasedCorsConfigurationSource();<br \/>\n    source.registerCorsConfiguration(&#034;\/**&#034;, configuration);<\/p>\n<p>    return source;<br \/>\n} <\/p>\n<h4>10.2 \u4f1a\u8bdd\u7ba1\u7406<\/h4>\n<p>\u914d\u7f6e\u5206\u5e03\u5f0f\u4f1a\u8bdd&#xff1a;<\/p>\n<p>yaml<\/p>\n<p> spring:<br \/>\n  session:<br \/>\n    store-type: redis<br \/>\n    redis:<br \/>\n      namespace: spring:session <\/p>\n<h4>10.3 \u4ee4\u724c\u5237\u65b0<\/h4>\n<p>\u81ea\u52a8\u5237\u65b0\u4ee4\u724c\u7b56\u7565&#xff1a;<\/p>\n<p>java<\/p>\n<p> &#064;Component<br \/>\npublic class TokenRefreshService {<\/p>\n<p>    &#064;Scheduled(fixedDelay &#061; 5 * 60 * 1000) \/\/ \u6bcf5\u5206\u949f\u68c0\u67e5\u4e00\u6b21<br \/>\n    public void refreshTokens() {<br \/>\n        \/\/ \u68c0\u67e5\u5373\u5c06\u8fc7\u671f\u7684token\u5e76\u5237\u65b0<br \/>\n    }<br \/>\n} <\/p>\n<hr \/>\n<p>\u53c2\u8003\u8d44\u6599&#xff1a;<\/p>\n<ul>\n<li>\n<p>Spring Authorization Server\u5b98\u65b9\u6587\u6863<\/p>\n<\/li>\n<li>\n<p>OAuth 2.1\u89c4\u8303<\/p>\n<\/li>\n<li>\n<p>OpenID Connect\u89c4\u8303<\/p>\n<\/li>\n<\/ul>\n<p>\u672c\u6587\u57fa\u4e8eSpring Authorization Server 1.1.2\u548cSpring Boot 3.1.4\u7f16\u5199&#xff0c;\u6db5\u76d6\u4e86\u4ece\u57fa\u7840\u6982\u5ff5\u5230\u751f\u4ea7\u7ea7\u90e8\u7f72\u7684\u5168\u6d41\u7a0b\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d&#xff0c;\u5efa\u8bae\u6839\u636e\u5177\u4f53\u4e1a\u52a1\u9700\u6c42\u548c\u5b89\u5168\u8981\u6c42\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001Spring Authorization Server\u6982\u8ff0<br \/>\n1.1 \u4ec0\u4e48\u662fSpring Authorization Server<br \/>\nSpring Authorization Server\u662fSpring\u5b98\u65b9\u63a8\u51fa\u7684\u65b0\u4e00\u4ee3\u8ba4\u8bc1\u6388\u6743\u6846\u67b6&#xff0c;\u63d0\u4f9b\u4e86OAuth 2.1\u548cOpenID Connect 1.0\u89c4\u8303\u7684\u5b8c\u6574\u5b9e\u73b0\u3002\u5b83\u5efa\u7acb\u5728Spring Security\u4e4b\u4e0a&#xff0c;\u4e3a\u6784\u5efa\u8eab\u4efd\u63d0\u4f9b\u8005\u548c\u6388\u6743\u670d\u52a1\u5668\u63d0\u4f9b\u4e86\u5b89\u5168\u3001\u8f7b\u91cf\u7ea7\u4e14\u53ef\u5b9a\u5236\u7684\u57fa\u7840\u3002<br \/>\n\u6838\u5fc3\u7279\u6027&#xff1a; \u5b8c<\/p>\n","protected":false},"author":2,"featured_media":72583,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1188,101,1348],"topic":[],"class_list":["post-72584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-oauth2","tag-spring","tag-1348"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Spring Security OAuth2\u5b9e\u6218\uff1a\u4ece\u6388\u6743\u670d\u52a1\u5668\u5230\u5fae\u670d\u52a1\u7f51\u5173\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/72584.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Security OAuth2\u5b9e\u6218\uff1a\u4ece\u6388\u6743\u670d\u52a1\u5668\u5230\u5fae\u670d\u52a1\u7f51\u5173\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001Spring Authorization Server\u6982\u8ff0 1.1 \u4ec0\u4e48\u662fSpring Authorization Server Spring Authorization Server\u662fSpring\u5b98\u65b9\u63a8\u51fa\u7684\u65b0\u4e00\u4ee3\u8ba4\u8bc1\u6388\u6743\u6846\u67b6&#xff0c;\u63d0\u4f9b\u4e86OAuth 2.1\u548cOpenID Connect 1.0\u89c4\u8303\u7684\u5b8c\u6574\u5b9e\u73b0\u3002\u5b83\u5efa\u7acb\u5728Spring Security\u4e4b\u4e0a&#xff0c;\u4e3a\u6784\u5efa\u8eab\u4efd\u63d0\u4f9b\u8005\u548c\u6388\u6743\u670d\u52a1\u5668\u63d0\u4f9b\u4e86\u5b89\u5168\u3001\u8f7b\u91cf\u7ea7\u4e14\u53ef\u5b9a\u5236\u7684\u57fa\u7840\u3002 \u6838\u5fc3\u7279\u6027&#xff1a; \u5b8c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/72584.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-05T18:24:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205182457-6984e0795e9fe.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/72584.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/72584.html\",\"name\":\"Spring Security OAuth2\u5b9e\u6218\uff1a\u4ece\u6388\u6743\u670d\u52a1\u5668\u5230\u5fae\u670d\u52a1\u7f51\u5173\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-05T18:24:59+00:00\",\"dateModified\":\"2026-02-05T18:24:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/72584.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/72584.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/72584.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Spring Security OAuth2\u5b9e\u6218\uff1a\u4ece\u6388\u6743\u670d\u52a1\u5668\u5230\u5fae\u670d\u52a1\u7f51\u5173\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Security OAuth2\u5b9e\u6218\uff1a\u4ece\u6388\u6743\u670d\u52a1\u5668\u5230\u5fae\u670d\u52a1\u7f51\u5173\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/72584.html","og_locale":"zh_CN","og_type":"article","og_title":"Spring Security OAuth2\u5b9e\u6218\uff1a\u4ece\u6388\u6743\u670d\u52a1\u5668\u5230\u5fae\u670d\u52a1\u7f51\u5173\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001Spring Authorization Server\u6982\u8ff0 1.1 \u4ec0\u4e48\u662fSpring Authorization Server Spring Authorization Server\u662fSpring\u5b98\u65b9\u63a8\u51fa\u7684\u65b0\u4e00\u4ee3\u8ba4\u8bc1\u6388\u6743\u6846\u67b6&#xff0c;\u63d0\u4f9b\u4e86OAuth 2.1\u548cOpenID Connect 1.0\u89c4\u8303\u7684\u5b8c\u6574\u5b9e\u73b0\u3002\u5b83\u5efa\u7acb\u5728Spring Security\u4e4b\u4e0a&#xff0c;\u4e3a\u6784\u5efa\u8eab\u4efd\u63d0\u4f9b\u8005\u548c\u6388\u6743\u670d\u52a1\u5668\u63d0\u4f9b\u4e86\u5b89\u5168\u3001\u8f7b\u91cf\u7ea7\u4e14\u53ef\u5b9a\u5236\u7684\u57fa\u7840\u3002 \u6838\u5fc3\u7279\u6027&#xff1a; \u5b8c","og_url":"https:\/\/www.wsisp.com\/helps\/72584.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-05T18:24:59+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205182457-6984e0795e9fe.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"11 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/72584.html","url":"https:\/\/www.wsisp.com\/helps\/72584.html","name":"Spring Security OAuth2\u5b9e\u6218\uff1a\u4ece\u6388\u6743\u670d\u52a1\u5668\u5230\u5fae\u670d\u52a1\u7f51\u5173\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-05T18:24:59+00:00","dateModified":"2026-02-05T18:24:59+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/72584.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/72584.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/72584.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Spring Security OAuth2\u5b9e\u6218\uff1a\u4ece\u6388\u6743\u670d\u52a1\u5668\u5230\u5fae\u670d\u52a1\u7f51\u5173\u7684\u5b8c\u6574\u89e3\u51b3\u65b9\u6848"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/72584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=72584"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/72584\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/72583"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=72584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=72584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=72584"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=72584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}