Quickly complete the third-party authorized login tool library JustAuth

Quickly complete the third-party authorized login tool library JustAuth

2022-09-02 0 843
Resource Number 38013 Last Updated 2025-02-24
¥ 0HKD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

This issue recommends an open source third party authorized login tool class library——JustAuth。

Quickly complete the third-party authorized login tool library JustAuth插图

JustAuth, as you can see, is just a third party authorized login tool class library, it can let us out of the cumbersome third party login SDK, So easy to log in!

JustAuth integrates with dozens of third-party platforms at home and abroad such as Github, Gitee, Alipay, Sina Weibo, wechat, Google, Facebook, Twitter, StackOverflow, etc.

Quickly complete the third-party authorized login tool library JustAuth插图1

JustAuth features:

  • Rich OAuth platform, supporting dozens of well-known third-party platforms at home and abroad OAuth login.
  • Custom state, support custom State and cache mode, developers can choose any cache plug-in according to the actual situation.
  • Customize OAuth, provide a unified interface, support access to any OAuth website, quickly achieve OAuth login function.
  • Custom Http, interface HTTP tools, developers can choose the corresponding HTTP tools according to the actual situation of their own project.
  • Custom Scope Supports custom scope to suit more business scenarios, not just for login.
  • The code specification is simple, the code strictly abides by Alibaba’s code specification, the structure is clear and the logic is simple.

how to use

  • Procedure of use

There are three steps to using JustAuth (these steps are also appropriate for any platform JustAuth supports) :

1. Apply to register a developer account for a third-party platform

2. Create applications for third-party platforms and obtain configuration information (accessKey, secretKey, redirectUri)

3. Use this tool to implement authorized login

  • usage mode

Introduce dependency

<dependency>
    <groupId>me.zhyd.oauth</groupId>
    <artifactId>JustAuth</artifactId>
    <version>${latest.version}</version>
</dependency>

latest version:
https://search.maven.org/search?q=g:me.zhyd.oauth%20AND%20a:JustAuth

call api

// Create authorization request
AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
        .clientId("clientId")
        .clientSecret("clientSecret")
        .redirectUri("redirectUri")
        .build());
// Generate authorization page
authRequest.authorize("state");
// After authorized login, code (auth_code (Alipay only)) and state will be returned. After 1.8.0, the AuthCallback class can be used as the parameter of the callback interface
// Note: The default saving time of JustAuth state is 3 minutes. If the state is not used within 3 minutes, the expired state will be automatically cleared
authRequest.login(callback);

Attention: JustAuth has integrated simple-http as the HTTP universal interface by default since v1.14.0. Since HTTP tools have been integrated in common projects, such as OkHttp3, apache HttpClient, hutool-http, Therefore, in order to reduce unnecessary dependencies, JustAuth will not integrate hutool-http by default starting from v1.14.0. If the developer’s project is new or does not integrate HTTP implementation tools in the project, please add corresponding HTTP implementation classes by yourself.

  • API decomposition

The core of JustAuth is a request, each platform corresponds to a specific request class, so before use, it is necessary to create a response request for a specific authorization platform

// Create authorization request
AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
        .clientId("clientId")
        .clientSecret("clientSecret")
        .redirectUri("redirectUri")
        .build());

Get authorization link

String authorizeUrl = authRequest.authorize("state");

After obtaining authorizeUrl, you can manually redirect it to authorizeUrl

pseudo-code

/**
 * @param source Third-party authorization platforms, using this example as a reference, the value is gitee (because AuthGiteeRequest is declared above)
 */
@RequestMapping("/render/{source}")
public void renderAuth(@PathVariable("source") String source, HttpServletResponse response) throws IOException {
    AuthRequest authRequest = getAuthRequest(source);
    String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
    response.sendRedirect(authorizeUrl);
}

Note: state advice must pass! The primary role of state in OAuth’s flow is to ensure request integrity and prevent CSRF risks, and the state passed here will be returned on callback

Login (Get user information)

AuthResponse response = authRequest.login(callback);

After authorized login, code (auth_code (for Alipay only), authorization_code (for Huawei only)), and state are returned. After 1.8.0, the AuthCallback class is used as the input parameter of the callback interface

pseudocode

/**
 * @param source Third-party authorization platforms, using this example as a reference, the value is gitee (because AuthGiteeRequest is declared above)
 */
@RequestMapping("/callback/{source}")
public Object login(@PathVariable("source") String source, AuthCallback callback) {
    AuthRequest authRequest = getAuthRequest(source);
    AuthResponse response = authRequest.login(callback);
    return response;
}

Note: For the authorization callback address configured on the third-party platform, the callback address when creating an authorization application is set to:[host]/callback/gitee

Refresh token

Note: refresh is not supported on every platform

AuthResponse response = authRequest.refresh(AuthToken.builder().refreshToken(token).build());

pseudocode

/**
 * @param source Third-party authorization platforms, using this example as a reference, the value is gitee (because AuthGiteeRequest is declared above)
 * @param token  login Returned after success refreshToken
 */
