Java 如何将Eureka名称与OAuth2RestTemplate一起使用
我正在使用OAuth2RestTemplate,以便通过REST请求传递oauth令牌。但是,我现在需要硬编码我的URL,例如Java 如何将Eureka名称与OAuth2RestTemplate一起使用,java,spring-boot,spring-security,spring-security-oauth2,netflix-eureka,Java,Spring Boot,Spring Security,Spring Security Oauth2,Netflix Eureka,我正在使用OAuth2RestTemplate,以便通过REST请求传递oauth令牌。但是,我现在需要硬编码我的URL,例如 restTemplate.postForLocation("http://localhost:5555/other-service2/message", "Message") 而当我使用一个自我创建的带注释的Ribbon(使用@LoadBalanced)RESTTemplatebean时,我可以做如下事情 restTemplate.postForLocation("
restTemplate.postForLocation("http://localhost:5555/other-service2/message", "Message")
而当我使用一个自我创建的带注释的Ribbon(使用@LoadBalanced)RESTTemplatebean时,我可以做如下事情
restTemplate.postForLocation("http://service1/other-service2/message", "Message")
这是因为当您使用LoadBalanced时,它会自动使其成为一个Ribbon Rest模板,允许您使用服务发现功能或Eureka,但是当您使用@LoadBalanced注释OAuth2RestTemplate bean时,它会在运行时尝试使用OAuth2RestTemplate时抛出某种错误,即
o.s.b.a.s.o.r.UserInfoTokenServices : Could not fetch user details: class java.lang.IllegalStateException, No instances available for localhost
我的OAuth2RestTemplate创建看起来像
@LoadBalanced
@Bean
public OAuth2RestTemplate restTemplate(final UserInfoRestTemplateFactory factory) {
final OAuth2RestTemplate userInfoRestTemplate = factory.getUserInfoRestTemplate();
return userInfoRestTemplate;
}
如何在OAuth2RestTemplate上使用Eureka Ribbon的服务发现功能和负载平衡功能?我想您可以尝试一下 在我的项目中,我们还使用OAuth2、Eureka和Ribbon来实现微服务之间的通信。为了将Ribbon与OAuth2一起使用,我们采用的方法略有不同 首先,我们让rest模板保持不变
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
然而,我们创建了实现RequestIntercepter的假ClientIntercepter,它在通过restTemplate发出请求时为OAuth设置授权令牌
@Component
public class UserFeignClientInterceptor implements RequestInterceptor {
private static final String AUTHORIZATION_HEADER = "Authorization";
private static final String BEARER_TOKEN_TYPE = "Jwt";
@Override
public void apply(RequestTemplate template) {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
if (authentication != null && authentication
.getDetails() instanceof OAuth2AuthenticationDetails) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication
.getDetails();
template.header(AUTHORIZATION_HEADER,
String.format("%s %s", BEARER_TOKEN_TYPE, details.getTokenValue()));
}
}
}
@FeignClient("your-project-name")
public interface YourProjectClient {
@GetMapping("your-endpoint")
JsonObject getSomething();
如果您试图创建SpringMSA项目,我更喜欢使用外部客户机而不是restTemplate
@Component
public class UserFeignClientInterceptor implements RequestInterceptor {
private static final String AUTHORIZATION_HEADER = "Authorization";
private static final String BEARER_TOKEN_TYPE = "Jwt";
@Override
public void apply(RequestTemplate template) {
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
if (authentication != null && authentication
.getDetails() instanceof OAuth2AuthenticationDetails) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication
.getDetails();
template.header(AUTHORIZATION_HEADER,
String.format("%s %s", BEARER_TOKEN_TYPE, details.getTokenValue()));
}
}
}
@FeignClient("your-project-name")
public interface YourProjectClient {
@GetMapping("your-endpoint")
JsonObject getSomething();
很好,我明天上班时会测试,但这似乎是一个可行的解决方案,所以被接受了。。实际上,请求似乎没有被拦截。还有什么我需要更改或打开的吗?我需要用那些假@FeignClient(“endpoint”)注释标记我的所有端点吗?很抱歉,我检查你的问题太晚了。明天,我会给你们更多详细的例子。太棒了,我会在工作中尝试一下,并给出反馈!