Quickly complete the third-party authorized login tool library JustAuth

Quickly complete the third-party authorized login tool library JustAuth

2022-09-02 0 408
Resource Number 38013 Last Updated 2025-02-24
¥ 0USD 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/kyym/quickly-complete-the-third-party-authorized-login-tool-library-justauth.html

Share free open-source source code

Q&A
  • 1, automatic: after taking the photo, click the (download) link to download; 2. Manual: After taking the photo, contact the seller to issue it or contact the official to find the developer to ship.
View details
  • 1, the default transaction cycle of the source code: manual delivery of goods for 1-3 days, and the user payment amount will enter the platform guarantee until the completion of the transaction or 3-7 days can be issued, in case of disputes indefinitely extend the collection amount until the dispute is resolved or refunded!
View details
  • 1. Heptalon will permanently archive the process of trading between the two parties and the snapshots of the traded goods to ensure that the transaction is true, effective and safe! 2, Seven PAWS can not guarantee such as "permanent package update", "permanent technical support" and other similar transactions after the merchant commitment, please identify the buyer; 3, in the source code at the same time there is a website demonstration and picture demonstration, and the site is inconsistent with the diagram, the default according to the diagram as the dispute evaluation basis (except for special statements or agreement); 4, in the absence of "no legitimate basis for refund", the commodity written "once sold, no support for refund" and other similar statements, shall be deemed invalid; 5, before the shooting, the transaction content agreed by the two parties on QQ can also be the basis for dispute judgment (agreement and description of the conflict, the agreement shall prevail); 6, because the chat record can be used as the basis for dispute judgment, so when the two sides contact, only communicate with the other party on the QQ and mobile phone number left on the systemhere, in case the other party does not recognize self-commitment. 7, although the probability of disputes is very small, but be sure to retain such important information as chat records, mobile phone messages, etc., in case of disputes, it is convenient for seven PAWS to intervene in rapid processing.
View details
  • 1. As a third-party intermediary platform, Qichou protects the security of the transaction and the rights and interests of both buyers and sellers according to the transaction contract (commodity description, content agreed before the transaction); 2, non-platform online trading projects, any consequences have nothing to do with mutual site; No matter the seller for any reason to require offline transactions, please contact the management report.
View details

Related Article

make a comment
No comments available at the moment
Official customer service team

To solve your worries - 24 hours online professional service