@RequestMapping("/refresh/{source}")
public Object refreshAuth(@PathVariable("source") String source, String token){
    AuthRequest authRequest = getAuthRequest(source);
    return authRequest.refresh(AuthToken.builder().refreshToken(token).build());
}

cancel authorization

Note: revoke is not supported on every platform

AuthResponse response = authRequest.revoke(AuthToken.builder().accessToken(token).build());

pseudocode

/**
 * @param source Third-party authorization platforms, using this example as a reference, the value is gitee (because AuthGiteeRequest is declared above)
 * @param token  login Returned after success accessToken
 */
@RequestMapping("/revoke/{source}/{token}")
public Object revokeAuth(@PathVariable("source") String source, @PathVariable("token") String token) throws IOException {
    AuthRequest authRequest = getAuthRequest(source);
    return authRequest.revoke(AuthToken.builder().accessToken(token).build());
}

Integrated Tiktok login example

1.Apply for application

Visit the Tiktok Open Platform and log in, if you do not have an account, please register yourself.

After the login is complete, you need to perform enterprise authentication (only enterprise authentication).

After the authentication is successful, the following information is displayed:

Quickly complete the third-party authorized login tool library JustAuth插图2

On the Management Center – Application Management page, choose Create Application, create Application page, and select Website Application.

Quickly complete the third-party authorized login tool library JustAuth插图3

Note that the local host cannot be used. You only need to configure the root domain, for example, justauth.cn

The following is an example:

Quickly complete the third-party authorized login tool library JustAuth插图4

After completing the filling, submit it for review, wait for the completion of the platform review, and then proceed to the next step.

copy The following information: App ID, App Key, and website callback domain

2. Integrate JustAuth

The integration section refers to the previous chapter to introduce dependencies, create requests, and generate authorization addresses.

We can generate authorized links to third party platforms directly by:

String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());

We can redirect this link directly in the background to jump, or we can return to the front end after the front end controls the jump. The benefit of front-end control is that third-party authorization pages can be embedded in the iframe to fit the website design.

Quickly complete the third-party authorized login tool library JustAuth插图5

You can read more on your own.

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free Source Code Quickly complete the third-party authorized login tool library JustAuth https://ictcoder.com/quickly-complete-the-third-party-authorized-login-tool-library-justauth/

Share free open-source source code

Q&A
  • 1. Automatic: After making an online payment, click the (Download) link to download the source code; 2. Manual: Contact the seller or the official to check if the template is consistent. Then, place an order and make payment online. The seller ships the goods, and both parties inspect and confirm that there are no issues. ICTcoder will then settle the payment for the seller. Note: Please ensure to place your order and make payment through ICTcoder. If you do not place your order and make payment through ICTcoder, and the seller sends fake source code or encounters any issues, ICTcoder will not assist in resolving them, nor can we guarantee your funds!
View details
  • 1. Default transaction cycle for source code: The seller manually ships the goods within 1-3 days. The amount paid by the user will be held in escrow by ICTcoder until 7 days after the transaction is completed and both parties confirm that there are no issues. ICTcoder will then settle with the seller. In case of any disputes, ICTcoder will have staff to assist in handling until the dispute is resolved or a refund is made! If the buyer places an order and makes payment not through ICTcoder, any issues and disputes have nothing to do with ICTcoder, and ICTcoder will not be responsible for any liabilities!
View details
  • 1. ICTcoder will permanently archive the transaction process between both parties and snapshots of the traded goods to ensure the authenticity, validity, and security of the transaction! 2. ICTcoder cannot guarantee services such as "permanent package updates" and "permanent technical support" after the merchant's commitment. Buyers are advised to identify these services on their own. If necessary, they can contact ICTcoder for assistance; 3. When both website demonstration and image demonstration exist in the source code, and the text descriptions of the website and images are inconsistent, the text description of the image shall prevail as the basis for dispute resolution (excluding special statements or agreements); 4. If there is no statement such as "no legal basis for refund" or similar content, any indication on the product that "once sold, no refunds will be supported" or other similar declarations shall be deemed invalid; 5. Before the buyer places an order and makes payment, the transaction details agreed upon by both parties via WhatsApp or email can also serve as the basis for dispute resolution (in case of any inconsistency between the agreement and the description of the conflict, the agreement shall prevail); 6. Since chat records and email records can serve as the basis for dispute resolution, both parties should only communicate with each other through the contact information left on the system when contacting each other, in order to prevent the other party from denying their own commitments. 7. Although the probability of disputes is low, it is essential to retain important information such as chat records, text messages, and email records, in case a dispute arises, so that ICTcoder can intervene quickly.
View details
  • 1. As a third-party intermediary platform, ICTcoder solely protects transaction security and the rights and interests of both buyers and sellers based on the transaction contract (product description, agreed content before the transaction); 2. For online trading projects not on the ICTcoder platform, any consequences are unrelated to this platform; regardless of the reason why the seller requests an offline transaction, please contact the administrator to report.
View details

Related Source code

ICTcoder Customer Service

24-hour online professional services