这是一个使用 JustAuth 实现第三方登录的 Spring Boot 示例项目。
git clone <your-repo-url>
cd justauth-spring-boot-demo
编辑 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
http://localhost:8080/oauth/callback/githubhttp://localhost:8080/oauth/callback/gitee# 使用 Maven 运行
mvn spring-boot:run
# 或先打包再运行
mvn clean package
java -jar target/justauth-spring-boot-demo-1.0.0.jar
打开浏览器访问: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
配置各平台的 AuthRequest Bean,使用 Spring 的依赖注入。
处理授权请求和回调:
/oauth/login - 登录页面/oauth/authorize/{source} - 发起授权/oauth/callback/{source} - 授权回调从配置文件读取各平台的配置信息。
如果需要支持其他平台,按以下步骤操作:
application.yml 中添加平台配置AuthProperties.java 中添加对应的配置类AuthConfiguration.java 中添加对应的 BeanOAuthController.java 中添加对应的处理逻辑index.html 中添加登录按钮确保在各平台配置的回调地址与 application.yml 中的 redirect-uri 一致。
检查:
修改 application.yml 中的端口配置:
server:
port: 8081
MIT License
欢迎提交 Issue 和 Pull Request!