【问题标题】:Retrieving user info in oauth2 resource server在 oauth2 资源服务器中检索用户信息
【发布时间】: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


    【解决方案1】:

    要对用户进行身份验证,请查看 GitLab as OpenID Connect identity provider。 OpenID Connect 是在 OAuth2 之上实现的配置文件,用于对用户进行身份验证。

    【讨论】:

    • 我认为请求已经过身份验证,我只是缺少有关 Prinicipal / SecurityContextHolder 中的身份验证用户的信息,我不知道我应该如何设置这些。
    猜你喜欢
    • 2022-09-28
    • 1970-01-01
    • 2018-08-03
    • 2017-12-15
    • 2023-04-10
    • 2013-12-21
    • 2018-04-14
    • 1970-01-01
    • 2011-03-06
    相关资源
    最近更新 更多