如何设置自定义OAuth2RestTemplate或自定义OAuth2ProtectedResourceDetails?

时间:2016-02-12 13:14:14

标签: java spring spring-security-oauth2

我启用了@ EnableOAuth2Sso的前端应用程序。此外,我想使用自定义OAuth2ProtectedResourceDetails注入所有OAuth2RestTemplate bean(特别是在OAuth2RestOperationsConfiguration中重新定义OAuth2RestTemplate的行为)。 我刚为OAuth2ProtectedResourceDetails定义了自己的bean,但OAuth2RestTemplate仍使用AuthorizationCodeResourceDetails。

@SpringBootApplication(scanBasePackages = {"base.package"})
@EnableOAuth2Sso
@EnableZuulProxy
public class Application extends WebSecurityConfigurerAdapter {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Autowired
private RedirectUriResolver redirectUriResolver;

@Override
public void configure(HttpSecurity http) throws Exception {
    // some http configuration
}

@Bean
public OAuth2ProtectedResourceDetails resource() {
    AuthorizationCodeResourceDetails resource = new CustomAuthorizationCodeResourceDetails(redirectUriResolver);
    return resource;
}
}

我应该以某种方式重新定义整个OAuth2RestTemplate bean,还是只重新定义这个OAuth2ProtectedResourceDetails?

1 个答案:

答案 0 :(得分:1)

您应该可以使用自己的

重载在OAuth2RestOperationsConfiguration中设置的bean
    @Bean
    @ConfigurationProperties("security.oauth2.client")
    public ClientCredentialsResourceDetails oauth2RemoteResource() {
        ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
        return details;
    }

如果没有,只需创建自己的豆

@Bean
@ConfigurationProperties("security.oauth2.client")
public ClientCredentialsResourceDetails customOauth2RemoteResource() {
    ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
    return details;
}

@Bean
public OAuth2RestTemplate customOauth2RestTemplate() {
    OAuth2RestTemplate template = new OAuth2RestTemplate(customOauth2RemoteResource(),
            new DefaultOAuth2ClientContext());
    return template;
}

然后使用@Qualifier来引用它们

@Autowired
@Qualifier("customOauth2RestTemplate")
private RestTemplate restTemplate;