在 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>
最佳答案
NatFar 的答案是正确的,但我想我会添加一些我无法放入评论的颜色。
实际上,资源服务器是关于授权的,但是 API 提供了钩子(Hook)让您能够自定义它,调用其中的 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)中,它也支持不透明 token 。它还稍微概括了表示:
@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那已经相当接近了。
关于spring-boot - oauth2 资源服务器是否应该询问身份验证服务器上的 Userinfo 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57897281/