Spring Boot Feign 客户端 - 拦截器不工作

发布于 2025-01-15 23:14:52 字数 1713 浏览 1 评论 0原文

我有假客户端拦截器,它添加了 Auth 标头(由 RestTemplate 获取的不记名令牌)。如果服务器响应 401(令牌过期),我想重新进行身份验证并再次尝试该请求,但拦截器不会第二次触发。

拦截器代码:

        @Override
        public void apply(RequestTemplate requestTemplate) {
            if (AuthenticationService.bearerToken == null)
                authenticationService.authenticate();
            requestTemplate.header(AUTHORIZATION, BEARER_TOKEN_PREFIX + AuthenticationService.bearerToken );
        }

错误解码器:

@Override
    public Exception decode(String s, Response response) {
        FeignException exception = feign.FeignException.errorStatus(s, response);
        switch (response.status()) {
            case 401:
                authenticationService.authenticate();
                return new RetryableException(response.status(), exception.getMessage(), response.request().httpMethod(), exception, null, response.request());
            case 500:
                throw new BadActionException(s, response.reason());
            default:
                break;
        }
        return exception;
    }

客户端配置类:

@Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor (authenticationService);
    }

@Bean
public RestClientDecoder restClientDecoder() {
    return new RestClientDecoder(authenticationService);
}

Feign 客户端:

@FeignClient(value = "server", url = "${server.base-url}", configuration = RestClientConfig.class)
public interface RestClient {

    @PostMapping("api/test/{id}/confirm")
    void test(@PathVariable Long id);

}

旁注: 除了 oAuth 和 BasicAuth 之外,是否有内置拦截器用于身份验证?我正在通信的服务器有简单的 jwt 身份验证,并且有过期时间。

I have feign client interceptor which adds Auth header (bearer token being fetched by RestTemplate). If the server responds with 401 (expired token) I want to reauthenticate and try the request again but the interceptor is not getting triggered 2nd time.

Interceptor code:

        @Override
        public void apply(RequestTemplate requestTemplate) {
            if (AuthenticationService.bearerToken == null)
                authenticationService.authenticate();
            requestTemplate.header(AUTHORIZATION, BEARER_TOKEN_PREFIX + AuthenticationService.bearerToken );
        }

Error decoder:

@Override
    public Exception decode(String s, Response response) {
        FeignException exception = feign.FeignException.errorStatus(s, response);
        switch (response.status()) {
            case 401:
                authenticationService.authenticate();
                return new RetryableException(response.status(), exception.getMessage(), response.request().httpMethod(), exception, null, response.request());
            case 500:
                throw new BadActionException(s, response.reason());
            default:
                break;
        }
        return exception;
    }

Client config class:

@Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor (authenticationService);
    }

@Bean
public RestClientDecoder restClientDecoder() {
    return new RestClientDecoder(authenticationService);
}

Feign client:

@FeignClient(value = "server", url = "${server.base-url}", configuration = RestClientConfig.class)
public interface RestClient {

    @PostMapping("api/test/{id}/confirm")
    void test(@PathVariable Long id);

}

Side note: is there built in interceptor for authentication other than oAuth and BasicAuth? The server I am communicating with has simple jwt auth with expiration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文