【发布时间】:2021-03-06 16:56:36
【问题描述】:
我一直在开发一个使用 OAuth2 登录的网络应用程序,但我不太确定是否应该根据我的资源服务器中的 access_token 来识别用户。我认为我错过了一些关键步骤,或者我在实施中做错了什么。我使用 Gitlab 作为 OAuth2.0 提供者,使用 Spring boot 作为 API 和资源服务器,当然还有 Web 客户端。
资源服务器配置:
@SpringBootApplication
@EnableResourceServer
public class MyApi {
public static void main(String[] args) {
SpringApplication.run(MyApi.class, args);
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends ResourceServerConfigurerAdapter {
@Value("${client.address}")
private String clientAddress;
@Value("${oauth2.gitlab.client.id}")
private String clientId;
@Value("${oauth2.gitlab.client.secret}")
private String clientSecret;
@Value("${oauth2.gitlab.check_token}")
private String checkTokenUri;
@Override
public void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.anyRequest().authenticated();
}
@Override
public void configure(final ResourceServerSecurityConfigurer resources) throws Exception {
resources
.resourceId("user")
.tokenServices(tokenService());
}
@Primary
@Bean
public RemoteTokenServices tokenService() {
RemoteTokenServices tokenService = new RemoteTokenServices();
tokenService.setClientId(clientId);
tokenService.setClientSecret(clientSecret);
tokenService.setCheckTokenEndpointUrl(checkTokenUri);
return tokenService;
}
}
我覆盖了 RemoteTokenServices,我使用的端点是 /oauth/introspect。使用该设置,到达我的 API 的每个 HTTP 请求都必须经过身份验证,并且必须具有有效的 access_token。问题是该端点返回以下响应。似乎基于此,我只能检查请求是否经过身份验证,而不是它的所有者是谁,我应该以某种方式开始在我的资源服务器中使用 user_info_endpoint 。谁能让我走上正轨?
{
"active": true,
"scope": "api",
"client_id": "my-client-api",
"token_type": "Bearer",
"exp": 0,
"iat": 1606119799
}
【问题讨论】:
标签: spring-boot spring-security oauth-2.0 gitlab spring-security-oauth2