Micronaut令牌传播引发未经授权的错误

时间:2019-09-30 11:11:29

标签: bearer-token micronaut propagation

作为我的项目要求的一部分,我试图从父服务中调用两个服务器API。我所有的端点都是安全的,因此令牌必须通过每个请求传递。为了将令牌传递给从我的父控制器调用的端点,我使用了micronaut的令牌传播概念。但是我仍然看到子请求出现“未经授权”错误。注意我在父控制器方法(记录并验证)上收到令牌。下面是我的代码。

父控制器-ClientUserController

    package io.appter.portal.controllers;

    import io.appter.portal.models.ClientContact;
    import io.appter.portal.models.User;
    import io.appter.portal.repositories.IClientManagementClient;
    import io.appter.portal.repositories.IUserManagementClient;
    import io.micronaut.http.annotation.Controller;
    import io.micronaut.http.annotation.Header;
    import io.micronaut.http.annotation.Post;

    import io.micronaut.security.annotation.Secured;
    import io.micronaut.security.rules.SecurityRule;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import java.util.List;

    @Controller("clientuser")
    public class ClientUserController {
        private static final Logger LOG = LoggerFactory.getLogger(ClientUserController.class);
        private IClientManagementClient clientManagementClient;
        private IUserManagementClient userManagementClient;

        public ClientUserController(IClientManagementClient clientManagementClient, IUserManagementClient userManagementClient) {
            this.clientManagementClient = clientManagementClient;
            this.userManagementClient = userManagementClient;
        }

        @Post("/")
        @Secured(SecurityRule.IS_AUTHENTICATED)
        public ClientContact createClientContactUser(ClientContact clientContact,
                                                     @Header("Authorization") String authorization
        ) {
            LOG.info("Authorization token received is: " + authorization);
            List<ClientContact> clientContacts = clientManagementClient.getClientContactByClientId(133);
            LOG.info("client contact 0 - Email Address received from API is: " + clientContacts.get(0).getEmailAddress());

            String testInfo = userManagementClient.test();
            LOG.info("Test Info received from API is: " + testInfo);

            return clientContact;
        }
    }

IUserManagementClient.java

    package io.appter.portal.repositories;

    import io.appter.portal.models.User;
    import io.micronaut.http.annotation.Get;
    import io.micronaut.http.annotation.Post;
    import io.micronaut.http.client.annotation.Client;

    @Client(id = "usermanagement")
    public interface IUserManagementClient {
        @Post("/user")
        public User createUser(User user);

        @Get("/user/test")
        public String test();
    }

IClientManagementClient.java

    package io.appter.portal.repositories;

    import io.appter.portal.models.ClientContact;
    import io.micronaut.http.annotation.Get;
    import io.micronaut.http.annotation.Post;
    import io.micronaut.http.client.annotation.Client;

    import java.util.List;

    @Client(id = "clientmanagement")
    public interface IClientManagementClient {
        @Post("/clientcontact")
        public ClientContact createClientContact(ClientContact clientContact);

        @Get("/getClientContactByClientId/{id}")
        public List<ClientContact> getClientContactByClientId(Integer id);
    }

application.yml

    micronaut:
      application:
        name: appter-portal-api
      server:
        port: 8080
        cors:
          enabled: true
      security:
        enabled: true
        token:
          jwt:
            enabled: true
            signatures:
              secret:
                generator:
                  secret: xxxxxxxxxx
          writer:
            header:
              enabled: true
            propagation:
              enabled: true
              service-id-regex: "clientmanagement"
      http:
        services:
          usermanagement:
            urls:
              - "http://appterusersvc.com"
          clientmanagement:
            urls:
              - "http://apptorclientmgmtsvc.com"

有人可以帮我吗?

谢谢, Buddanna T

1 个答案:

答案 0 :(得分:0)

您可以使用:

public String test(@Header("Authorization") String authorization);