简体   繁体   English

使用 Spring Security 保护 REST 微服务

[英]Securing REST microservices with Spring Security

I'm looking for a best-practice and efficient solution to secure multiple microservices communicating via REST to a Web Client application.我正在寻找一种最佳实践和高效的解决方案来保护通过 REST 与 Web 客户端应用程序通信的多个微服务。

Current setup :当前设置

These microservices are made in Java, with Spring Framework and run into Docker containers.这些微服务是用 Java 制作的,使用 Spring 框架并运行到 Docker 容器中。

The client is an Angular 2 application.客户端是一个 Angular 2 应用程序。

I made a new µService that will act as a " gateway " and be the only communication point between my web client and my other services.我创建了一个新的 µService,它将充当“网关”,并且是我的 Web 客户端和我的其他服务之间的唯一通信点。

I retrieve a JWT encrypted token from a remote authentication API (let's call it LOCK)我从远程身份验证 API 检索 JWT 加密令牌(我们称之为 LOCK)

Solution I was thinking about :我正在考虑的解决方案

在此处输入图片说明

I could store the login JWT into a cookie, and send it to the gateway.我可以将登录 JWT 存储到 cookie 中,然后将其发送到网关。

The gateway embed in the final payload sent to the concerned µService the token and store the user if it's new into a database.网关将令牌嵌入到发送给相关 µService 的最终有效载荷中,并将用户(如果用户是新用户)存储到数据库中。

The microservice then get the query, checks in the remote authentication service the user role, and if it's sufficient, it returns a 200 status with result.然后微服务获取查询,在远程身份验证服务中检查用户角色,如果足够,则返回 200 状态和结果。

Edit编辑

We will need to have a RabbitMQ Broker into our µServices hive, and thus, to use the WebSockets.我们需要在 µServices hive 中安装一个 RabbitMQ Broker,从而使用 WebSockets。 In order to secure WebSockets in the same way as securing REST APIs, I'm not sure if we still should manage security in a gateway, and maybe manage it at the microservice level by itself.为了以与保护 REST API 相同的方式保护 WebSockets,我不确定我们是否仍然应该在网关中管理安全性,或者在微服务级别单独管理它。 Because lots of messages will transit, and we should maybe get rid a middleware that will slow down the thing.因为会传输大量消息,我们也许应该摆脱会减慢传输速度的中间件。

Questions :问题

Is it a good practice ?这是一个好习惯吗? What could possibly be done better ?什么可以做得更好? Do you have any example of things done that fills the same needs ?你有什么例子可以满足相同的需求吗? Thanks a lot for your shares & thoughts.非常感谢您的分享和想法。

I suggest you have a look on Dave Sayer's great webinar on this topic. 我建议你看看Dave Sayer关于这个主题的精彩网络研讨会。 He suggest several possible options varying from Spring session to OAuth2. 他建议了几种可能的选择,从Spring会话到OAuth2。

You can find the webinar here: https://spring.io/blog/2014/11/07/webinar-replay-security-for-microservices-with-spring-and-oauth2 您可以在此处找到网络研讨会: https//spring.io/blog/2014/11/07/webinar-replay-security-for-microservices-with-spring-and-oauth2

There was also a tutorial with code samples but I can't find it right now. 还有一个代码示例的教程,但我现在找不到它。

I would do this in the following way:我会通过以下方式做到这一点:

Consider that every microservice is behind the apigateway, even the authentication/authorisation microservice -> let's call it simply auth .考虑到每个微服务都在 apigateway 后面,即使是身份验证/授权微服务 -> 让我们简单地称之为auth

Once the request comes, the apigateway checks with auth service, auth service does what it needs in order to verify that the token and the requester are legit, and responds back to apigateway with a 200/401(/403).一旦请求到来,apigateway 就会检查 auth 服务,auth 服务会做它需要的事情来验证令牌和请求者是否合法,并用 200/401(/403) 响应 apigateway。

If apigw got a 200, it forwards the request to whatever microservice was at that endpoint, otherwise returns the other statuses.如果 apigw 得到 200,它会将请求转发到该端点上的任何微服务,否则返回其他状态。

If we consider this scenario and that the only entry point into your environment it through apigateway, I don't see any reason for you to secure the communication between other services.如果我们考虑这种情况,并且它是通过 apigateway 进入您环境的唯一入口点,我认为您没有任何理由保护其他服务之间的通信。

Let's consider this scenario:让我们考虑这个场景:

client -> calls POST /api/v1/cars

  1. Between client and apigw we have a secure call because obviously we're gonna be using a certificate... therefore the client will call https://..../api/v1/cars在客户端和 apigw 之间我们有一个安全调用,因为显然我们将使用证书......因此客户端将调用https://..../api/v1/cars
  2. request reaches apigw, it decrypts the request请求到达 apigw,它解密请求
  3. gets the jtw得到 jtw
  4. asks auth -> this one return 200询问 auth -> 这个返回 200
  5. forwards the request to CarsMicroservice将请求转发给CarsMicroservice
  6. Cars microservice trusts the request because it knows that someone else already made the JTW signature decryption so it's safe to asume that any information in it is legit Cars 微服务信任该请求,因为它知道其他人已经进行了 JTW 签名解密,因此可以安全地假设其中的任何信息都是合法的
  7. If carsMicroservice needs some user specific information, for example this being a post for creating a new car, for some reason instead of using the subject field from the JWT token that's the uuid of the user, we want a user name -> car association so when the request reaches carsMicroservice, it will DECODE the payload, get the user uid, asks userMicroservice to return all the information about the user with that uid.如果carsMicroservice需要一些用户特定的信息,例如这是一个创建新车的帖子,出于某种原因而不是使用JWT令牌中的subject字段,即用户的uuid,我们需要一个用户名->汽车关联当请求到达carsMicroservice时,它会对payload进行DECODE ,获取用户uid,要求userMicroservice返回该用户的所有信息。 This communication doesn't need to be secure because it's done internally after all the necessary security checks.这种通信不需要是安全的,因为它是在所有必要的安全检查之后在内部完成的。

Of course you can enforce this that the only user information a user can access it's his own etc, but these are custom heuristic.当然,您可以强制执行此操作,即用户可以访问的唯一用户信息是他自己的等,但这些是自定义启发式的。

In addition, you may have to establish a trust between the microservices.此外,您可能必须在微服务之间建立信任。 You may use combinations of:您可以使用以下组合:

  • Using LDAP/OAuth2 tokens使用 LDAP/OAuth2 令牌
  • Using IP whitelisting使用 IP 白名单
  • CORS rules CORS 规则
  • Https HTTPS

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

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