SpringCloud Alibaba Microservices Practice: Gateway Authorization VS Microservices Authorization

SpringCloud Alibaba Microservices Practice: Gateway Authorization VS Microservices Authorization

[[386274]]

This article is reprinted from the WeChat public account "JAVA Daily Record", the author is single tone. Please contact the JAVA Daily Record public account for reprinting this article.

In the SpringCloud architecture, there are two ways to implement authorization:

  • Authorization at the gateway level
  • Authorization by the backend microservice itself

Both methods have implementation solutions in this series of articles, so the question is: which one is the best solution and which one is more reasonable?

I'm sorry, but you may not get the answer you want after reading this article, because the conclusion is that there is no optimal solution. Both solutions have their own advantages and disadvantages, and you can only choose the corresponding solution based on your own business. In this article, we will make a simple comparison between the two solutions so that everyone can have a reference for making a decision.

Solution Comparison

First, let's look at the principles of the two solutions: If you have any questions about the specific implementation methods, you can refer to this article:

SpringCloud Alibaba Microservices Practice 19 - Integrated RBAC Authorization

Gateway Authorization

Gateway-based authorization is also called path matcher-based authorization. When a request passes through the gateway, it verifies whether the path of the current request is in the resource path owned by the user.

When authorization is based on a path matcher, it is necessary to consider the RESTful-style access path, such as /account-service/blog/user/{id} or /account-service/blog/**, etc. Therefore, authorization at the gateway is mainly based on wildcard matching.

Microservice authorization

Microservice authorization is also called method-based interception. The corresponding method identifier is marked on the resource and then assigned to the user. The corresponding annotation is used to determine whether the current user has permission to access this method on the request method. For example, the @PreAuthorize("hasAuthority('')") annotation in SpringSecurity and the @RequiresPermissions('') annotation in Shiro. Whether it is SpringSecurity or Shiro, their implementation principle is based on keyword exact matching.

Advantages and disadvantages comparison

Gateway Authorization

advantage

The advantage of using gateway authorization is obvious. All backend microservices only need to be ordinary services and no longer need to rely on permissions.

shortcoming

The performance of wildcard matching in the gateway is relatively poor. The wildcard needs to be split, matching the prefix first, and then matching the wildcard after the prefix matches. Here you can see the implementation logic of org.springframework.util.AntPathMatcher#doMatch().

For RESTful-style URL paths, permissions cannot be finely controlled

For example, a microservice has the following API

  1. GET /v1/pb/ user  
  2.  
  3. POST /v1/pb/ user  
  4.  
  5. PUT /v1/pb/ user  

In this way, when the gateway obtains the user request path through the request.getURI().getPath() method, it is the same address. After granting a user the /v1/pb/user permission, he has three different permissions: GET, PUT, and POST. Obviously, this cannot meet the requirements of fine-grained permission control.

As for how to solve this problem, I wrote an article to discuss it. Students who are interested can take a look: SpringCloud Alibaba Microservices Practice 25 - Restful Interface Interception

Microservice authorization

advantage:

The disadvantages of gateway authorization mentioned above are actually the advantages of microservice authorization. It is completely matched based on method interception, consumes very little CPU, and there is no RestFul problem.

shortcoming:

The implementation is relatively complicated. All resource server related configurations need to be introduced in the SpringSecurity Oauth2 system, so a separate resource server module is generally established. This is also a problem that needs to be solved in the next article of the series.

in conclusion

Here we try to summarize the two implementation solutions. If the system functions and business modules are not many, the gateway authorization mode can be used. This is the simplest and most convenient implementation. Although the Restful style cannot fine-tune the permission control problem, we can solve it by adding a Method field.

If your system is large and there are many resources that need to be authorized, it is recommended to adopt the microservice authorization model. In order to avoid the need for each microservice to handle the logic of permission verification, we also need to extract a common permission authentication module for the backend service to reference.

<<:  People's Daily Overseas Edition: "China's Internet Speed" Empowers Thousands of Industries

>>:  BICS: 5G device connectivity unlocks new IoT use cases

Recommend

Is SDN the next stop for network administrators? Why is it important?

SDN (Software Defined Networking) has become one ...

If you think 5G will solve all your IoT challenges, think again

5G stands for the next generation of mobile commu...

Looking ahead to network technology trends in 2018

If 2017 was considered the year of major disrupti...

Once together, now separated, 5G baseband will connect everything

2019 is the first year of 5G, but SoC and 5G base...

WiFi 6 Blowout Begins

Despite the current epidemic, Xiaomi released a W...

How to restore blood flow to your brain after a long holiday?

[Original article from 51CTO.com] Hello, my frien...

How do these countries plan their 5G breakthrough amid the COVID-19 crisis?

5G is a new technology field that all countries a...

Kuroit: £2/month KVM-2GB/15GB NVMe/1TB/Los Angeles Data Center

Kuroit is a foreign hosting company founded in 20...

5G service routines for users have caused consumers to have more doubts about 5G

While the industry has painted many bright prospe...