如何在 Spring Webflux WebClient 中处理令牌刷新

卡米尔

我想创建一些用于 的身份验证服务WebClient,因此它会在需要时自动刷新令牌:

@Service
public class AuthService {

    private String token;

    private final WebClient webClient;

    private final Map<String, String> bodyValues;

    @Autowired
    public AuthService(WebClient webClient) {
        this.webClient = webClient;
        this.bodyValues = new HashMap<>();
        this.bodyValues.put("user", "myUser");
        this.bodyValues.put("password", "somePassword");
    }

    public String getToken() {
        if (this.token == null || this.isExpired(this.token) {
            this.refreshToken();
        } 
        return this.token;
    }


    private void refreshToken() {
        this.token = webClient.post()
                .uri("authEndpointPath")
                .contentType(MediaType.APPLICATION_JSON)
                .body(BodyInserters.fromValue(bodyValues))
                .retrieve()
                .bodyToMono(String.class)
                .block();
    }

    private boolean isExpired() {
        //implementation
    }
}

过期时它会正确获取令牌。有没有办法只使用一次,而不将其注入其他服务?我正在考虑定义Beanwhich usesauthService.getToken()方法:

@Configuration
public class CustomWebClientConfig {

    private final AuthService authService;

    @Autowired
    public CustomWebClientConfig(AuthService authService) {
        this.authService = authService;
    }  
  
    @Bean("myCustomWebClient")
    WebClient webClient() {
        return WebClient.builder()
                .defaultHeader("Access-Token", authService.getToken())
                .build()
    }
}

但显然它只会在应用程序启动时获得一次令牌。有没有办法以某种方式注入它或拦截所有 webclient 请求并添加令牌?

谢苗·基列科夫

您可以声明一个自定义过滤器WebClient,该过滤器应用于每个请求。

@Configuration
public class CustomWebClientConfig {

    private final AuthService authService;

    @Autowired
    public CustomWebClientConfig(AuthService authService) {
        this.authService = authService;
    }  
  
    @Bean("myCustomWebClient")
    WebClient webClient() {
        return WebClient.builder()
                .filter(ExchangeFilterFunction.ofRequestProcessor(
                    (ClientRequest request) -> Mono.just(
                        ClientRequest.from(request)
                            .header("Access-Token", authService.getToken())
                            .build()
                    )
                ))
                .build();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring WebFlux WebClient处理ConnectTimeoutException

如何在Webflux WebClient中自定义异常?

如何通过循环在 spring-webflux 中的 WebClient 中设置不同的主体?

如何限制spring-webflux WebClient中打开套接字的数量?

Spring WebFlux WebClient:延迟执行

Spring WebFlux WebClient-如何解决400错误请求

对于Spring WebFlux的WebClient,如何捕获Netty的异常(例如ProxyConnectException)

如何在 Spring WebFlux 中设置 ViewResolver

如何在Spring WebClient中设置和处理超时?

Spring Webflux - 基于 TLSv1.2 的 WebClient

How to set a timeout in Spring 5 WebFlux WebClient

使用WebClient Spring WebFlux的多个请求

spring WebClient如何从使用http / 1.1的spring webflux服务器接收流数据

Java Spring Webflux - WebClient .onStatus((HttpStatus::isError) 返回错误处理

如何在Spring Security中刷新令牌

Spring Webflux:如何配置Controller和WebClient使其像代理一样工作?

Spring WebFlux - 如何使用 WebClient 将响应打印为字符串而不是对象

在将响应返回给调用方时,如何注销对Spring WebFlux WebClient请求的失败响应的主体?

如何将Hystrix与Spring WebFlux WebClient一起使用?

从Spring WebFlux Web应用程序中的WebClient调用中缓存Mono的结果

Spring WebFlux-通过WebClient为Mono内列表中的每个元素发送HTTP请求

如何在Spring Webflux中获取引荐来源网址?

如何在Spring WebFlux中获取语言环境?

如何在Spring WebFlux中记录请求和响应正文

如何在Spring WebFlux中整合Mono Objects的结果?

如何在Spring WebClient中拦截HTTP通信?

如何在Spring WebClient构建器实例中设置onStatus

如何在Spring WebClient中禁用主机名验证?

如何在 Spring Webclient 中调用 onStatus() 内部的方法