getrefreshtoken()

vm0i2vca  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(232)

我有一个spring应用程序,它使用key斗篷作为授权服务器,并使用oauth2库来处理登录和注销流。
我有以下Reactwebflux配置类:

在调用oauth2login().authenticationsuccesshandler时,我将遍历oauth2loginauthenticationwebfilter筛选器类和方法onauthenticationsuccess。问题是这个类没有返回原始对象authenticationresult中的对象oauth2refreshtoken。

我试图通过http.addfilterat方法添加一个自定义web筛选器来覆盖这个行为,如下所示:公共类customloginwebfilter扩展authenticationwebfilter{private final serveroauth2authorizedclientrepository authorizedclientrepository;

/**
 * Creates an instance
 *
 * @param authenticationManager      the authentication manager to use
 * @param authorizedClientRepository
 */
public CustomLoginWebFilter(
        ReactiveAuthenticationManager authenticationManager,
        ServerOAuth2AuthorizedClientRepository authorizedClientRepository) {
    super(authenticationManager);
    Assert.notNull(authorizedClientRepository, "authorizedClientService cannot be null");
    this.authorizedClientRepository = authorizedClientRepository;
}

@Override
protected Mono<Void> onAuthenticationSuccess(Authentication authentication,
                                             WebFilterExchange webFilterExchange) {
    OAuth2LoginAuthenticationToken authenticationResult = (OAuth2LoginAuthenticationToken) authentication;
    OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(
            authenticationResult.getClientRegistration(),
            authenticationResult.getName(),
            authenticationResult.getAccessToken(),
            authenticationResult.getRefreshToken());
    return this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, authenticationResult, webFilterExchange.getExchange())
                                          .then(super.onAuthenticationSuccess(authenticationResult, webFilterExchange));
}

}
问题是我必须从securityconfig类中传递2个bean身份验证管理器(delegatingreactiveauthenticationmanager的示例)和客户机存储库(oauth2authorizedclientrepository),这是非常痛苦的,因为我必须从头开始重新定义所有bean以及后面的所有配置。
http.addfilterat(new customloginwebfilter(reactiveauthenticationmanager(),authorizedclientrepository()),securitywebfiltersorder.authentication);
有没有办法重用参数serverhttpsecurity http在springsecurityfilterchain方法中已经创建的bean?我真的必须使用seucirtyconfig类中的bean重新实现所有对象吗?

@Bean
ReactiveAuthenticationManager reactiveAuthenticationManager() {
    // final ReactiveUserDetailsService detailsService = userDetailsService();
    LinkedList<ReactiveAuthenticationManager> managers = new LinkedList<>();
    managers.add(authentication -> {
        return Mono.empty();
    });
    // managers.add(new UserDetailsRepositoryReactiveAuthenticationManager(detailsService));
    return new DelegatingReactiveAuthenticationManager(managers);
}

有人已经做了我想做的事吗?
它不需要是一个定制的过滤器,它可以是另一个spring过滤器类,比如oauth2authorizationcodegrantwebfilter,它包含我正在寻找的刷新令牌对象。
最后一个目标是让oauth2自动刷新access\u令牌,这在今天不是这样,因为我没有在authenticationsuccesshandler部分中获得oauth2refreshtoken对象。
谢谢分享你的代码。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题