Spring 正在尝试将Oauth2令牌与假客户端和hystrix一起使用
我正在尝试从“ServiceA”调用“ServiceB”,这两个服务都是资源服务器,我正在尝试通过“外部客户端和OAuth2 toke”进行此服务间调用,该调用与配置类中的以下bean实现配合良好:Spring 正在尝试将Oauth2令牌与假客户端和hystrix一起使用,spring,cloud,hystrix,netflix-feign,oauth2,Spring,Cloud,Hystrix,Netflix Feign,Oauth2,我正在尝试从“ServiceA”调用“ServiceB”,这两个服务都是资源服务器,我正在尝试通过“外部客户端和OAuth2 toke”进行此服务间调用,该调用与配置类中的以下bean实现配合良好: @Bean public RequestInterceptor requestTokenBearerInterceptor() { return new RequestInterceptor() { @Override public void ap
@Bean
public RequestInterceptor requestTokenBearerInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)
SecurityContextHolder.getContext().getAuthentication()
.getDetails();
requestTemplate.header("Authorization",
"bearer " + details.getTokenValue());
}
};
}
当我尝试使用带回退功能的假客户机时,即没有OAuth令牌的Hystrix(即当没有任何服务是资源服务器时),这也可以正常工作
但是,尽管试图同时使用其中三个(即,佯装客户端、Hystrix和OAuth2),但它不起作用。尽管所有服务都已启动并正在运行,但每次它都将使用回退方法
以下是我的代码:
App.java
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import feign.RequestInterceptor;
import feign.RequestTemplate;
@SpringBootApplication
@RestController
@EnableFeignClients
@EnableEurekaClient
@EnableCircuitBreaker
public class App
{
/*@Autowired
@Qualifier("abc")
private GitHubClient gitHub;*/
@Autowired
private CallService callService;
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
@RequestMapping(value="/feign",method=RequestMethod.POST,consumes="application/json")
public String contributors1(@RequestBody JSONObject payLoad) {
String callservice2 = callService.callservice(payLoad);
return callservice2;
}
@Bean
public RequestInterceptor requestTokenBearerInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)
SecurityContextHolder.getContext().getAuthentication()
.getDetails();
requestTemplate.header("Authorization",
"bearer " + details.getTokenValue());
}
};
}
}
Callervice.java
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class CallService {
@Autowired
@Qualifier("abc")
private GitHubClient gitHub;
public String callservice(JSONObject payLoad){
String forObject =gitHub.contributors(payLoad);
return forObject;
}
}
HystrixWrappedClient.java
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Component("abc")
public class HystrixWrappedClient implements GitHubClient{
@Autowired
@Qualifier("gitHubClient")
private GitHubClient gitHub;
@Override
@HystrixCommand(fallbackMethod="failure")
public String contributors(JSONObject payLoad) {
return gitHub.contributors(payLoad);
}
public String failure(JSONObject payLoad){
System.out.println(payLoad);
return "Failure";
}
}
import org.json.simple.JSONObject;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("MockRestService")
interface GitHubClient {
@RequestMapping(method = RequestMethod.POST, value = "/test",consumes="application/json")
String contributors(@RequestBody JSONObject payLoad);
}
GitHubClient.java
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Component("abc")
public class HystrixWrappedClient implements GitHubClient{
@Autowired
@Qualifier("gitHubClient")
private GitHubClient gitHub;
@Override
@HystrixCommand(fallbackMethod="failure")
public String contributors(JSONObject payLoad) {
return gitHub.contributors(payLoad);
}
public String failure(JSONObject payLoad){
System.out.println(payLoad);
return "Failure";
}
}
import org.json.simple.JSONObject;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("MockRestService")
interface GitHubClient {
@RequestMapping(method = RequestMethod.POST, value = "/test",consumes="application/json")
String contributors(@RequestBody JSONObject payLoad);
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
<!-- For swagger documenation -->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<!-- NEW -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>1.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
org.springframework.boot
spring启动程序父级
1.2.5.1发布
com.googlecode.json-simple
简单json
1.1.1
org.springframework.cloud
spring云配置客户端
1.0.0.1版本
org.springframework.boot
弹簧靴起动器执行器
org.springframework.cloud
春云起动器尤里卡
1.0.1.1发布
芒果工厂
大摇大摆
1.0.2
org.springframework.cloud
SpringCloudStarter安全
org.springframework.cloud
春云
org.springframework.security.oauth
spring-security-oauth2
org.springframework.cloud
春云起动器
org.springframework.cloud
SpringCloudStarter父级
1.0.1.1发布
聚甲醛
进口
请建议。每当试图同时使用FaignClient、OAuth2和Hystrix时,它总是使用回退方法。u可以研究一下这个问题
Hystrix在另一个/不同的线程中执行,通过HystrixRequestVariableDefault传输您的安全上下文您是否以某种方式与Hystrix共享了Spring安全上下文 由于Spring Cloud Netflix 1.2.0可以使用i.e config param启用与Hystrix的安全上下文共享
hystrix.shareSecurityContext:true