{"id":82671,"date":"2026-07-25T11:19:13","date_gmt":"2026-07-25T03:19:13","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/82671.html"},"modified":"2026-07-25T11:19:13","modified_gmt":"2026-07-25T03:19:13","slug":"spring-boot-%e5%ae%89%e5%85%a8%e9%98%b2%e7%ba%bf%ef%bc%9aoauth2-%e8%b5%84%e6%ba%90%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e7%bb%86%e7%b2%92%e5%ba%a6%e9%89%b4%e6%9d%83%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/82671.html","title":{"rendered":"Spring Boot \u5b89\u5168\u9632\u7ebf\uff1aOAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218"},"content":{"rendered":"<h2>Spring Boot \u5b89\u5168\u9632\u7ebf&#xff1a;OAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218<\/h2>\n<h3>\u4e00\u3001\u8ba4\u8bc1\u4e0d\u7b49\u4e8e\u9274\u6743&#xff1a;\u5fae\u670d\u52a1\u573a\u666f\u4e0b\u7684\u6743\u9650\u7a7a\u6d1e<\/h3>\n<p>\u5f88\u591a Spring Boot \u9879\u76ee\u5728\u5b89\u5168\u4e0a\u53ea\u505a\u4e86&#034;\u8ba4\u8bc1&#034;\u2014\u2014\u7528\u6237\u767b\u5f55\u540e\u62ff\u5230 JWT&#xff0c;\u540e\u7eed\u8bf7\u6c42\u643a\u5e26 Token \u5373\u53ef\u8bbf\u95ee\u6240\u6709\u63a5\u53e3\u3002\u8fd9\u5728\u5355\u4f53\u5e94\u7528\u4e2d\u52c9\u5f3a\u53ef\u884c&#xff0c;\u4f46\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e0b\u4f1a\u66b4\u9732\u4e25\u91cd\u7684\u6743\u9650\u7a7a\u6d1e&#xff1a;\u6240\u6709\u5df2\u8ba4\u8bc1\u7528\u6237\u62e5\u6709\u76f8\u540c\u7684\u8bbf\u95ee\u8303\u56f4&#xff0c;\u670d\u52a1\u95f4\u8c03\u7528\u7f3a\u4e4f\u8eab\u4efd\u4f20\u9012&#xff0c;\u63a5\u53e3\u7ea7\u522b\u7684\u64cd\u4f5c\u6743\u9650\u5b8c\u5168\u7f3a\u5931\u3002<\/p>\n<p>\u66f4\u5e38\u89c1\u7684\u95ee\u9898\u662f JWT \u4e2d\u585e\u5165\u8fc7\u591a\u6743\u9650\u4fe1\u606f&#xff0c;\u5bfc\u81f4 Token \u4f53\u79ef\u81a8\u80c0\u5230\u6570 KB&#xff0c;\u6bcf\u6b21\u8bf7\u6c42\u90fd\u643a\u5e26\u5de8\u5927\u7684 Header\u3002\u4e00\u65e6\u6743\u9650\u53d8\u66f4&#xff0c;\u5df2\u7b7e\u53d1\u7684 Token \u65e0\u6cd5\u5373\u65f6\u5931\u6548&#xff0c;\u53ea\u80fd\u7b49\u5f85\u8fc7\u671f\u3002OAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u6a21\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u5408\u7406\u7684\u62c6\u5206\u2014\u2014\u8ba4\u8bc1\u7531\u6388\u6743\u670d\u52a1\u5668\u8d1f\u8d23&#xff0c;\u8d44\u6e90\u670d\u52a1\u5668\u53ea\u505a Token \u9a8c\u8bc1\u548c\u9274\u6743&#xff0c;\u804c\u8d23\u6e05\u6670\u4e14\u53ef\u72ec\u7acb\u6f14\u8fdb\u3002<\/p>\n<h3>\u4e8c\u3001OAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u67b6\u6784&#xff1a;Token \u9a8c\u8bc1\u4e0e\u6743\u9650\u52a0\u8f7d<\/h3>\n<p>Spring Security \u7684 OAuth2 Resource Server \u6a21\u5757\u652f\u6301 JWT \u548c Introspection \u4e24\u79cd Token \u9a8c\u8bc1\u65b9\u5f0f\u3002JWT \u65b9\u5f0f\u672c\u5730\u9a8c\u8bc1\u7b7e\u540d&#xff0c;\u65e0\u9700\u56de\u8c03\u6388\u6743\u670d\u52a1\u5668&#xff0c;\u6027\u80fd\u66f4\u4f18&#xff1b;Introspection \u65b9\u5f0f\u6bcf\u6b21\u8bf7\u6c42\u90fd\u5411\u6388\u6743\u670d\u52a1\u5668\u6821\u9a8c Token&#xff0c;\u652f\u6301\u5373\u65f6\u64a4\u9500\u3002<\/p>\n<p>sequenceDiagram<br \/>\n    participant C as \u5ba2\u6237\u7aef<br \/>\n    participant GW as API \u7f51\u5173<br \/>\n    participant RS as \u8d44\u6e90\u670d\u52a1\u5668<br \/>\n    participant AS as \u6388\u6743\u670d\u52a1\u5668<br \/>\n    participant DB as \u6743\u9650\u6570\u636e\u5e93<\/p>\n<p>    C-&gt;&gt;AS: \u767b\u5f55\u83b7\u53d6 JWT<br \/>\n    AS&#8211;&gt;&gt;C: JWT (\u542b userId, scope)<br \/>\n    C-&gt;&gt;GW: \u643a\u5e26 JWT \u8bbf\u95ee\u8d44\u6e90<br \/>\n    GW-&gt;&gt;RS: \u8f6c\u53d1\u8bf7\u6c42 &#043; Token<br \/>\n    RS-&gt;&gt;RS: \u672c\u5730\u9a8c\u8bc1 JWT \u7b7e\u540d<br \/>\n    RS-&gt;&gt;DB: \u6839\u636e userId \u67e5\u8be2\u7ec6\u7c92\u5ea6\u6743\u9650<br \/>\n    DB&#8211;&gt;&gt;RS: \u6743\u9650\u5217\u8868<br \/>\n    RS-&gt;&gt;RS: \u9274\u6743\u5224\u65ad<br \/>\n    alt \u6743\u9650\u4e0d\u8db3<br \/>\n        RS&#8211;&gt;&gt;GW: 403 Forbidden<br \/>\n        GW&#8211;&gt;&gt;C: \u6743\u9650\u4e0d\u8db3<br \/>\n    else \u6743\u9650\u901a\u8fc7<br \/>\n        RS&#8211;&gt;&gt;GW: 200 OK &#043; \u8d44\u6e90\u6570\u636e<br \/>\n        GW&#8211;&gt;&gt;C: \u54cd\u5e94\u6570\u636e<br \/>\n    end<\/p>\n<p>\u5173\u952e\u8bbe\u8ba1\u70b9\u5728\u4e8e&#034;JWT \u53ea\u643a\u5e26\u8eab\u4efd\u6807\u8bc6&#xff0c;\u6743\u9650\u5b9e\u65f6\u67e5\u8be2&#034;\u3002JWT \u4e2d\u53ea\u4fdd\u7559 sub&#xff08;\u7528\u6237 ID&#xff09;\u548c scope&#xff08;OAuth2 \u6807\u51c6\u8303\u56f4&#xff09;&#xff0c;\u7ec6\u7c92\u5ea6\u7684\u63a5\u53e3\u6743\u9650\u7531\u8d44\u6e90\u670d\u52a1\u5668\u4ece\u6570\u636e\u5e93\u6216\u7f13\u5b58\u4e2d\u52a0\u8f7d\u3002\u8fd9\u6837\u65e2\u63a7\u5236\u4e86 Token \u4f53\u79ef&#xff0c;\u53c8\u652f\u6301\u6743\u9650\u7684\u5373\u65f6\u751f\u6548\u3002<\/p>\n<h3>\u4e09\u3001\u751f\u4ea7\u7ea7\u4ee3\u7801\u5b9e\u73b0&#xff1a;\u591a\u5c42\u9274\u6743\u4e0e\u81ea\u5b9a\u4e49\u6743\u9650\u6a21\u578b<\/h3>\n<h4>3.1 \u8d44\u6e90\u670d\u52a1\u5668\u57fa\u7840\u914d\u7f6e<\/h4>\n<p>&#064;Configuration<br \/>\n&#064;EnableWebSecurity<br \/>\npublic class ResourceServerConfig {<\/p>\n<p>    &#064;Bean<br \/>\n    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {<br \/>\n        http<br \/>\n            .authorizeHttpRequests(auth -&gt; auth<br \/>\n                \/\/ \u516c\u5f00\u7aef\u70b9<br \/>\n                .requestMatchers(&#034;\/api\/public\/**&#034;).permitAll()<br \/>\n                \/\/ \u5065\u5eb7\u68c0\u67e5<br \/>\n                .requestMatchers(&#034;\/actuator\/health&#034;).permitAll()<br \/>\n                \/\/ \u5176\u4ed6\u8bf7\u6c42\u9700\u8ba4\u8bc1<br \/>\n                .anyRequest().authenticated()<br \/>\n            )<br \/>\n            .oauth2ResourceServer(oauth2 -&gt; oauth2<br \/>\n                .jwt(jwt -&gt; jwt<br \/>\n                    .jwtAuthenticationConverter(new CustomJwtAuthenticationConverter())<br \/>\n                )<br \/>\n                \/\/ Token \u9a8c\u8bc1\u5931\u8d25\u65f6\u7684\u81ea\u5b9a\u4e49\u54cd\u5e94<br \/>\n                .authenticationEntryPoint(new CustomAuthenticationEntryPoint())<br \/>\n                .accessDeniedHandler(new CustomAccessDeniedHandler())<br \/>\n            )<br \/>\n            \/\/ \u7981\u7528 CSRF&#xff08;\u65e0\u72b6\u6001 API&#xff09;<br \/>\n            .csrf(CsrfConfigurer::disable)<br \/>\n            \/\/ \u65e0\u72b6\u6001\u4f1a\u8bdd<br \/>\n            .sessionManagement(session -&gt;<br \/>\n                session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)<br \/>\n            );<\/p>\n<p>        return http.build();<br \/>\n    }<br \/>\n}<\/p>\n<h4>3.2 \u81ea\u5b9a\u4e49 JWT \u8f6c\u6362\u5668&#xff1a;\u4ece Token \u5230\u6743\u9650<\/h4>\n<p>public class CustomJwtAuthenticationConverter<br \/>\n        implements Converter&lt;Jwt, AbstractAuthenticationToken&gt; {<\/p>\n<p>    private final PermissionService permissionService;<\/p>\n<p>    &#064;Override<br \/>\n    public AbstractAuthenticationToken convert(Jwt jwt) {<br \/>\n        \/\/ \u4ece JWT \u4e2d\u63d0\u53d6\u7528\u6237\u6807\u8bc6<br \/>\n        String userId &#061; jwt.getSubject();<br \/>\n        String tenantId &#061; jwt.getClaimAsString(&#034;tenant_id&#034;);<\/p>\n<p>        \/\/ \u6784\u5efa\u57fa\u7840\u6743\u9650\u96c6\u5408&#xff08;\u6765\u81ea JWT \u7684 scope&#xff09;<br \/>\n        Collection&lt;GrantedAuthority&gt; authorities &#061; extractScopes(jwt);<\/p>\n<p>        \/\/ \u52a0\u8f7d\u7ec6\u7c92\u5ea6\u6743\u9650&#xff08;\u4ece\u7f13\u5b58\u6216\u6570\u636e\u5e93&#xff09;<br \/>\n        Collection&lt;GrantedAuthority&gt; fineGrainedAuthorities &#061;<br \/>\n            permissionService.loadPermissions(userId, tenantId);<\/p>\n<p>        \/\/ \u5408\u5e76\u6743\u9650<br \/>\n        List&lt;GrantedAuthority&gt; allAuthorities &#061; new ArrayList&lt;&gt;(authorities);<br \/>\n        allAuthorities.addAll(fineGrainedAuthorities);<\/p>\n<p>        return new JwtAuthenticationToken(jwt, allAuthorities, userId);<br \/>\n    }<\/p>\n<p>    private Collection&lt;GrantedAuthority&gt; extractScopes(Jwt jwt) {<br \/>\n        String scope &#061; jwt.getClaimAsString(&#034;scope&#034;);<br \/>\n        if (scope &#061;&#061; null || scope.isEmpty()) {<br \/>\n            return Collections.emptyList();<br \/>\n        }<br \/>\n        return Arrays.stream(scope.split(&#034; &#034;))<br \/>\n            .map(s -&gt; new SimpleGrantedAuthority(&#034;SCOPE_&#034; &#043; s))<br \/>\n            .collect(Collectors.toList());<br \/>\n    }<br \/>\n}<\/p>\n<h4>3.3 \u65b9\u6cd5\u7ea7\u9274\u6743&#xff1a;\u81ea\u5b9a\u4e49\u6743\u9650\u6ce8\u89e3<\/h4>\n<p>&#064;Target({ElementType.METHOD, ElementType.TYPE})<br \/>\n&#064;Retention(RetentionPolicy.RUNTIME)<br \/>\n&#064;PreAuthorize(&#034;hasPermission(#resourceId, #resourceType, #action)&#034;)<br \/>\npublic &#064;interface RequirePermission {<br \/>\n    String resourceType();<br \/>\n    String action();<br \/>\n}<\/p>\n<p>\/\/ \u6743\u9650\u8bc4\u4f30\u5668\u5b9e\u73b0<br \/>\n&#064;Component<br \/>\npublic class CustomPermissionEvaluator implements PermissionEvaluator {<\/p>\n<p>    private final PermissionRepository permissionRepo;<\/p>\n<p>    &#064;Override<br \/>\n    public boolean hasPermission(Authentication authentication,<br \/>\n                                  Object resourceId,<br \/>\n                                  Object permission) {<br \/>\n        if (authentication &#061;&#061; null || !authentication.isAuthenticated()) {<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        String userId &#061; authentication.getName();<br \/>\n        \/\/ permission \u683c\u5f0f: &#034;resourceType:action&#034;<br \/>\n        String[] parts &#061; permission.toString().split(&#034;:&#034;);<br \/>\n        if (parts.length !&#061; 2) {<br \/>\n            return false;<br \/>\n        }<\/p>\n<p>        String resourceType &#061; parts[0];<br \/>\n        String action &#061; parts[1];<\/p>\n<p>        \/\/ \u68c0\u67e5\u7528\u6237\u662f\u5426\u62e5\u6709\u8be5\u8d44\u6e90\u7684\u64cd\u4f5c\u6743\u9650<br \/>\n        return permissionRepo.hasPermission(userId,<br \/>\n            resourceId.toString(), resourceType, action);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public boolean hasPermission(Authentication authentication,<br \/>\n                                  Serializable targetId,<br \/>\n                                  String targetType,<br \/>\n                                  Object permission) {<br \/>\n        return hasPermission(authentication, targetId,<br \/>\n            targetType &#043; &#034;:&#034; &#043; permission);<br \/>\n    }<br \/>\n}<\/p>\n<h4>3.4 \u670d\u52a1\u95f4\u8c03\u7528\u7684\u8eab\u4efd\u4f20\u9012<\/h4>\n<p>&#064;Component<br \/>\npublic class ServiceAuthenticationInterceptor implements ClientHttpRequestInterceptor {<\/p>\n<p>    private final TokenRelayService tokenRelay;<\/p>\n<p>    &#064;Override<br \/>\n    public ClientHttpResponse intercept(HttpRequest request,<br \/>\n                                         byte[] body,<br \/>\n                                         ClientHttpRequestExecution execution)<br \/>\n            throws IOException {<br \/>\n        \/\/ \u4ece\u5f53\u524d\u4e0a\u4e0b\u6587\u83b7\u53d6\u7528\u6237\u8eab\u4efd<br \/>\n        Authentication auth &#061; SecurityContextHolder.getContext().getAuthentication();<\/p>\n<p>        if (auth instanceof JwtAuthenticationToken jwtAuth) {<br \/>\n            \/\/ \u4f20\u9012\u539f\u59cb JWT&#xff0c;\u4e0b\u6e38\u670d\u52a1\u53ef\u9a8c\u8bc1<br \/>\n            request.getHeaders().set(&#034;Authorization&#034;,<br \/>\n                &#034;Bearer &#034; &#043; jwtAuth.getToken().getTokenValue());<br \/>\n            \/\/ \u9644\u52a0\u670d\u52a1\u95f4\u8c03\u7528\u6807\u8bc6<br \/>\n            request.getHeaders().set(&#034;X-Caller-Service&#034;, &#034;order-service&#034;);<br \/>\n        } else {<br \/>\n            \/\/ \u670d\u52a1\u95f4\u5185\u90e8\u8c03\u7528&#xff0c;\u4f7f\u7528\u670d\u52a1\u8d26\u53f7 Token<br \/>\n            String serviceToken &#061; tokenRelay.getServiceToken();<br \/>\n            request.getHeaders().setBearerAuth(serviceToken);<br \/>\n        }<\/p>\n<p>        return execution.execute(request, body);<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u56db\u3001\u5b89\u5168\u67b6\u6784\u7684\u9690\u6027\u4ee3\u4ef7&#xff1a;\u6027\u80fd\u3001\u590d\u6742\u5ea6\u4e0e\u5bc6\u94a5\u8f6e\u6362<\/h3>\n<p>\u6743\u9650\u5b9e\u65f6\u67e5\u8be2\u7684\u6027\u80fd\u5f00\u9500&#xff1a;\u6bcf\u6b21\u8bf7\u6c42\u90fd\u67e5\u8be2\u6570\u636e\u5e93\u83b7\u53d6\u6743\u9650&#xff0c;\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u4f1a\u6210\u4e3a\u74f6\u9888\u3002\u5efa\u8bae\u91c7\u7528\u4e24\u7ea7\u7f13\u5b58\u2014\u2014\u672c\u5730 Caffeine \u7f13\u5b58&#xff08;TTL 30 \u79d2&#xff09;&#043; Redis \u7f13\u5b58&#xff08;TTL 5 \u5206\u949f&#xff09;&#xff0c;\u6743\u9650\u53d8\u66f4\u65f6\u4e3b\u52a8\u5931\u6548\u7f13\u5b58\u3002\u7f13\u5b58\u7684\u4ee3\u4ef7\u662f\u6743\u9650\u751f\u6548\u5ef6\u8fdf&#xff0c;\u9700\u6839\u636e\u4e1a\u52a1\u5b89\u5168\u7b49\u7ea7\u6743\u8861 TTL\u3002<\/p>\n<p>JWT \u5bc6\u94a5\u8f6e\u6362\u7684\u7a97\u53e3\u98ce\u9669&#xff1a;RSA \u5bc6\u94a5\u9700\u8981\u5b9a\u671f\u8f6e\u6362&#xff0c;\u4f46\u8f6e\u6362\u671f\u95f4\u65e7 Token \u53ef\u80fd\u4ecd\u88ab\u4f7f\u7528\u3002Spring Security \u652f\u6301\u914d\u7f6e\u591a\u4e2a\u9a8c\u8bc1\u5bc6\u94a5&#xff08;jwkSetUri \u81ea\u52a8\u5904\u7406&#xff09;&#xff0c;\u4f46\u81ea\u7b7e\u53d1\u573a\u666f\u9700\u624b\u52a8\u7ef4\u62a4\u5bc6\u94a5\u5217\u8868\u3002\u5efa\u8bae\u8bbe\u7f6e 15 \u5206\u949f\u7684\u5bc6\u94a5\u91cd\u53e0\u671f&#xff0c;\u65b0\u65e7\u5bc6\u94a5\u540c\u65f6\u6709\u6548\u3002<\/p>\n<p>\u7ec6\u7c92\u5ea6\u9274\u6743\u7684\u7ef4\u62a4\u6210\u672c&#xff1a;\u6bcf\u4e2a\u63a5\u53e3\u90fd\u914d\u7f6e\u6743\u9650\u6ce8\u89e3&#xff0c;\u968f\u7740\u4e1a\u52a1\u589e\u957f&#xff0c;\u6743\u9650\u77e9\u9635\u4f1a\u53d8\u5f97\u6781\u5176\u590d\u6742\u3002\u5efa\u8bae\u6309\u8d44\u6e90\u7c7b\u578b\u800c\u975e\u63a5\u53e3\u7c92\u5ea6\u914d\u7f6e\u6743\u9650&#xff0c;\u901a\u8fc7 RBAC &#043; ABAC \u6df7\u5408\u6a21\u578b\u964d\u4f4e\u590d\u6742\u5ea6\u3002\u89d2\u8272\u8986\u76d6 80% \u7684\u901a\u7528\u573a\u666f&#xff0c;\u5c5e\u6027\u89c4\u5219\u8986\u76d6 20% \u7684\u7279\u6b8a\u573a\u666f\u3002<\/p>\n<p>\u670d\u52a1\u95f4 Token \u4f20\u9012\u7684\u4fe1\u4efb\u8fb9\u754c&#xff1a;\u4e0b\u6e38\u670d\u52a1\u6536\u5230\u4e0a\u6e38\u4f20\u9012\u7684 JWT \u540e&#xff0c;\u65e0\u6cd5\u533a\u5206\u8bf7\u6c42\u662f\u6765\u81ea\u7528\u6237\u8fd8\u662f\u670d\u52a1\u3002\u5efa\u8bae\u5728 JWT \u4e2d\u589e\u52a0 azp&#xff08;authorized party&#xff09;\u58f0\u660e&#xff0c;\u6216\u5728 Header \u4e2d\u589e\u52a0 X-Caller-Service \u6807\u8bc6&#xff0c;\u4e0b\u6e38\u670d\u52a1\u636e\u6b64\u51b3\u5b9a\u662f\u5426\u4fe1\u4efb\u8be5\u8bf7\u6c42\u7684\u6743\u9650\u8303\u56f4\u3002<\/p>\n<h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<p>Spring Boot \u5b89\u5168\u67b6\u6784\u7684\u6838\u5fc3\u539f\u5219\u662f&#034;\u8ba4\u8bc1\u96c6\u4e2d\u3001\u9274\u6743\u5206\u6563&#034;\u2014\u2014\u6388\u6743\u670d\u52a1\u5668\u8d1f\u8d23\u7b7e\u53d1 Token&#xff0c;\u8d44\u6e90\u670d\u52a1\u5668\u8d1f\u8d23\u9a8c\u8bc1\u548c\u9274\u6743\u3002\u672c\u6587\u65b9\u6848\u7684\u5173\u952e\u5b9e\u73b0\u4e3a&#xff1a;JWT \u8f7b\u91cf\u5316&#xff08;\u53ea\u643a\u5e26\u8eab\u4efd\u6807\u8bc6&#xff09;\u3001\u6743\u9650\u5b9e\u65f6\u52a0\u8f7d&#xff08;\u7f13\u5b58 &#043; \u6570\u636e\u5e93&#xff09;\u3001\u65b9\u6cd5\u7ea7\u9274\u6743&#xff08;\u81ea\u5b9a\u4e49\u6ce8\u89e3 &#043; PermissionEvaluator&#xff09;\u3001\u670d\u52a1\u95f4\u8eab\u4efd\u4f20\u9012&#xff08;Token Relay&#xff09;\u3002\u843d\u5730\u65f6\u9700\u91cd\u70b9\u5173\u6ce8\u4e09\u4e2a\u914d\u7f6e&#xff1a;JWT \u7b7e\u540d\u7b97\u6cd5&#xff08;\u5efa\u8bae RS256&#xff0c;\u907f\u514d HS256 \u7684\u5bc6\u94a5\u5206\u53d1\u95ee\u9898&#xff09;\u3001\u6743\u9650\u7f13\u5b58 TTL&#xff08;\u5efa\u8bae 30 \u79d2\u672c\u5730 &#043; 5 \u5206\u949f Redis&#xff09;\u3001\u5bc6\u94a5\u8f6e\u6362\u5468\u671f&#xff08;\u5efa\u8bae 90 \u5929&#xff0c;\u91cd\u53e0\u671f 15 \u5206\u949f&#xff09;\u3002\u4e0a\u7ebf\u524d\u52a1\u5fc5\u8fdb\u884c\u6743\u9650\u77e9\u9635\u7684\u5168\u9762\u5ba1\u8ba1&#xff0c;\u786e\u4fdd\u65e0\u8d8a\u6743\u63a5\u53e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u5b89\u5168\u9632\u7ebf&#xff1a;OAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218<br \/>\n\u4e00\u3001\u8ba4\u8bc1\u4e0d\u7b49\u4e8e\u9274\u6743&#xff1a;\u5fae\u670d\u52a1\u573a\u666f\u4e0b\u7684\u6743\u9650\u7a7a\u6d1e<br \/>\n\u5f88\u591a Spring Boot \u9879\u76ee\u5728\u5b89\u5168\u4e0a\u53ea\u505a\u4e86\\&#8221;\u8ba4\u8bc1\\&#8221;\u2014\u2014\u7528\u6237\u767b\u5f55\u540e\u62ff\u5230 JWT&#xff0c;\u540e\u7eed\u8bf7\u6c42\u643a\u5e26 Token \u5373\u53ef\u8bbf\u95ee\u6240\u6709\u63a5\u53e3\u3002\u8fd9\u5728\u5355\u4f53\u5e94\u7528\u4e2d\u52c9\u5f3a\u53ef\u884c&#xff0c;\u4f46\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e0b\u4f1a\u66b4\u9732\u4e25\u91cd\u7684\u6743\u9650\u7a7a\u6d1e&#xff1a;\u6240\u6709\u5df2\u8ba4\u8bc1\u7528\u6237\u62e5\u6709\u76f8\u540c\u7684\u8bbf\u95ee\u8303\u56f4&#xff0c;\u670d\u52a1\u95f4\u8c03\u7528\u7f3a\u4e4f\u8eab\u4efd\u4f20\u9012&#xff0c;\u63a5\u53e3\u7ea7\u522b\u7684\u64cd\u4f5c<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,101,1348],"topic":[],"class_list":["post-82671","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","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 Boot \u5b89\u5168\u9632\u7ebf\uff1aOAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218 - \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\/82671.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Boot \u5b89\u5168\u9632\u7ebf\uff1aOAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Spring Boot \u5b89\u5168\u9632\u7ebf&#xff1a;OAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218 \u4e00\u3001\u8ba4\u8bc1\u4e0d\u7b49\u4e8e\u9274\u6743&#xff1a;\u5fae\u670d\u52a1\u573a\u666f\u4e0b\u7684\u6743\u9650\u7a7a\u6d1e \u5f88\u591a Spring Boot \u9879\u76ee\u5728\u5b89\u5168\u4e0a\u53ea\u505a\u4e86&quot;\u8ba4\u8bc1&quot;\u2014\u2014\u7528\u6237\u767b\u5f55\u540e\u62ff\u5230 JWT&#xff0c;\u540e\u7eed\u8bf7\u6c42\u643a\u5e26 Token \u5373\u53ef\u8bbf\u95ee\u6240\u6709\u63a5\u53e3\u3002\u8fd9\u5728\u5355\u4f53\u5e94\u7528\u4e2d\u52c9\u5f3a\u53ef\u884c&#xff0c;\u4f46\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e0b\u4f1a\u66b4\u9732\u4e25\u91cd\u7684\u6743\u9650\u7a7a\u6d1e&#xff1a;\u6240\u6709\u5df2\u8ba4\u8bc1\u7528\u6237\u62e5\u6709\u76f8\u540c\u7684\u8bbf\u95ee\u8303\u56f4&#xff0c;\u670d\u52a1\u95f4\u8c03\u7528\u7f3a\u4e4f\u8eab\u4efd\u4f20\u9012&#xff0c;\u63a5\u53e3\u7ea7\u522b\u7684\u64cd\u4f5c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/82671.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-25T03:19:13+00:00\" \/>\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=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82671.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/82671.html\",\"name\":\"Spring Boot \u5b89\u5168\u9632\u7ebf\uff1aOAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-25T03:19:13+00:00\",\"dateModified\":\"2026-07-25T03:19:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/82671.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/82671.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/82671.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Spring Boot \u5b89\u5168\u9632\u7ebf\uff1aOAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218\"}]},{\"@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 Boot \u5b89\u5168\u9632\u7ebf\uff1aOAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218 - \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\/82671.html","og_locale":"zh_CN","og_type":"article","og_title":"Spring Boot \u5b89\u5168\u9632\u7ebf\uff1aOAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Spring Boot \u5b89\u5168\u9632\u7ebf&#xff1a;OAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218 \u4e00\u3001\u8ba4\u8bc1\u4e0d\u7b49\u4e8e\u9274\u6743&#xff1a;\u5fae\u670d\u52a1\u573a\u666f\u4e0b\u7684\u6743\u9650\u7a7a\u6d1e \u5f88\u591a Spring Boot \u9879\u76ee\u5728\u5b89\u5168\u4e0a\u53ea\u505a\u4e86\"\u8ba4\u8bc1\"\u2014\u2014\u7528\u6237\u767b\u5f55\u540e\u62ff\u5230 JWT&#xff0c;\u540e\u7eed\u8bf7\u6c42\u643a\u5e26 Token \u5373\u53ef\u8bbf\u95ee\u6240\u6709\u63a5\u53e3\u3002\u8fd9\u5728\u5355\u4f53\u5e94\u7528\u4e2d\u52c9\u5f3a\u53ef\u884c&#xff0c;\u4f46\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e0b\u4f1a\u66b4\u9732\u4e25\u91cd\u7684\u6743\u9650\u7a7a\u6d1e&#xff1a;\u6240\u6709\u5df2\u8ba4\u8bc1\u7528\u6237\u62e5\u6709\u76f8\u540c\u7684\u8bbf\u95ee\u8303\u56f4&#xff0c;\u670d\u52a1\u95f4\u8c03\u7528\u7f3a\u4e4f\u8eab\u4efd\u4f20\u9012&#xff0c;\u63a5\u53e3\u7ea7\u522b\u7684\u64cd\u4f5c","og_url":"https:\/\/www.wsisp.com\/helps\/82671.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-25T03:19:13+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/82671.html","url":"https:\/\/www.wsisp.com\/helps\/82671.html","name":"Spring Boot \u5b89\u5168\u9632\u7ebf\uff1aOAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-25T03:19:13+00:00","dateModified":"2026-07-25T03:19:13+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/82671.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/82671.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/82671.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Spring Boot \u5b89\u5168\u9632\u7ebf\uff1aOAuth2 \u8d44\u6e90\u670d\u52a1\u5668\u4e0e\u7ec6\u7c92\u5ea6\u9274\u6743\u5b9e\u6218"}]},{"@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\/82671","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=82671"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/82671\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=82671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=82671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=82671"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=82671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}