oauth2 资源服务器是否应该在身份验证服务器上询问 Userinfo 端点
Should oauth2 Resource Server Interrogate Userinfo Endpoint on Authentication Server
在 spring 引导中创建资源服务器以保护我的 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 reference 关于资源服务器的内容:
It’s atypical for a resource server to need to call a user info endpoint. This is because, fundamentally, a resource server is about authorizing a request, not authenticating it
通常,是客户端应用程序查询用户信息端点以获取有关用户的更多信息。
但参考继续说明如果您使用旧的 Spring Security OAuth,如何配置资源服务器以调用用户信息端点。
但是,在 Spring Security 5 中,您似乎只能通过 .oauth2Client()
或 [=11 使用用户信息端点=].
reference 指出是客户端请求用户信息。
NatFar 的回答是正确的,但我想我会添加一些我无法放入评论的颜色。
确实,资源服务器是关于授权的,但是 API 为您提供了挂钩,以便能够自定义它,调用其中的 userinfo 端点。
从 Spring 安全 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 to get documentation added around your usecase; however, the JWT-introspection example 已经很接近了。
在 spring 引导中创建资源服务器以保护我的 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 reference 关于资源服务器的内容:
It’s atypical for a resource server to need to call a user info endpoint. This is because, fundamentally, a resource server is about authorizing a request, not authenticating it
通常,是客户端应用程序查询用户信息端点以获取有关用户的更多信息。
但参考继续说明如果您使用旧的 Spring Security OAuth,如何配置资源服务器以调用用户信息端点。
但是,在 Spring Security 5 中,您似乎只能通过 .oauth2Client()
或 [=11 使用用户信息端点=].
reference 指出是客户端请求用户信息。
NatFar 的回答是正确的,但我想我会添加一些我无法放入评论的颜色。
确实,资源服务器是关于授权的,但是 API 为您提供了挂钩,以便能够自定义它,调用其中的 userinfo 端点。
从 Spring 安全 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 to get documentation added around your usecase; however, the JWT-introspection example 已经很接近了。