java 401错误提示401未经授权的错误提示

yhxst69z  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(207)

从一个服务向另一个服务发送请求时遇到了问题。所有服务都有安全性。这就是为什么没有承载令牌你不能向url发送请求的原因。
I在从此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"后,它可以正常工作

如何解决此问题?
以下是回购协议:Link

    • 要运行应用程序,**

1)运行服务注册表(Eureka服务器)
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)运行其他服务

bkhjykvo

bkhjykvo1#

答案在这里
移除OrderServiceImplgetOrderDetails中的“承载者”后,它工作

相关问题