JSON Web Token (JWT)
#
SummaryJSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and independent method for securely transmitting information as JSON objects between parties. Since this information is digitally signed, it can be verified and trusted. The JWT can be signed using a secret (using the HMAC algorithm) or using a public/private key pair of RSA or ECDSA.
#
When should you use JSON Web Tokens?Authorization: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.
Information exchange: JSON Web Tokens are a good way of securely transmitting information between parties. Because JWTs can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.
#
Why should we use JSON Web Tokens?As JSON is less verbose than XML, when it is encoded its size is also smaller, making JWT more compact than SAML. This makes JWT a good choice to be passed in HTML and HTTP environments.
Security-wise, SWT can only be symmetrically signed by a shared secret using the HMAC algorithm. However, JWT and SAML tokens can use a public/private key pair in the form of a X.509 certificate for signing. Signing XML with XML Digital Signature without introducing obscure security holes is very difficult when compared to the simplicity of signing JSON.
JSON parsers are common in most programming languages because they map directly to objects. Conversely, XML doesn't have a natural document-to-object mapping. This makes it easier to work with JWT than SAML assertions.
Regarding usage, JWT is used at Internet scale. This highlights the ease of client-side processing of the JSON Web token on multiple platforms, especially mobile.
tip
All the above content quote from jwt.io
#
How to use jwt in go-zeroJwt authentication is generally used at the api layer. In this demonstration project, we generate jwt token when user api logs in, and verify the user jwt token when searching api for books.
#
user api generates jwt tokenFollowing the content of the Business Coding chapter, we perfect the getJwtToken
method left over from the previous section, that is, generate the jwt token logic
#
Add configuration definition and yaml configuration itemstip
$AccessSecret: The easiest way to generate the key of the jwt token is to use an uuid value.
$AccessExpire: Jwt token validity period, unit: second
For more configuration information, please refer to API Configuration
#
search.api uses jwt token authentication#
Write search.api filetip
jwt: Auth
: Enable jwt authentication
If the routing requires JWT authentication, you need to declare this syntax flag above the service, such as /search/do
above
Routes that do not require jwt authentication do not need to be declared, such as /search/ping
above
For more grammar, please read API IDL
#
Generate codeAs described above, there are three ways to generate code, so I won’t go into details here.
#
Add yaml configuration itemstip
$AccessSecret: This value must be consistent with the one declared in the user api.
$AccessExpire: Valid period
Modify the port here to avoid conflicts with user api port 8888
#
Verify jwt tokenStart user api service, and login
Start the search api service, call
/search/do
to verify whether the jwt authentication is passedLet’s not pass the jwt token and see the result:
Obviously, the jwt authentication failed, and the statusCode of 401 is returned. Next, let's take a jwt token (that is, the
accessToken
returned by the user login)tip
Service startup error, please check Error
At this point, the demonstration of jwt from generation to use is complete. The authentication of jwt token is already encapsulated in go-zero. You only need to simply declare it when defining the service in the api file.
#
Get the information carried in the jwt tokenAfter go-zero is parsed from the jwt token, the kv passed in when the user generates the token will be placed in the Context of http.Request intact, so we can get the value you want through the Context.
Add a log to output the userId parsed from jwt.
Output