我正在尝试使用 spring cloud feign 创建一个简单的 REST 客户端来使用受 OAuth2 安全令牌保护的服务。我正在使用 OAuth2FeignRequestInterceptor 添加不记名令牌,请检查我的以下代码。我正面临 401。当尝试调试我的代码时,我在我的 Request 对象中找不到承载令牌。
@Configuration
@EnableConfigurationProperties(value=OAuth2ClientCredentialsProperties.class)
@EnableOAuth2Client
@Profile(OAuth2Profiles.CLIENT_CREDENTIALS)
public class ClientCredentialsConfiguration {
@Autowired
private OAuth2ClientCredentialsProperties oAuth2ClientCredentialsProperties;
@Bean
@Qualifier("ClientCredentialsOAuth2FeignRequestInterceptor")
public OAuth2FeignRequestInterceptor oauth2schemeRequestInterceptor() {
return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), oauth2schemeResourceDetails());
}
@Bean
public ClientCredentialsResourceDetails oauth2schemeResourceDetails() {
ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
details.setClientId(oAuth2ClientCredentialsProperties.getClientId());
details.setClientSecret(oAuth2ClientCredentialsProperties.getClientSecret());
details.setAccessTokenUri(oAuth2ClientCredentialsProperties.getAccessTokenUri());
details.setScope(oAuth2ClientCredentialsProperties.getScopes());
return details;
}
}
这是我的客户端界面
@FeignClient(name = "test", url = "http://localhost:8080", configuration = ClientCredentialsConfiguration.class)
interface GitHubClient {
@RequestMapping(value = "/api/v1/products",
produces = "application/json",
consumes = "application/json;charset=UTF-8",
method = RequestMethod.POST)
ResponseEntity<Object> testUsingPOST(@RequestBody TestDTO testDTO);
我的属性在下面
server.port=10080
security.user.name=admin
security.user.password=admin
security.basic.enabled=false
org.springframework.boot.autoconfigure.EnableAutoConfiguration=sgcib.clips.bcsapi.configuration.ClientCredentialsConfiguration
feign.oauth2.enabled=true
feign.hystrix.enabled=false
我的主课
@SpringBootApplication
@EnableWebMvc
@Controller
@EnableFeignClients
@EnableAutoConfiguration
public class App extends SpringBootServletInitializer {
@Autowired
private GitHubClient gitHub;
@RequestMapping("/")
public String home() {
return "index";
}
@RequestMapping("/{owner}")
@ResponseBody
public ResponseEntity<Object> contributors(@PathVariable String owner) {
return gitHub.productsUsingPOST(new TestDTO());
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}