oauth2 资源服务器是否应该询问身份验证服务器上的 Userinfo 端点

Jon*_*sen 4 spring-security spring-boot spring-security-oauth2 openid-connect

在 Spring Boot 中创建资源服务器以保护我的 api 端点时,我使用的是 spring-boot-starter-oauth2-resource-server 并且它不会尝试从身份验证服务器上的 userinfo 端点撤回声明。我想知道这是否是预期的行为,如果是,我应该使用另一个库来为我的资源服务器设置 spring 安全性吗?似乎调试该模块从众所周知的信息中提取信息,并且应该能够轻松知道 userinfo 端点。

这是我正在使用的当前依赖项,也许我只是缺少一些我不知道的模块。

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>openid-resource</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>openid-resource</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
Run Code Online (Sandbox Code Playgroud)

jzh*_*aux 7

NatFar 的答案在金钱上是正确的,但我想我会添加一些我无法发表评论的颜色。

事实上,资源服务器是关于授权的,但 API 提供了钩子让你能够自定义它,调用其中的一个 userinfo 端点。

从 Spring Security 5.1 开始:

@Override
protected void configure(HttpSecurity http) {
    http
        .oauth2ResourceServer()
             .jwt()
                 .jwtAuthenticationConverter(new MyConverter());
}

private static class MyConverter
    implements Converter<Jwt, AbstractAuthenticationToken> {

    @Override
    public AbstractAuthenticationToken convert(Jwt jwt) {
        // invoke the userinfo endpoint
        // construct an Authentication statement from the response
    }

}
Run Code Online (Sandbox Code Playgroud)

Spring Security 5.1 仅支持 JWT,但在 Spring Security 5.2(几周后将推出 GA)中,它也支持不透明令牌。它还对表示进行了一些概括:

@Override
protected void configure(HttpSecurity http) {
    http
        .oauth2ResourceServer()
             .opaqueToken()
                 .introspector(new MyIntrospector());
}

private static class MyIntrospector implements OpaqueTokenIntrospector {

    @Override
    public OAuth2AuthenticatedPrincipal introspect(String token) {
        // invoke the userinfo endpoint
        // construct an OAuth2AuthenticatedPrincipal from the response
    }
}
Run Code Online (Sandbox Code Playgroud)

添加了一张票来获取围绕您的用例添加的文档;然而,已经存在的 JWT-introspection 示例相当接近。


Nat*_*Far 5

查看Spring 参考资料中关于资源服务器的内容:

\n\n
\n

资源服务器需要调用用户信息端点,这是非典型的。这是因为从根本上来说,资源服务器是关于授权请求,而不是对其进行身份验证

\n
\n\n

通常,客户端应用程序查询用户信息端点以获取有关用户的更多信息。

\n\n

但是,如果您使用旧的 Spring Security OAuth,该参考资料将继续展示如何配置资源服务器来调用用户信息端点。

\n\n

然而,在 Spring Security 5 中,您似乎只能通过.oauth2Client()或使用用户信息端点.oauth2Login()

\n\n

参考文献指出,是客户端发出对用户信息的请求。

\n

  • 该文档用于使用 spring-security-oauth2-autoconfigure 我正在使用 spring-boot-starter-oauth2-resource-server ,它使用 spring-security-oauth2-resource-server 。为什么我应该使用 spring-security-oauth2-autoconfigure 而不是我正在使用的?当您使用 Spring Boot Initializer 创建资源服务器时,它使用我正在使用的内容。 (2认同)