Micronaut token propagation throwing Unauthrized error

浪子不回头ぞ 提交于 2019-12-11 19:10:17

问题


As part of my project requirement, I am trying to call two server APIs from my parent service. All my endpoints are secured, so token has to be passed through every request. To pass token to endpoints calling from my parent controller, I have used token propagation concept of micronaut. But still I am seeing 'unauthrozed' error with child request. Note I am receiving token at parent controller method (logged and verified). Below is my code.

Parent Controller - 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"

Can anyone help me on this please?

Thanks, Buddanna T

来源:https://stackoverflow.com/questions/58166731/micronaut-token-propagation-throwing-unauthrized-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!