oauth2-Github第三方登录案例

oauth2-Github第三方登录案例

  • 1.在github上申请一个app应用案例,获取client_id和secret
  • 2.案例
  • 3.测试

1.在github上申请一个app应用案例,获取client_id和secret

oauth2-Github第三方登录案例_第1张图片
2.
oauth2-Github第三方登录案例_第2张图片
3.
oauth2-Github第三方登录案例_第3张图片

oauth2-Github第三方登录案例_第4张图片

5.查看client_id和生成client_secret
oauth2-Github第三方登录案例_第5张图片

2.案例

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>github_login</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>github_login</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.主程序

package com.yl.github_login;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class GithubLoginApplication {

    public static void main(String[] args) {
        SpringApplication.run(GithubLoginApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


}

3.controller
oauth2-Github第三方登录案例_第6张图片

4.index.html(存放于template目录)
oauth2-Github第三方登录案例_第7张图片

3.测试

1.访问index.html
oauth2-Github第三方登录案例_第8张图片

2.登录github账号
oauth2-Github第三方登录案例_第9张图片

3.登录成功后,可以看到能获取登录的账号信息
oauth2-Github第三方登录案例_第10张图片
oauth2-Github第三方登录案例_第11张图片

你可能感兴趣的:(spring,security,spring)