gpt4 book ai didi

java - 在 Spring 上下文中覆盖默认的 OAuth2RestTemplate

转载 作者:行者123 更新时间:2023-11-30 07:29:52 24 4
gpt4 key购买 nike

我正在使用 spring spring security oAuth2 客户端。并且有默认的 OAuth2RestTemplate 声明,如下所示

@Bean
@Primary
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext oauth2ClientContext,
OAuth2ProtectedResourceDetails details) {
OAuth2RestTemplate template = new OAuth2RestTemplate(details,
oauth2ClientContext);
return template;
}

我需要的是提供自定义错误处理,因此我尝试将其放入上下文中

@Bean
public OAuth2RestTemplate restTemplate(OAuth2ClientContext oauth2ClientContext,
OAuth2ProtectedResourceDetails details) {
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(details, oauth2Context);

restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
// do some stuff
}
});

return restTemplate;
}

这里的问题是默认实现被注释为@Primary,所以我想知道如何覆盖它?

最佳答案

您可能只需获取它的默认实例并设置您需要的所有属性,而不是覆盖默认实现。请参阅下面的示例。

@Configuration
public class OverridenConfiguration {
@Autowire
private OAuth2RestTemplate restTemplate;

@PostConstruct
public void customSettings() {
System.out.println("************** custom settings ***********");
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
// do some stuff
}
});
}
}

关于java - 在 Spring 上下文中覆盖默认的 OAuth2RestTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36309791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com