[英]Spring Boot Microservices - Rest Template 401 Unauthorized error
我在从一项服务向其他服务发送请求时遇到问题。 所有服务都有安全性。 这就是为什么没有不记名令牌就无法向 url 发送请求的原因。
我从这个 url localhost:9090/authenticate/login
获得了ROLE_USER
的不记名令牌
我在订单服务的 getOrderDetails 中定义了不记名令牌,但我遇到了如下所示的问题。 我该如何解决这个问题?
org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : "{ "error": "Full authentication is required to access this resource" }<EOL><EOL>"
以下是调用其他服务的代码片段中的问题。
public OrderResponse getOrderDetails(long orderId, String bearerToken) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer "+ bearerToken);
HttpEntity request = new HttpEntity<>(headers);
log.info("OrderServiceImpl | getOrderDetails | Get order details for Order Id : {}", orderId);
Order order
= orderRepository.findById(orderId)
.orElseThrow(() -> new CustomException("Order not found for the order Id:" + orderId,
"NOT_FOUND",
404));
log.info("OrderServiceImpl | getOrderDetails | Invoking Product service to fetch the product for id: {}", order.getProductId());
/*ProductResponse productResponse
= restTemplate.getForObject(
"http://PRODUCT-SERVICE/product/" + order.getProductId(),
ProductResponse.class
);*/
// HERE IS THE ISSUE
ResponseEntity<ProductResponse> responseProduct = restTemplate.exchange(
"http://PRODUCT-SERVICE/product/" + order.getProductId(),
HttpMethod.GET, request, ProductResponse.class);
ProductResponse productResponse = responseProduct.getBody();
log.info("OrderServiceImpl | getOrderDetails | Getting payment information form the payment Service");
/*PaymentResponse paymentResponse
= restTemplate.getForObject(
"http://PAYMENT-SERVICE/payment/order/" + order.getId(),
PaymentResponse.class
);*/
// HERE IS THE ISSUE
ResponseEntity<PaymentResponse> responsePayment = restTemplate.exchange(
"http://PAYMENT-SERVICE/payment/order/" + order.getId(),
HttpMethod.GET, request, PaymentResponse.class);
PaymentResponse paymentResponse = responsePayment.getBody();
OrderResponse.ProductDetails productDetails
= OrderResponse.ProductDetails
.builder()
.productName(productResponse.getProductName())
.productId(productResponse.getProductId())
.build();
OrderResponse.PaymentDetails paymentDetails
= OrderResponse.PaymentDetails
.builder()
.paymentId(paymentResponse.getPaymentId())
.paymentStatus(paymentResponse.getStatus())
.paymentDate(paymentResponse.getPaymentDate())
.paymentMode(paymentResponse.getPaymentMode())
.build();
OrderResponse orderResponse
= OrderResponse.builder()
.orderId(order.getId())
.orderStatus(order.getOrderStatus())
.amount(order.getAmount())
.orderDate(order.getOrderDate())
.productDetails(productDetails)
.paymentDetails(paymentDetails)
.build();
log.info("OrderServiceImpl | getOrderDetails | orderResponse : " + orderResponse.toString());
return orderResponse;
}
修复问题:去除OrderServiceImpl的getOrderDetails中的“Bearer”后,可以正常使用
我该如何解决这个问题?
这是回购协议:链接
要运行该应用程序,
1)运行服务注册(尤里卡服务器)
2)运行配置服务器
3 ) 通过下面显示的这些命令在 docker 上运行 zipkin 和 redis
docker run -d -p 9411:9411 openzipkin/zipkin
docker run -d --name redis -p 6379:6379 redis
4)运行api网关
5)运行其他服务
这是答案
在OrderServiceImpl
的getOrderDetails
中删除“Bearer”后,它起作用了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.