You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot新授权服务器:OAuth2.1/OAuth2令牌撤销与端点查询

Spring Boot新授权服务器:令牌撤销与端点查询

一、撤销OAuth2.1令牌

Spring Authorization Server 遵循RFC 7009标准实现令牌撤销功能,操作步骤如下:

  1. 确保客户端配置了TOKEN_REVOCATION权限,在客户端注册时添加该权限:
    @Bean
    public RegisteredClientRepository registeredClientRepository() {
        RegisteredClient client = RegisteredClient.withId(UUID.randomUUID().toString())
                .clientId("your-client-id")
                .clientSecret("{noop}your-client-secret")
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                .scope(OAuth2Scopes.OPENID)
                .scope("read")
                .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                // 配置令牌撤销权限
                .build();
        return new InMemoryRegisteredClientRepository(client);
    }
    
  2. 发送POST请求到令牌撤销端点/oauth2/revoke,携带目标令牌、可选的令牌类型提示,同时完成客户端身份认证:
    curl -X POST "http://localhost:8080/oauth2/revoke" \
      -u "your-client-id:your-client-secret" \
      -d "token=your-access-or-refresh-token" \
      -d "token_type_hint=access_token" # 可选值:access_token/refresh_token
    

二、撤销OAuth2令牌

在Spring Authorization Server中,旧版OAuth2令牌的撤销逻辑与OAuth2.1完全一致,同样使用/oauth2/revoke端点。只要令牌由该服务器颁发、客户端拥有TOKEN_REVOCATION权限,即可按上述步骤完成撤销。

若使用的是旧版Spring Security OAuth授权服务器,撤销端点为/oauth/revoke_token,请求参数与认证方式保持一致。

三、查询授权服务器所有端点

你可以通过两种方式获取完整端点列表:

  • 默认核心端点清单:Spring Authorization Server默认提供以下核心端点:
    • /oauth2/authorize:授权请求端点
    • /oauth2/token:令牌获取端点
    • /oauth2/revoke:令牌撤销端点
    • /oauth2/introspect:令牌校验端点
    • /oauth2/jwks:JWKS密钥集端点
    • /oauth2/logout:用户注销端点(需额外配置)
  • 通过Actuator查看全量映射:添加Spring Boot Actuator依赖后,启用mappings端点,访问/actuator/mappings即可查看应用内所有注册端点,包括授权服务器的自定义或默认端点。

内容的提问来源于stack exchange,提问作者Thirumal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 08:25:07