如何生成JWT令牌以通过API度假访问微服务

发布于 2025-01-19 22:08:39 字数 363 浏览 2 评论 0原文

我正在尝试使用.NET Core中的微服务架构来构建项目。 假设我创建了以下服务

-Service 1

-Service 2,

然后我使用了API -GATEWAY并添加了Service1和Servic2的路线。最后,启用JWT授权层。

现在,我对如何基于用户名和密码为用户生成代币以及对用户的自定义索赔感到困惑,以便在API-Gateway验证该代币并相应地提供服务?

我应该创建另一个服务(例如IdentityService),用户可以根据用户名和密码生成令牌?

- 如果是的,则将该服务放置在API网关或API网关外部?

最好的方法是什么?

谢谢你, Saurabh Khandelwal。

I am trying to build Project with microservices architecture in .net core.
Say I have created the below services

-Service 1

-Service 2

Then I used the API-Gateway and added the routes for Service1 and Servic2. And at last, enable the JWT Authorization layer.

Now I'm confused about how to generate tokens along with the custom claims for the users based on username and password so that this token will be validated at API-Gateway and served services accordingly?

Should I create another Service (say IdentityService) where the user can generate the token based on username and password?

-If yes then, Where to place that service in the API Gateway or Outside the API Gateway?

What is the best way?

Thank you,
Saurabh Khandelwal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生戏 2025-01-26 22:08:40

嗯,有很多可用的方法,但是每种情况都有优点和缺点。

举个例子,我总是将 Abp Framework 的方法用于任何我认为将持续很长一段时间且未来会发生大量变化的项目。
通过使用这种方法,您可以将 IdentityServer 集中在域层内,如下所示:

Domain Layer

然后使用方法生成令牌的任何设置。例如:

    var client = await _clientRepository.FindByClientIdAsync(name);
    if (client == null)
    {
        client = await _clientRepository.InsertAsync(
            new Client(
                _guidGenerator.Create(),
                name
            )
            {
                ClientName = name,
                ProtocolType = "oidc",
                Description = name,
                AlwaysIncludeUserClaimsInIdToken = true,
                AllowOfflineAccess = true,
                AbsoluteRefreshTokenLifetime = 31536000, //365 days
                    AccessTokenLifetime = 31536000, //365 days
                    AuthorizationCodeLifetime = 300,
                IdentityTokenLifetime = 300,
                RequireConsent = false,
                FrontChannelLogoutUri = frontChannelLogoutUri,
                RequireClientSecret = requireClientSecret,
                RequirePkce = requirePkce
            },
            autoSave: true
        );
    }

我相信这种方法非常适合 DDD 模式。你可以在这里查看 使用 ABP 进行领域驱动设计

Well, there are so many approaches available, However, each scenario has pros and cons.

As an example, I always use Abp Framework's approach for any project that I believe is going to live for a long period of time with a lot of changes down the road.
By using this approach you can concentrate your IdentityServer inside the Domain layer as per below:

Domain Layer

Then use a method to generate any settings for tokens. For example:

    var client = await _clientRepository.FindByClientIdAsync(name);
    if (client == null)
    {
        client = await _clientRepository.InsertAsync(
            new Client(
                _guidGenerator.Create(),
                name
            )
            {
                ClientName = name,
                ProtocolType = "oidc",
                Description = name,
                AlwaysIncludeUserClaimsInIdToken = true,
                AllowOfflineAccess = true,
                AbsoluteRefreshTokenLifetime = 31536000, //365 days
                    AccessTokenLifetime = 31536000, //365 days
                    AuthorizationCodeLifetime = 300,
                IdentityTokenLifetime = 300,
                RequireConsent = false,
                FrontChannelLogoutUri = frontChannelLogoutUri,
                RequireClientSecret = requireClientSecret,
                RequirePkce = requirePkce
            },
            autoSave: true
        );
    }

This approach is super great for DDD patterns I believe. you can check it out here Domain Driven Design using ABP

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文