【问题标题】:Configure Spring Boot to use Custom Access Token converter配置 Spring Boot 以使用自定义访问令牌转换器
【发布时间】:2020-02-10 07:55:00
【问题描述】:

我正在尝试从身份提供者提供的访问令牌中获取用户信息。我正在使用的身份提供程序以字符串而不是列表的形式提供它的范围,因为 DefaultAccessTokenConverter 对我不起作用。因此,我希望将其扩展到 CustomAccessTokenConverter 以覆盖它的 extractAuthentication() 方法。我在我的安全配置中使用以下内容来使 Spring 使用这个自定义类而不是默认类:

@Configuration
@EnableResourceServer
public class SecurityConfig extends ResourceServerConfigurerAdapter {
    @Autowired
    private CustomAccessTokenConverter customAccessTokenConverter;

    // For validating the incoming access token and fetching user information from it
    @Bean
    public ResourceServerTokenServices createResourceServerTokenServices() {
        RemoteTokenServices tokenServices = new RemoteTokenServices();
        tokenServices.setCheckTokenEndpointUrl(*Introspection URL*);
        tokenServices.setClientId(*Client ID*);
        tokenServices.setClientSecret(*Client Secret*);
        return tokenServices;
    }

    @Bean
    public AccessTokenConverter accessTokenConverter() {
        return customAccessTokenConverter;
    }
}

但是,Spring 仍然使用 DefaultAccessTokenConverter。我究竟做错了什么?请帮帮我。

这是我的 CustomAccessTokenConverter 类的外观,仅供参考:

@Component
public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {

    @Override
    public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
        .
        .
        .
        return new OAuth2Authentication(request, user);
    }
}

我正在使用具有以下依赖项的 Spring Boot:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.0.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-oauth2-resource-server</artifactId>
    <version>5.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

【问题讨论】:

  • 我在起草问题时想通了,我一直在考虑是否有一个小时,然后我放弃并开始起草这个问题,尽管在添加资源令牌服务的代码时我想通了这里可能有一些东西可以添加访问令牌转换器,幸运的是它确实有。我不想放弃草稿,因为我已经研究了 10 分钟,所以我继续发布问题和答案。

标签: spring spring-boot oauth-2.0 spring-security-oauth2


【解决方案1】:

ResourceTokenServices 允许我们使用自己的 AccessTokenConverter。

只需将以下内容添加到您的安全配置中:

@Bean
public ResourceServerTokenServices createResourceServerTokenServices() {
    RemoteTokenServices tokenServices = new RemoteTokenServices();
    tokenServices.setCheckTokenEndpointUrl(*Introspection URL*);
    tokenServices.setClientId(*Client ID*);
    tokenServices.setClientSecret(*Client Secret*);
    // ADD THE NEXT LINE
    tokenServices.setAccessTokenConverter(customAccessTokenConverter);
    return tokenServices;
} 

【讨论】:

    猜你喜欢
    • 2020-02-08
    • 2019-07-14
    • 2018-05-12
    • 2020-03-01
    • 2022-01-14
    • 2018-12-24
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多