简体   繁体   English

与kong集成的微服务的用户注册+身份验证

[英]User registration + authentication for microservices integrated with kong

I am in the process of developing a node.js microservices-based application. 我正在开发一个基于node.js微服务的应用程序。 The individual microservices are developed including one that already handles the authentication, authorization and registration processes. 开发了各个微服务,包括已经处理过身份验证,授权和注册过程的微服务。 I am reviewing using kong as the api gateway but am looking for clarity regarding how I can handle authentication, etc. through kong but still use the user service I already created, which already has a db associated with it. 我正在审查使用kong作为api网关,但正在寻求有关如何通过kong处理身份验证等方面的明确性,但仍使用我已经创建的用户服务,该用户服务已经与它关联了一个数据库。

Ideally, I would prefer if I could have kong pass the authentication information to the user service to validate the user's login credentials (username and password) and have kong generate the jwt token provided the authentication was successful. 理想情况下,如果允许kong将身份验证信息传递给用户服务以验证用户的登录凭据(用户名和密码),并让kong生成jwt令牌(如果身份验证成功),我将更愿意。

Is this possible with kong or would I have to somehow move all of this functionality to kong, authentication, authorization and registration? kong是否可能实现此功能,或者我是否必须以某种方式将所有功能移至kong,身份验证,授权和注册?

Update 更新资料

I found the following article outlining an approach using a custom Authentication server, but this seems pretty involved: Custom Authentication Service in Kong API Gateway . 我发现以下文章概述了使用自定义身份验证服务器的方法,但这似乎很复杂: Kong API Gateway中的自定义身份验证服务

In a nutshell, I would like the user to pass a username/password combination to kong, which kong would somehow pass upstream to the endpoint of the user service I already have. 简而言之,我希望用户将用户名/密码组合传递给kong,该kong将以某种方式上游传递给我已经拥有的用户服务的端点。 This would then validate the user and confirm or deny user/pass is correct. 然后,这将验证用户并确认或拒绝用户/密码正确。 From this point, kong would generate the jwt and return to the user. 从这一点开始,kong将生成jwt并返回给用户。 If this were possible it would be great. 如果可能的话,那就太好了。 If I have a implement a custom authorization service that could work as well, but definitely not preferred.:-) 如果我有一个实现自定义授权服务的工具,它也可以正常工作,但绝对不是首选。:-)

So, what I would say from actively using Kong in a couple of places, I would suggest that you either use JWTs, and the JWT plugin with Kong, or use the OAuth2 plugin, but a combination of both is (as far as I know) actually not possible with Kong. 因此,从在几个地方积极使用Kong的观点来看,我建议您要么使用JWT,将JWT插件与Kong一起使用, 要么使用OAuth2插件,但两者的结合是(据我所知),实际上对于Kong是不可能的。 But, bear with me for a little while. 但是,请耐心一点。

Using JWTs 使用JWT

For your situation, using a JWT might be a fairly simple solution. 对于您的情况,使用JWT可能是一个非常简单的解决方案。 The only misconception I see right now in your assumptions is that Kong actually helps you in crafting JWT tokens, which is not the case . 我现在在您的假设中看到的唯一误解是Kong实际上可以帮助您制作JWT令牌, 事实并非如此 Kong can only validate JWT, but not craft them; Kong只能验证JWT,而不能制作它们。 this has to be done by your own Authorization Service. 这必须由您自己的授权服务来完成。 Then again, how you want to pass the token to the consuming service is again something which depends on the type of the service. 再说一次,如何将令牌传递给使用服务又取决于服务的类型。

Here, you might want to use an OAuth2 flow, eg the Implicit Grant if your client is an SPA, or some other grant (such as the Resource Owner Password Grant), but in both cases, Kong will not help you in implementing it. 在这里,您可能要使用OAuth2流,例如,如果您的客户端是SPA,则是“隐式授予”,或者是其他一些授予(例如“资源所有者密码授予”),但是在两种情况下,Kong 都无法帮助您实现它。 You will need to implement the /authorize (for the implicit grant) or the /token endpoints (for the Resource Owner Password Grant) by yourself. 您将需要自己实现/authorize (用于隐式授予)或/token端点(用于资源所有者密码授予)。 Once more: Kong will only validate the JWT which you craft, and not craft the token. 再次:Kong将仅验证制作的JWT,而不制作令牌。 Implementing those end points is not really difficult, but requires reading the RFC 6749 (which is worth the effort). 实现这些端点并不是很困难,但是需要阅读RFC 6749 (值得付出努力)。

When crafting your tokens, check the documentation of Kong on what Kong expects from the JWT, especially regarding the iss claim, which has to match a certain property inside your API definition. 在制作令牌时,请查看Kong的文档,了解Kong对JWT的期望,特别是关于iss索赔,该索赔必须与您的API定义中的某个属性匹配。 You can either retrieve that manually for your API, or you can use the Kong Admin API to do that (port 8001 ). 您可以为您的API手动检索该值,也可以使用Kong Admin API进行此操作(端口8001 )。 The former is easier, the latter requires some coding but is more reliable and flexible. 前者更容易,后者需要一些编码,但更可靠,更灵活。

Using the OAuth2 Plugin 使用OAuth2插件

The solution outline you found in that other question suggests a different solution, where you actually implement a real OAuth2 Authorization Server, and make use of the Kong OAuth2 plugin. 您在另一个问题中发现的解决方案概述提出了一个不同的解决方案,您在其中实际实现了真正的OAuth2授权服务器,并使用了Kong OAuth2插件。

This really requires digging into the OAuth2 spec, and also understanding quite well how Kong does this (which is a little special). 这确实需要深入研究OAuth2规范,并且还非常了解Kong如何做到这一点(这有点特殊)。 The answer I provided in that question outlines the necessary steps. 我在该问题中提供的答案概述了必要的步骤。

Common properties of the solutions 解决方案的共同属性

With both solutions you get the benefit of Kong validating your calls to your API, and only letting calls with a valid token pass. 使用这两种解决方案,您都可以从Kong验证您对API的调用中获益,并且仅让具有有效令牌的调用通过。 The JWT option leaves you with crafting your token, and will also require your API backend implementation to inspect the token and take out which claims it needs, such as something like a "User ID" or "Scopes" from the JWT token. JWT选项使您可以精心设计令牌,并且还需要API后端实现检查令牌并取出所需的声明,例如JWT令牌中的“用户ID”或“范围”之类的东西。 Kong will not do that work for you, it will just validate that the JWT token is correctly signed. Kong不会为您完成这项工作,只会验证JWT令牌已正确签名。

The second option offloads that more to Kong, where you, when authorizing the user, add an X-Authenticated-UserId and (optionally) an X-Authenticated-Scope to the opaque token, which in turn is enriched to the call to your backend API via headers. 第二个选项将更多的负载分流到Kong,在授权用户时,您会在不透明令牌中添加X-Authenticated-UserId和(可选) X-Authenticated-Scope ,这反过来又充实了您对后端的调用通过标头的API。 Which means that your API does not have to do any extra work in taking the token apart - the information is readily available via Kong-injected extra headers. 这意味着您的API无需做任何额外的工作即可将令牌拆开-可以通过Kong注入的额外标头轻松获得信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM