Java 如何在Spring4中处理多个ClientHttPrequestInterceptor

Java 如何在Spring4中处理多个ClientHttPrequestInterceptor,java,spring,resttemplate,Java,Spring,Resttemplate,在RestTemplate中,我配置了两个ClientHttpRequestInterceptor(一个用于基本授权,另一个用于基于令牌的身份验证) 从客户端看,我如何要求RestTemplate使用正确的ClientHttpRequestInterceptor来执行API调用 某些API调用需要基本授权才能工作。(例如:如果URL以“/admin”开头,则需要基本授权,其他API调用则需要基于令牌的身份验证) 如何在Spring 4中实现这一点?您可以使用RestTemplate的两个实例,一

在RestTemplate中,我配置了两个ClientHttpRequestInterceptor(一个用于基本授权,另一个用于基于令牌的身份验证)

从客户端看,我如何要求RestTemplate使用正确的ClientHttpRequestInterceptor来执行API调用

某些API调用需要基本授权才能工作。(例如:如果URL以“/admin”开头,则需要基本授权,其他API调用则需要基于令牌的身份验证)


如何在Spring 4中实现这一点?

您可以使用RestTemplate的两个实例,一个用于基本身份验证,另一个用于令牌身份验证

@Bean
@Qualifier("authRestTemplate")
public RestTemplate getAuthTemplate{
    // create rest template, add auth interceptor
}

@Bean
@Qualifier("tokenRestTemplate")
public RestTemplate getTokenTemplate{
    // create rest template, add token interceptor
}
然后,在自动连接RestTemplate时,使用所需的@Qualifier

@Autowired
@Qualifier("authRestTemplate")
private RestTemplate authTemplate;

@Autowired
@Qualifier("tokenRestTemplate")
private RestTemplate tokenTemplate;
另一个选项是在
rest模板中添加两个
clienthttpprequestinterceptor

class BasicAuthInterceptor implements ClientHttpRequestInterceptor {

    private final AuthService authService;

    public BasicAuthHeaderInterceptor(AuthService authService) {
        this.authService = authService;
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        if(isApplicable(request)){
            String token = Base64Utils.encodeToString((authService.getUsername() + ":" + authService.getpassword()).getBytes(Charset.forName("UTF-8")));
            request.getHeaders().add("Authorization", "Basic " + token);
        }
        return execution.execute(request, body);
    }

}

class TokenInterceptor implements ClientHttpRequestInterceptor {

    private final AuthService authService;

    public TokenHeaderInterceptor(AuthService authService) {
        this.authService = authService;
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        if(isApplicable(request)){
            request.getHeaders().add("Authorization", "Bearer " + tokenService.getToken());
        }
        return execution.execute(request, body);
    }

}
@Bean
public RestTemplate restTemplate(){
    RestTemplate template = new RestTemplate();

    template.getInterceptors().add(new BasicAuthInterceptor(authService));
    template.getInterceptors().add(new TokenInterceptor(authService));

    return template;
}
然后,将两个拦截器添加到
RestTemplate

class BasicAuthInterceptor implements ClientHttpRequestInterceptor {

    private final AuthService authService;

    public BasicAuthHeaderInterceptor(AuthService authService) {
        this.authService = authService;
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        if(isApplicable(request)){
            String token = Base64Utils.encodeToString((authService.getUsername() + ":" + authService.getpassword()).getBytes(Charset.forName("UTF-8")));
            request.getHeaders().add("Authorization", "Basic " + token);
        }
        return execution.execute(request, body);
    }

}

class TokenInterceptor implements ClientHttpRequestInterceptor {

    private final AuthService authService;

    public TokenHeaderInterceptor(AuthService authService) {
        this.authService = authService;
    }

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        if(isApplicable(request)){
            request.getHeaders().add("Authorization", "Bearer " + tokenService.getToken());
        }
        return execution.execute(request, body);
    }

}
@Bean
public RestTemplate restTemplate(){
    RestTemplate template = new RestTemplate();

    template.getInterceptors().add(new BasicAuthInterceptor(authService));
    template.getInterceptors().add(new TokenInterceptor(authService));

    return template;
}

我实际上在寻找一个解决方案,其中“handler”类(例如:InterceptorHandler)根据您要调用的URL选择正确的处理程序。