spring oauth server能获取所有端点信息的接口

spring oauth server能获取所有端点信息的接口

在 Spring Authorization Server(Spring Boot 3.x)中,默认没有直接提供一个接口来返回所有 OAuth2 相关端点的完整列表(类似 Springfox Swagger 的 /v3/api-docs)。但你可以通过以下几种方式获取 OAuth2 端点的信息:


方法 1:通过 Spring Boot Actuator 暴露端点信息

如果你集成了 Spring Boot Actuator,可以查看 /actuator 来获取已注册的端点(包括 OAuth2 相关路径)。

步骤

  1. 添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
  2. 配置 application.yml

    management:
      endpoints:
        web:
          exposure:
            include: "*"  # 暴露所有端点
      endpoint:
        health:
          show-details: always
    
  3. 访问 /actuator

    GET http://localhost:8080/actuator
    

    返回示例:

    {
      "_links": {
        "self": { "href": "/actuator", "templated": false },
        "health": { "href": "/actuator/health", "templated": false },
        "oauth2Authorize": { "href": "/oauth2/authorize", "templated": false },
        "oauth2Token": { "href": "/oauth2/token", "templated": false },
        "jwks": { "href": "/oauth2/jwks", "templated": false }
      }
    }
    

    注意:Actuator 默认不会显示所有 OAuth2 端点(如 /userinfo),仅显示已注册的路径。


方法 2:通过代码获取 OAuth2 端点

如果你需要动态获取 Spring Authorization Server 的所有端点,可以通过注入 OAuth2AuthorizationServerConfigurer 获取配置的端点路径:

示例代码

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration;
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class OAuth2EndpointController {

    private final HttpSecurity httpSecurity;

    public OAuth2EndpointController(HttpSecurity httpSecurity) {
        this.httpSecurity = httpSecurity;
    }

    @GetMapping("/oauth2-endpoints")
    public Map<String, String> getOAuth2Endpoints() throws Exception {
        OAuth2AuthorizationServerConfigurer configurer = new OAuth2AuthorizationServerConfigurer();
        httpSecurity.apply(configurer);

        Map<String, String> endpoints = new HashMap<>();
        endpoints.put("authorization_endpoint", configurer.getAuthorizationEndpointMatcher().getPattern());
        endpoints.put("token_endpoint", configurer.getTokenEndpointMatcher().getPattern());
        endpoints.put("jwks_endpoint", configurer.getJwkSetEndpointMatcher().getPattern());
        endpoints.put("token_introspection_endpoint", configurer.getTokenIntrospectionEndpointMatcher().getPattern());
        endpoints.put("token_revocation_endpoint", configurer.getTokenRevocationEndpointMatcher().getPattern());
        
        // 如果是 OpenID Connect,还可以添加 /userinfo
        endpoints.put("userinfo_endpoint", "/userinfo");

        return endpoints;
    }
}

访问 GET /oauth2-endpoints 返回示例:

{
  "authorization_endpoint": "/oauth2/authorize",
  "token_endpoint": "/oauth2/token",
  "jwks_endpoint": "/oauth2/jwks",
  "token_introspection_endpoint": "/oauth2/introspect",
  "token_revocation_endpoint": "/oauth2/revoke",
  "userinfo_endpoint": "/userinfo"
}

方法 3:通过 OpenID Connect Discovery 端点

如果你的 OAuth2 服务器支持 OpenID Connect,可以通过标准的 .well-known/openid-configuration 端点获取元数据(包括 OAuth2 端点)。

访问方式

GET http://localhost:8080/.well-known/openid-configuration

返回示例:

{
  "issuer": "http://localhost:8080",
  "authorization_endpoint": "http://localhost:8080/oauth2/authorize",
  "token_endpoint": "http://localhost:8080/oauth2/token",
  "jwks_uri": "http://localhost:8080/oauth2/jwks",
  "userinfo_endpoint": "http://localhost:8080/userinfo",
  "revocation_endpoint": "http://localhost:8080/oauth2/revoke",
  "introspection_endpoint": "http://localhost:8080/oauth2/introspect"
}

注意:此端点需要额外配置才能启用(Spring Authorization Server 默认不自动提供)。

如何启用 OpenID Connect Discovery 端点?

SecurityConfig 中配置:

@Bean
@Order(1)
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
    OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
    http.getConfigurer(OAuth2AuthorizationServerConfigurer.class)
        .oidc(oidc -> oidc
            .providerConfigurationEndpoint(provider -> provider
                .issuer("http://localhost:8080") // 设置 Issuer URL
            )
        );
    return http.build();
}

总结

方法 适用场景 访问方式
Actuator 查看已注册的端点 /actuator
自定义接口 动态获取所有 OAuth2 端点 /oauth2-endpoints
OIDC Discovery 标准化的元数据端点 /.well-known/openid-configuration

推荐使用 OpenID Connect Discovery(方法 3),它是标准化方案,兼容性最好。如果仅需简单查看端点,用 Actuator(方法 1)即可。

posted @ 2025-05-21 18:58  明月心~  阅读(121)  评论(0)    收藏  举报