Micronaut令牌传播引发未经授权的错误 [英] Micronaut token propagation throwing Unauthrized error

查看:148
本文介绍了Micronaut令牌传播引发未经授权的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

父控制器-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

Thanks, Buddanna T

推荐答案

您可以使用:

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

这篇关于Micronaut令牌传播引发未经授权的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