logo
0
0
WeChat Login

JustAuth Spring Boot 示例项目

这是一个使用 JustAuth 实现第三方登录的 Spring Boot 示例项目。

功能特性

  • ✅ 支持 GitHub 登录
  • ✅ 支持 Gitee 登录
  • ✅ 支持微信登录
  • ✅ 支持 QQ 登录
  • ✅ 支持 Google 登录
  • ✅ 精美的登录页面
  • ✅ 完整的用户信息展示
  • ✅ 简洁的代码结构

技术栈

  • Spring Boot 2.7.18
  • JustAuth 1.16.7
  • Thymeleaf
  • Maven
  • Lombok
  • Hutool

快速开始

1. 克隆项目

git clone <your-repo-url>
cd justauth-spring-boot-demo

2. 配置应用

编辑 src/main/resources/application.yml,配置各平台的客户端信息:

justauth:
  github:
    client-id: your-github-client-id
    client-secret: your-github-client-secret
    redirect-uri: http://localhost:8080/oauth/callback/github

  gitee:
    client-id: your-gitee-client-id
    client-secret: your-gitee-client-secret
    redirect-uri: http://localhost:8080/oauth/callback/gitee

  wechat:
    client-id: your-wechat-client-id
    client-secret: your-wechat-client-secret
    redirect-uri: http://localhost:8080/oauth/callback/wechat

  qq:
    client-id: your-qq-client-id
    client-secret: your-qq-client-secret
    redirect-uri: http://localhost:8080/oauth/callback/qq

  google:
    client-id: your-google-client-id
    client-secret: your-google-client-secret
    redirect-uri: http://localhost:8080/oauth/callback/google

3. 获取各平台的应用凭证

GitHub

  1. 访问 https://github.com/settings/developers
  2. 点击 "New OAuth App"
  3. 填写应用信息,回调 URL: http://localhost:8080/oauth/callback/github
  4. 获取 Client ID 和 Client Secret

Gitee

  1. 访问 https://gitee.com/oauth/applications
  2. 点击 "创建应用"
  3. 填写应用信息,回调 URL: http://localhost:8080/oauth/callback/gitee
  4. 获取 Client ID 和 Client Secret

微信

  1. 访问 https://open.weixin.qq.com/
  2. 申请公众号或小程序
  3. 配置网页授权域名
  4. 获取 AppID 和 AppSecret

QQ

  1. 访问 https://connect.qq.com/
  2. 申请应用
  3. 配置回调地址
  4. 获取 App ID 和 App Key

Google

  1. 访问 https://console.cloud.google.com/
  2. 创建项目并启用 Google+ API
  3. 创建 OAuth 2.0 凭据
  4. 获取 Client ID 和 Client Secret

4. 运行项目

# 使用 Maven 运行
mvn spring-boot:run

# 或先打包再运行
mvn clean package
java -jar target/justauth-spring-boot-demo-1.0.0.jar

5. 访问应用

打开浏览器访问:http://localhost:8080/oauth/login

项目结构

justauth-spring-boot-demo/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/justauth/
│   │   │       ├── JustAuthSpringBootDemoApplication.java
│   │   │       ├── config/
│   │   │       │   ├── AuthConfiguration.java
│   │   │       │   └── AuthProperties.java
│   │   │       └── controller/
│   │   │           └── OAuthController.java
│   │   └── resources/
│   │       ├── application.yml
│   │       └── templates/
│   │           ├── index.html
│   │           └── result.html
└── pom.xml

核心代码说明

AuthConfiguration

配置各平台的 AuthRequest Bean,使用 Spring 的依赖注入。

OAuthController

处理授权请求和回调:

  • /oauth/login - 登录页面
  • /oauth/authorize/{source} - 发起授权
  • /oauth/callback/{source} - 授权回调

AuthProperties

从配置文件读取各平台的配置信息。

扩展支持其他平台

如果需要支持其他平台,按以下步骤操作:

  1. application.yml 中添加平台配置
  2. AuthProperties.java 中添加对应的配置类
  3. AuthConfiguration.java 中添加对应的 Bean
  4. OAuthController.java 中添加对应的处理逻辑
  5. index.html 中添加登录按钮

常见问题

1. 回调地址错误

确保在各平台配置的回调地址与 application.yml 中的 redirect-uri 一致。

2. 授权失败

检查:

  • Client ID 和 Client Secret 是否正确
  • 回调地址是否正确
  • 网络是否可以访问第三方平台

3. 端口被占用

修改 application.yml 中的端口配置:

server:
  port: 8081

参考文档

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!

About

No description, topics, or website provided.
Language
Java63.1%
HTML35.3%
Dockerfile1.6%