oauth2资源服务器应在身份验证服务器上询问Userinfo端点

时间:2019-09-11 21:54:12

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

在Spring Boot中创建资源服务器以保护我的api端点时,我使用的是spring-boot-starter-oauth2-resource-server,它不会尝试从身份验证服务器上的userinfo端点撤回声明。我想知道这是否是预期的行为,是否应该使用另一个库为我的资源服务器设置Spring Security?看来,调试该模块可以从知名的应用程序中获取信息,并且应该能够轻松地知道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>

2 个答案:

答案 0 :(得分:2)

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已经很接近了。

答案 1 :(得分:1)

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

  

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

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

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

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

reference指出是用户请求用户信息。