简体   繁体   English

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

[英]Overriding default OAuth2RestTemplate in Spring context

I'm using the spring spring security oAuth2 client.我正在使用 spring spring security oAuth2 客户端。 And there's default OAuth2RestTemplate declaration which looks like并且有默认的 OAuth2RestTemplate 声明,看起来像

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

What I need is to provide custom error handling so i'm trying to put it into context like我需要的是提供自定义错误处理,所以我试图把它放到上下文中

@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;
}

The issue here is that default implementation is annotated as @Primary and so i'm wondering how that can be overridden?这里的问题是默认实现被注释为@Primary ,所以我想知道如何覆盖它?

Instead of overriding the default implementation you probably can just get it's default instance and set all the properties that you need.而不是覆盖默认实现,您可能只需获取它的默认实例并设置您需要的所有属性。 See the example below.请参阅下面的示例。

@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
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM