preface

Last time we gave amway an open source third-party authorization login tool library, so far the code cloud has been obtained

More than 40

The project was listed as a recommended project by Code Cloud on the first day of its launch, and has ranked first in the daily and weekly rankings for three days so far.

JustAuth
Third-party Authorized Login

This article summarizes how to use Github to authorize login

Access to the process

As mentioned in the previous article [link], the open source library access process is divided into three steps, and all platforms currently follow these three steps.

  1. Apply to register a developer account for a third-party platform
  2. Create third-party platform application, obtain configuration information (ID, secret, callbackUrl)
  3. Use this tool to achieve authorized login

The actual operation

Note: Developer documentation: developer.github.com/

1. Create a Github account

Github.com/. If so, ignore this step…

2. Create third-party applications

Enter Enter the personal Settings page in the upper right corner: github.com/settings/pr…

Github.com/settings/de…

On the App Management page, select OAuth App, and then click the New OAuth App button in the upper right to enter the New App page

Application name specifies the name of your website. Application description specifies the description of your Application. URL specifies the Homepage address of your website Important, this address is the address that the user needs to jump to after authorization, and carries a code parameter by default

After entering the above information, click the green Register ApplAction button in the bottom to create an application. After the application is created, you can click the application details page to view the application key and other information

Copy The Client ID, Client Secret, and Authorization callback URL.

Use 3.JustAuthRealizing authorized login

Creating an Authorization Request

AuthRequest authRequest = new AuthGithubRequest(AuthConfig.builder()
                .clientId("Client ID")
                .clientSecret("Client Secret")
                .redirectUri("Authorization callback URL")
                .build());
Copy the code

Generating an Authorization Address

authRequest.authorize()
Copy the code

After an authorization address is generated, you can redirect it to the authorization page.

After the authorization is complete, log in

AuthResponse Response = AuthRequest.login (code); // Code is the parameter carried when the user is authorized to callback to the local system.Copy the code

Response data structure is as follows:

{
  "code": 0."msg": null,
  "data": {
    "username": "zhangyd-c"."avatar": "https://avatars3.githubusercontent.com/u/12689082?v=4"."blog": "https://www.zhyd.me"."nickname": "yadong.zhang"."company": "innodev"."location": "Beijing"."email": "[email protected]"."remark": "There is nothing the heart desires."."gender": null,
    "source": "GITHUB"."accessToken": "f00969002de1a7b4bf411c39d917dab657c9ee5fd"}}Copy the code

Github authorized login is complete!

Finally, attach the integrated version of the above small example:

import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/oauth")
public class RestAuthController {

    @RequestMapping("/render/github")
    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        response.sendRedirect(authRequest.authorize());
    }

    @RequestMapping("/callback/github")
    public Object login(String code) {
        AuthRequest authRequest = getAuthRequest();
        return authRequest.login(code);
    }

    private AuthRequest getAuthRequest() {
        return new AuthGithubRequest(AuthConfig.builder()
                .clientId("Client ID")
                .clientSecret("Client Secret")
                .redirectUri("Authorization callback URL") .build()); }}Copy the code

For more APIS, please refer to the original project documentation, project address

  • Making: github.com/zhangyd-c/J…
  • Gitee: gitee.com/yadong.zhan…

Follow my official account