Question
I have a problem about sending a request to other service from one service. All services have security. That's why you couldn't send a request to url without the bearer token.
I after getting bearer token of ROLE_USER
from this url
localhost:9090/authenticate/login
I defined bearer token in getOrderDetails of Order Service but I got this issue shown below. How can I fix the issue?
org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 : "{ "error": "Full authentication is required to access this resource" }<EOL><EOL>"
Here are problem in the code snippets shown below to call another service.
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;
}
Fixed the issue : After removing the "Bearer" in getOrderDetails of OrderServiceImpl , It works
How can I fix the issue?
Here is the repo : Link
To run the app,
1 ) Run Service Registery (Eureka Server)
2 ) Run config server
3 ) Run zipkin and redis through these commands shown below on docker
docker run -d -p 9411:9411 openzipkin/zipkin
docker run -d --name redis -p 6379:6379 redis
4 ) Run api gateway
5 ) Run other services
Answer
Here is the answer
After removing the "Bearer" in getOrderDetails
of OrderServiceImpl
, It
works