Chapter 7. Securing your microservices
The chapter explains that security in a microservice system is not optional, even though it can be difficult to understand and debug. It focuses on the core problem of making sure a caller is both authenticated and authorized, while noting that infrastructure hardening and network controls are also important parts of a broader security strategy. To solve the authentication and authorization problem, the chapter uses Spring Cloud Security together with OAuth2, which lets a user log in once, receive a token, and then present that token to multiple services without repeatedly sending credentials.
The chapter then walks through building an OAuth2-based setup for a Spring application. It shows how to create an authentication service, register client applications, define users and roles, and issue tokens using the password grant flow. It also demonstrates how a protected resource checks tokens and permissions, first by allowing any authenticated user to access a service and then by restricting certain operations, such as DELETE requests, to users with an ADMIN role. Because microservices often call each other, the chapter also covers token propagation so that downstream services can continue to validate the same identity across a chain of calls, including through a Zuul gateway.
Next, the chapter introduces JWT as a more structured way to carry token information. JWTs are signed, compact, self-contained, and extensible, which makes them useful for microservices, though the chapter emphasizes that they are not encrypted and should not contain sensitive personal data. It shows how to configure Spring services to issue and consume JWTs, how to pass the token in an Authorization header, and how to extend the token with custom claims such as an organization ID. The chapter closes by stepping back from OAuth2 to broader operational guidance: use HTTPS for all service traffic, route requests through an API gateway, separate public and private service zones, and minimize exposed network ports to reduce the attack surface.
Figure 7.3. When requesting a OAuth2 token, the user’s credentials are passed in as HTTP form Parameters to the /auth/oauth/token endpoint.
Figure 7.10. Using http://jswebtoken.io allows you to decode the contents.
Summary
- OAuth2 is a token-based authentication framework to authenticate users.
- OAuth2 ensures that each microservice carrying out a user request doesn’t need to be presented with user credentials with every call.
- OAuth2 offers different mechanisms for protecting web services calls. These mechanisms are called grants.
- To use OAuth2 in Spring, you need to set up an OAuth2-based authentication service.
- Each application that wants to call your services needs to be registered with your OAuth2 authentication service.
- Each application will have its own application name and secret key.
- User credentials and roles are in memory or a data store and accessed via Spring security.
- Each service must define what actions a role can take.
- Spring Cloud Security supports the JavaScript Web Token (JWT) specification.
- JWT defines a signed, JavaScript standard for generating OAuth2 tokens.
- With JWT, you can inject custom fields into the specification.
- Securing your microservices involves more than just using OAuth2. You should
- Use HTTPS to encrypt all calls between services.
- Use a services gateway to narrow the number of access points a service can be reached through.
- Limit the attack surface for a service by limiting the number of inbound and outbound ports on the operating system that the service is running on.