<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Jimmy Song – OAuth 2.0</title><link>https://jimmysong.io/tags/oauth-2.0/</link><description>Recent content in OAuth 2.0 on Jimmy Song</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>Jimmy Song</managingEditor><webMaster>Jimmy Song</webMaster><follow_challenge><feedId>51621818828612637</feedId><userId>59800919738273792</userId></follow_challenge><lastBuildDate>Wed, 22 May 2024 05:54:49 +0800</lastBuildDate><atom:link href="https://jimmysong.io/tags/oauth-2.0/index.xml" rel="self" type="application/rss+xml"/><item><title>Detailed Explanation of Common Authentication Methods in Microservices</title><link>https://jimmysong.io/blog/microservice-auth-methods/</link><pubDate>Wed, 22 May 2024 05:54:49 +0000</pubDate><author>Jimmy Song</author><guid>https://jimmysong.io/blog/microservice-auth-methods/</guid><description>This article introduces several common authentication methods in microservices, helping you choose the appropriate authentication scheme when designing and implementing microservice systems.</description><content:encoded>
&lt;p&gt;In modern microservice architectures, security is a crucial aspect. As the number of microservices increases, ensuring secure communication between services becomes a challenge. This article will introduce several common authentication methods in microservices, helping you choose the appropriate authentication scheme when designing and implementing microservice systems.&lt;/p&gt;
&lt;h2 id="common-authentication-methods-in-microservices"&gt;Common Authentication Methods in Microservices&lt;/h2&gt;
&lt;p&gt;The table below lists several common authentication methods in microservices, comparing them from the perspectives of advantages, disadvantages, applicable scenarios, and real-world examples.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Authentication Method&lt;/th&gt;
&lt;th&gt;Advantages&lt;/th&gt;
&lt;th&gt;Disadvantages&lt;/th&gt;
&lt;th&gt;Deployment Location&lt;/th&gt;
&lt;th&gt;Applicable Scenarios&lt;/th&gt;
&lt;th&gt;Typical Uses&lt;/th&gt;
&lt;th&gt;Real-World Examples&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;JWT&lt;/td&gt;
&lt;td&gt;Self-contained token, reduces server load&lt;/td&gt;
&lt;td&gt;Larger token size, may increase bandwidth overhead&lt;/td&gt;
&lt;td&gt;API Gateway, Between Services&lt;/td&gt;
&lt;td&gt;Stateless communication between microservices&lt;/td&gt;
&lt;td&gt;User authentication and authorization&lt;/td&gt;
&lt;td&gt;User authentication in microservices (e.g., Auth0, Firebase)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OAuth 2.0&lt;/td&gt;
&lt;td&gt;Widely supported, highly flexible&lt;/td&gt;
&lt;td&gt;Complex implementation, requires additional interactions&lt;/td&gt;
&lt;td&gt;API Gateway&lt;/td&gt;
&lt;td&gt;Third-party application authorization&lt;/td&gt;
&lt;td&gt;Third-party application access to user data&lt;/td&gt;
&lt;td&gt;GitHub OAuth for third-party applications accessing GitHub data and APIs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;mTLS&lt;/td&gt;
&lt;td&gt;High security, prevents man-in-the-middle attacks&lt;/td&gt;
&lt;td&gt;Complex certificate management, significant performance overhead&lt;/td&gt;
&lt;td&gt;Between Services&lt;/td&gt;
&lt;td&gt;Communication requiring high security&lt;/td&gt;
&lt;td&gt;Secure communication between sensitive services&lt;/td&gt;
&lt;td&gt;Service communication in banking systems&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Basic Authentication&lt;/td&gt;
&lt;td&gt;Simple and easy to implement&lt;/td&gt;
&lt;td&gt;Insecure, easily intercepted&lt;/td&gt;
&lt;td&gt;API Gateway, Between Services&lt;/td&gt;
&lt;td&gt;Simple API protection&lt;/td&gt;
&lt;td&gt;Simple internal services&lt;/td&gt;
&lt;td&gt;Basic authentication of Kubernetes API Server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API Key Authentication&lt;/td&gt;
&lt;td&gt;Simple and easy to use&lt;/td&gt;
&lt;td&gt;Low security, easily abused&lt;/td&gt;
&lt;td&gt;API Gateway, Between Services&lt;/td&gt;
&lt;td&gt;Scenarios with low security requirements&lt;/td&gt;
&lt;td&gt;Simple service access control&lt;/td&gt;
&lt;td&gt;Various public APIs, such as OpenAI API&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Below we will introduce these common authentication methods in detail.&lt;/p&gt;
&lt;h2 id="jwt-authentication"&gt;JWT Authentication&lt;/h2&gt;
&lt;p&gt;JWT (JSON Web Token) was first proposed by the IETF JSON Web Token (JWT) Working Group and was officially released as the RFC 7519 standard in 2015. The design goal of JWT is to provide a compact and self-contained way to securely transmit information between parties. Due to its ease of use and stateless nature, JWT quickly gained widespread adoption and became one of the standards for identity verification and information exchange, especially in microservices and modern web applications.&lt;/p&gt;
&lt;p&gt;The following diagram shows the JWT authentication process.&lt;/p&gt;
&lt;figure class="mx-auto text-center"&gt;
&lt;img src="https://assets.jimmysong.io/images/blog/microservice-auth-methods/a990cb112a1ec1c4d6d7131c1e573e3f.svg" data-img="https://assets.jimmysong.io/images/blog/microservice-auth-methods/a990cb112a1ec1c4d6d7131c1e573e3f.svg" alt="Figure 11: JWT Authentication Process" data-caption="Figure 11: JWT Authentication Process"
width="1920"
height="964"
loading="lazy" decoding="async" class="image-loading"
onload="this.classList.remove('image-loading'); this.classList.add('image-loaded');"
onerror="handleImageError(this); this.classList.remove('image-loading');"&gt;
&lt;figcaption&gt;Figure 11: JWT Authentication Process&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;JWT Authentication Process Explanation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;User provides credentials&lt;/li&gt;
&lt;li&gt;Client requests access token&lt;/li&gt;
&lt;li&gt;Authentication server returns JWT token&lt;/li&gt;
&lt;li&gt;Client requests resource server with JWT token&lt;/li&gt;
&lt;li&gt;Resource server validates JWT&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="jwt-format-and-example"&gt;JWT Format and Example&lt;/h3&gt;
&lt;p&gt;A JWT (JSON Web Token) consists of three parts: Header, Payload, and Signature, each encoded with Base64 and concatenated with dots (.).&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Header&lt;/strong&gt;: The header includes the type of token and the signing algorithm.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;alg&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;HS256&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;typ&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;JWT&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Payload&lt;/strong&gt;: The payload contains claims, which are assertions about the user or other data.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;sub&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;1234567890&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;name&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;John Doe&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;iat&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1516239022&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Signature&lt;/strong&gt;: The signature is generated by encoding the header and payload, and signing them with a secret using the algorithm specified in the header.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;HMACSHA256(
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; base64UrlEncode(header) + &amp;#34;.&amp;#34; +
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; base64UrlEncode(payload),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; secret
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Below is an example of a JWT token:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id="oauth-20"&gt;OAuth 2.0&lt;/h2&gt;
&lt;p&gt;OAuth (Open Authorization) protocol was first proposed by Blaine Cook and Chris Messina in 2006. Initially, the goal was to provide an open authorization standard for Twitter. OAuth 1.0 was released in 2007, offering a standardized way for users to authorize third-party applications to access their resources. However, due to its complex signature mechanism and other security issues, it faced some limitations.&lt;/p&gt;
&lt;p&gt;To overcome these shortcomings, the IETF (Internet Engineering Task Force) established the OAuth Working Group to develop a simpler, more flexible authorization protocol. In 2012, OAuth 2.0 was officially released (RFC 6749 and RFC 6750). OAuth 2.0 simplifies the authorization process, adding multiple authorization modes, such as authorization code mode, implicit mode, resource owner password credentials mode, and client credentials mode.&lt;/p&gt;
&lt;p&gt;OAuth 2.0 quickly became an industry standard, widely used in various web services and applications such as Google, Facebook, GitHub, etc. Several extensions and complementary protocols (such as OpenID Connect) emerged based on it, further enhancing OAuth 2.0&amp;rsquo;s functionality and security.&lt;/p&gt;
&lt;p&gt;The development and expansion of OAuth 2.0 have made it a cornerstone of modern internet authentication and authorization, providing a flexible and secure solution to meet the evolving needs of web applications.&lt;/p&gt;
&lt;p&gt;The following diagram shows the OAuth 2.0 authentication process.&lt;/p&gt;
&lt;figure class="mx-auto text-center"&gt;
&lt;img src="https://assets.jimmysong.io/images/blog/microservice-auth-methods/411d77371072e128ec3bcce199df02a1.svg" data-img="https://assets.jimmysong.io/images/blog/microservice-auth-methods/411d77371072e128ec3bcce199df02a1.svg" alt="Figure 12: OAuth 2.0 Authentication Process" data-caption="Figure 12: OAuth 2.0 Authentication Process"
width="1920"
height="1005"
loading="lazy" decoding="async" class="image-loading"
onload="this.classList.remove('image-loading'); this.classList.add('image-loaded');"
onerror="handleImageError(this); this.classList.remove('image-loading');"&gt;
&lt;figcaption&gt;Figure 12: OAuth 2.0 Authentication Process&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;OAuth 2.0 Authentication Process Explanation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;User requests access to resource&lt;/li&gt;
&lt;li&gt;Client requests authentication&lt;/li&gt;
&lt;li&gt;User logs in and authorizes&lt;/li&gt;
&lt;li&gt;Authorization server returns authorization code&lt;/li&gt;
&lt;li&gt;Client exchanges authorization code for access token&lt;/li&gt;
&lt;li&gt;Client requests resource server with access token&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="oauth-20-authorization-code"&gt;OAuth 2.0 Authorization Code&lt;/h3&gt;
&lt;p&gt;In the OAuth 2.0 authorization code mode, the authorization code is a short-term credential obtained by the client from the authorization server after user authorization, used to exchange for an access token. The authorization code is a temporary string that can be passed between the authorization server and the client to obtain a more secure access token.&lt;/p&gt;
&lt;h3 id="oauth-20-extensions"&gt;OAuth 2.0 Extensions&lt;/h3&gt;
&lt;p&gt;OAuth 2.0 has developed many extensions to adapt to different scenarios. The table below lists some commonly used extensions, their main functions, and applicable scenarios.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Extension Name&lt;/th&gt;
&lt;th&gt;Main Function&lt;/th&gt;
&lt;th&gt;Applicable Scenarios&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Authorization Code PKCE Extension&lt;/td&gt;
&lt;td&gt;Enhances the security of the authorization code mode, prevents authorization code interception attacks&lt;/td&gt;
&lt;td&gt;Public clients (e.g., mobile apps, single-page apps)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dynamic Client Registration Protocol&lt;/td&gt;
&lt;td&gt;Allows clients to dynamically register and update client information&lt;/td&gt;
&lt;td&gt;Systems requiring high automation and flexibility&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Token Introspection&lt;/td&gt;
&lt;td&gt;Allows resource servers to verify and obtain detailed information about access tokens&lt;/td&gt;
&lt;td&gt;Scenarios requiring token validity verification and detailed information&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Token Revocation&lt;/td&gt;
&lt;td&gt;Provides a standard interface for token revocation&lt;/td&gt;
&lt;td&gt;Enhances system security and control&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Device Authorization Grant&lt;/td&gt;
&lt;td&gt;Allows devices with limited input capabilities to complete authentication via other devices&lt;/td&gt;
&lt;td&gt;Devices with limited input capabilities (e.g., smart TVs, game consoles)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mutual TLS Client Authentication&lt;/td&gt;
&lt;td&gt;Client authentication based on mutual TLS&lt;/td&gt;
&lt;td&gt;High-security application scenarios&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Resource Indicators&lt;/td&gt;
&lt;td&gt;Allows clients to specify the resource server to access in the authorization request&lt;/td&gt;
&lt;td&gt;Support for multiple resource servers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Step-up Authentication Challenge Protocol&lt;/td&gt;
&lt;td&gt;Allows resource servers to request stronger authentication (e.g., multi-factor authentication) as needed&lt;/td&gt;
&lt;td&gt;Advanced authentication for high-risk operations&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="oauth-20-authorization-process-using-github-as-an-example"&gt;OAuth 2.0 Authorization Process (Using GitHub as an Example)&lt;/h3&gt;
&lt;p&gt;GitHub uses OAuth 2.&lt;/p&gt;
&lt;p&gt;0 to authorize third-party applications to access users&amp;rsquo; GitHub data. OAuth 2.0 tokens on GitHub are called &amp;ldquo;access tokens&amp;rdquo; and are used for authentication and authorization to access GitHub APIs. It provides a secure, standardized way for third-party applications to access GitHub resources with user authorization. By using access tokens, applications can perform various operations on behalf of users, such as reading user information, accessing repositories, creating gists, etc. This process ensures user security and privacy while simplifying the authentication and authorization process for applications.&lt;/p&gt;
&lt;p&gt;The detailed process and examples of using GitHub OAuth 2.0 tokens are as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;User Authorization&lt;/strong&gt;: The user clicks the &amp;ldquo;Login with GitHub&amp;rdquo; button on the third-party application&amp;rsquo;s interface. The application redirects the user to GitHub&amp;rsquo;s authorization page.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Obtain Authorization Code&lt;/strong&gt;: The user logs in and agrees to authorize on the GitHub authorization page. GitHub redirects the user back to the application with an authorization code in the URL parameter.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;https://yourapp.com/callback?code=AUTHORIZATION_CODE&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Exchange Access Token&lt;/strong&gt;: The application server requests an access token from GitHub&amp;rsquo;s authorization server using the authorization code.&lt;/p&gt;
&lt;p&gt;Request Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;POST https://github.com/login/oauth/access_token
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Content-Type: application/json
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Accept: application/json
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;client_id&amp;#34;&lt;/span&gt;: &lt;span class="s2"&gt;&amp;#34;YOUR_CLIENT_ID&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;client_secret&amp;#34;&lt;/span&gt;: &lt;span class="s2"&gt;&amp;#34;YOUR_CLIENT_SECRET&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;code&amp;#34;&lt;/span&gt;: &lt;span class="s2"&gt;&amp;#34;AUTHORIZATION_CODE&amp;#34;&lt;/span&gt;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;redirect_uri&amp;#34;&lt;/span&gt;: &lt;span class="s2"&gt;&amp;#34;https://yourapp.com/callback&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;GitHub Returns Access Token&lt;/strong&gt;: GitHub validates the request and returns an access token.&lt;/p&gt;
&lt;p&gt;Response Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;access_token&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;YOUR_ACCESS_TOKEN&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;token_type&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;bearer&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;scope&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;repo,gist&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use Access Token to Access Resources&lt;/strong&gt;: The application uses the obtained access token to access the GitHub API.&lt;/p&gt;
&lt;p&gt;Request Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;curl -H &lt;span class="s2"&gt;&amp;#34;Authorization: token YOUR_ACCESS_TOKEN&amp;#34;&lt;/span&gt; https://api.github.com/user&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Response Example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;login&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;github-user&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;id&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;node_id&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;MDQ6VXNlcjE=&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;avatar_url&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;https://github.com/images/avatar.webp&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;name&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;Github User&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;company&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;GitHub&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;blog&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;https://example.com&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;location&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;Earth&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;email&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;github-user@example.com&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="characteristics-and-use-of-access-tokens"&gt;Characteristics and Use of Access Tokens&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Scopes&lt;/strong&gt;: The scope of an access token&amp;rsquo;s permissions is specified by the user during authorization and can include reading user profiles, accessing user repositories, managing gists, etc. For example, in the example above, the &lt;code&gt;scope&lt;/code&gt; includes &lt;code&gt;repo&lt;/code&gt; and &lt;code&gt;gist&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Validity and Refreshing&lt;/strong&gt;: Access tokens can be long-term or have a specified time range until the user actively revokes them.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Secure Transmission&lt;/strong&gt;: Access tokens should be transmitted via HTTPS to ensure they are not intercepted.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="mtls"&gt;mTLS&lt;/h2&gt;
&lt;p&gt;mTLS (Mutual TLS) is a technology that performs mutual authentication between the client and the server. It originated from the TLS (Transport Layer Security) protocol, the predecessor of which, SSL (Secure Sockets Layer), was developed by Netscape in 1995. With the increasing demand for internet security, TLS gradually evolved into a highly secure communication standard. mTLS further enhances security by requiring mutual authentication between both parties and is widely used in fields with high security requirements such as finance and healthcare.&lt;/p&gt;
&lt;p&gt;The following diagram shows the mTLS authentication process.&lt;/p&gt;
&lt;figure class="mx-auto text-center"&gt;
&lt;img src="https://assets.jimmysong.io/images/blog/microservice-auth-methods/d05161fb8841d5249d43ef2e697945ab.svg" data-img="https://assets.jimmysong.io/images/blog/microservice-auth-methods/d05161fb8841d5249d43ef2e697945ab.svg" alt="Figure 13: mTLS Authentication Process" data-caption="Figure 13: mTLS Authentication Process"
width="1920"
height="1472"
loading="lazy" decoding="async" class="image-loading"
onload="this.classList.remove('image-loading'); this.classList.add('image-loaded');"
onerror="handleImageError(this); this.classList.remove('image-loading');"&gt;
&lt;figcaption&gt;Figure 13: mTLS Authentication Process&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;mTLS Authentication Process Explanation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Client and server mutually send certificates&lt;/li&gt;
&lt;li&gt;Both parties validate each other&amp;rsquo;s certificates&lt;/li&gt;
&lt;li&gt;Establish secure connection&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For more information on TLS and mTLS, please refer to the blog post &lt;a href="https://jimmysong.io/blog/understanding-the-tls-encryption-in-istio/#what-is-tls-and-mtls"&gt;How Istio&amp;rsquo;s mTLS Traffic Encryption Works as Part of a Zero Trust Security Posture&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="basic-authentication"&gt;Basic Authentication&lt;/h2&gt;
&lt;p&gt;Basic Authentication is a simple authentication mechanism defined by the HTTP/1.0 specification (RFC 1945). It involves encoding the username and password with Base64 and appending them to the HTTP request header for authentication. Due to its simplicity and ease of use, Basic Authentication was widely adopted in early web applications. However, because of its inherent security issues (such as plain text transmission being easily intercepted), it is usually used with HTTPS or replaced by more secure authentication methods in modern applications.&lt;/p&gt;
&lt;p&gt;The following diagram shows the Basic Authentication process.&lt;/p&gt;
&lt;figure class="mx-auto text-center"&gt;
&lt;img src="https://assets.jimmysong.io/images/blog/microservice-auth-methods/cbaf0077dcd99163587797e51fe49518.svg" data-img="https://assets.jimmysong.io/images/blog/microservice-auth-methods/cbaf0077dcd99163587797e51fe49518.svg" alt="Figure 14: Basic Authentication Process" data-caption="Figure 14: Basic Authentication Process"
width="1920"
height="703"
loading="lazy" decoding="async" class="image-loading"
onload="this.classList.remove('image-loading'); this.classList.add('image-loaded');"
onerror="handleImageError(this); this.classList.remove('image-loading');"&gt;
&lt;figcaption&gt;Figure 14: Basic Authentication Process&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Basic Authentication Process Explanation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;User provides username and password&lt;/li&gt;
&lt;li&gt;Client requests resource server with username and password&lt;/li&gt;
&lt;li&gt;Resource server validates&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="basic-authentication-example"&gt;Basic Authentication Example&lt;/h3&gt;
&lt;p&gt;Basic Authentication uses a Base64-encoded username and password for authentication. Below is an example of a Basic Authentication request using the &lt;code&gt;curl&lt;/code&gt; command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;curl -u &amp;lt;username&amp;gt;:&amp;lt;password&amp;gt; https://api.example.com/data&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If the username is &lt;code&gt;admin&lt;/code&gt; and the password is &lt;code&gt;password123&lt;/code&gt;, the request example is as follows:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;curl -u admin:password123 https://api.example.com/data&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id="api-key-authentication"&gt;API Key Authentication&lt;/h2&gt;
&lt;p&gt;API Key Authentication is a method of authenticating by including a pre-assigned unique key in the request. It became popular in the early 2000s with the rise of web APIs. Due to its simplicity and ease of management, API Key Authentication is widely used in various public and private APIs. Although it has lower security and is easily abused, it is still an effective means of access control in many scenarios, especially for applications that do not require high security protection.&lt;/p&gt;
&lt;p&gt;The following diagram shows the API Key Authentication process.&lt;/p&gt;
&lt;figure class="mx-auto text-center"&gt;
&lt;img src="https://assets.jimmysong.io/images/blog/microservice-auth-methods/373df547422133ea5ba42e7321c3f5d4.svg" data-img="https://assets.jimmysong.io/images/blog/microservice-auth-methods/373df547422133ea5ba42e7321c3f5d4.svg" alt="Figure 15: API Key Authentication Process" data-caption="Figure 15: API Key Authentication Process"
width="1920"
height="995"
loading="lazy" decoding="async" class="image-loading"
onload="this.classList.remove('image-loading'); this.classList.add('image-loaded');"
onerror="handleImageError(this); this.classList.remove('image-loading');"&gt;
&lt;figcaption&gt;Figure 15: API Key Authentication Process&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;API Key Authentication Process Explanation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Client requests resource server with API Key&lt;/li&gt;
&lt;li&gt;Resource server validates API Key&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="api-key-example"&gt;API Key Example&lt;/h3&gt;
&lt;p&gt;An API Key is a unique identifier passed in the request for client authentication. API Keys are typically passed through HTTP request headers or URL parameters.&lt;/p&gt;
&lt;p&gt;Example of API Key in HTTP request header:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;curl -H &lt;span class="s2"&gt;&amp;#34;Authorization: ApiKey YOUR_API_KEY&amp;#34;&lt;/span&gt; https://api.example.com/data&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Example of API Key in URL parameter:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;curl https://api.example.com/data?api_key&lt;span class="o"&gt;=&lt;/span&gt;YOUR_API_KEY&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;An API Key is usually a string containing letters and numbers, for example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;1234567890abcdef1234567890abcdef&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2 id="summary"&gt;Summary&lt;/h2&gt;
&lt;p&gt;Choosing the right authentication method in a microservice architecture is crucial. Different authentication methods have their advantages and disadvantages in terms of security, complexity, and applicable scenarios. This article introduced five common authentication methods: JWT, OAuth 2.0, mTLS, Basic Authentication, and API Key Authentication, and provided their advantages, disadvantages, and applicable scenarios. Additionally, other common authentication methods such as SAML, LDAP, Kerberos, and OpenID Connect are also widely used in various internet application scenarios, especially in single sign-on and cross-domain authentication. I hope this information will help you choose the most suitable authentication scheme when designing and implementing microservice systems.&lt;/p&gt;</content:encoded></item></channel></rss>