【问题标题】:Should oauth2 Resource Server Interrogate Userinfo Endpoint on Authentication Serveroauth2 资源服务器是否应该询问身份验证服务器上的 Userinfo 端点
【发布时间】:2020-01-13 18:35:41
【问题描述】:

在 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>

【问题讨论】:

    标签: spring-boot spring-security spring-security-oauth2 openid-connect


    【解决方案1】:

    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
        }
    
    }
    

    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
        }
    }
    

    我已经added a ticket 来获取围绕您的用例添加的文档;但是,已经存在的the JWT-introspection example 相当接近。

    【讨论】:

    • 完美。这确实很好地解决了问题。我已经必须创建自己的转换器才能使用自定义 JwtAuthenticationToken 扩展,以便获取名称使用用户名声明而不是主题。
    • 这里有一些更新的 Spring Security 文档:docs.spring.io/spring-security/site/docs/current-SNAPSHOT/…
    • 谢谢,我去看看。
    【解决方案2】:

    查看Spring reference 对资源服务器的评价:

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

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

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

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

    reference 声明是客户端请求用户信息。

    【讨论】:

    • 该文档用于使用 spring-security-oauth2-autoconfigure 我正在使用 spring-boot-starter-oauth2-resource-server,它使用 spring-security-oauth2-resource-server。为什么我应该使用 spring-security-oauth2-autoconfigure 而不是我正在使用的那个?当您使用 Spring Boot Initializer 创建资源服务器时,它使用我正在使用的。
    • 所以最终会引入 Spring Security 5?
    猜你喜欢
    • 1970-01-01
    • 2018-01-18
    • 2016-01-02
    • 2017-05-25
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多