通过此artcle,我实现了在micronaut gradle应用程序中从我的REST API方法调用另一个rest API。由于我的REST API需要jwt token ,因此我发送的是与当前请求中收到的 token 相同的 token 。即使传递 token ,我也看到未经授权的错误。任何人都可以在这方面提供帮助。下面是我的代码。

import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.appter.clientmgmt.models.ClientContact;
import io.appter.clientmgmt.repositories.IClientContactRepository;
import io.micronaut.http.uri.UriTemplate;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.authentication.Authentication;
import io.micronaut.security.rules.SecurityRule;
import io.micronaut.http.annotation.*;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.Flowable;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import javax.validation.constraints.NotNull;
import java.security.Security;
import java.util.List;


@Controller("/clientcontact")
//@Secured(SecurityRule.IS_ANONYMOUS)
public class ClientContactController {
    private static final Logger LOG = LoggerFactory.getLogger(ClientContactController.class);
    private IClientContactRepository clientContactRepository;
    private final RxHttpClient httpClient;

    public ClientContactController(IClientContactRepository clientContactRepository,
                                   @Client("http://appterauthsvc-env.g2yapp2kcp.us-east-1.elasticbeanstalk.com") RxHttpClient httpClient) {
        this.clientContactRepository = clientContactRepository;
        this.httpClient = httpClient;
    }

    @Get("/")
    public HttpStatus index() {
        return HttpStatus.OK;
    }

    @Post("/")
    @Secured(SecurityRule.IS_AUTHENTICATED)
    public ClientContact createClientContact(@Body ClientContact clientContact,
                                             Authentication authentication,
                                             @Header("Authorization") String authorization) {
        try {
            List<ClientContact> existingClientContacts = clientContactRepository.getClientContactByClientId(clientContact.getClientId());
            LOG.info("current contacts count for the client " + clientContact.getClientId() + " is " + existingClientContacts.size());
            if (existingClientContacts.isEmpty()) {

                User userObj = new User();
                Long clientId = new Long(clientContact.getClientId());
                userObj.setClientId(clientId);
                userObj.setFirstName(clientContact.getFirstName());
                userObj.setLastName(clientContact.getLastName());
                userObj.setEmailId(clientContact.getEmailAddress());
                userObj.setPhoneNo(clientContact.getContactNumber());
                userObj.setIsActive(true);

                LOG.info("User Email set is: "+userObj.getEmailId());
                LOG.info("authorization token is: "+authorization);
                HttpRequest<?> request = HttpRequest.POST("/user", userObj).bearerAuth(authorization);

                String response = httpClient.toBlocking().retrieve(request);
                LOG.info("Request Object: "+ request.toString());
                LOG.info("Response Object: "+ response.toString());
                LOG.info("User API executed.. ");
            }


            return clientContactRepository.createClientContact(clientContact);
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            return null;
        }
    }
}

提前致谢。

最佳答案

可能是因为@Header("Authorization") String authorization返回的是Bearer xyz...之类的东西,而bearerAuth方法正在向字符串添加Bearer,所以您要发送Bearer Bearer xyz...
所以.header(HttpHeaders.AUTHORIZATION, authorization)
另外请注意,您实际上不应该在此方法中阻止HTTP调用。这不是世界末日,因为在这种情况下,您正在阻塞IO线程,但是应避免这种类型的代码。

10-08 15:12