mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-10-26 18:58:37 +08:00
feat: 支持第三方账号登录
Just Auth(开箱即用的整合第三方登录的开源组件,脱离繁琐的第三方登录 SDK,让登录变得 So easy!)
This commit is contained in:
@@ -49,16 +49,17 @@ import top.charles7c.cnadmin.system.model.vo.UserDetailVO;
|
||||
import top.charles7c.cnadmin.system.service.UserService;
|
||||
|
||||
/**
|
||||
* 登录 API
|
||||
* 认证 API
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/21 20:37
|
||||
*/
|
||||
@Tag(name = "登录 API")
|
||||
@Log(module = "登录")
|
||||
@Tag(name = "认证 API")
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/auth")
|
||||
public class LoginController {
|
||||
public class AuthController {
|
||||
|
||||
private final LoginService loginService;
|
||||
private final UserService userService;
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.webapi.controller.auth;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import com.xkcoding.justauth.AuthRequestFactory;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
|
||||
import top.charles7c.cnadmin.auth.model.vo.LoginVO;
|
||||
import top.charles7c.cnadmin.auth.service.LoginService;
|
||||
import top.charles7c.cnadmin.common.exception.BadRequestException;
|
||||
import top.charles7c.cnadmin.common.model.vo.R;
|
||||
import top.charles7c.cnadmin.common.util.validate.ValidationUtils;
|
||||
import top.charles7c.cnadmin.monitor.annotation.Log;
|
||||
|
||||
import me.zhyd.oauth.model.AuthCallback;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.request.AuthRequest;
|
||||
import me.zhyd.oauth.utils.AuthStateUtils;
|
||||
|
||||
/**
|
||||
* 社交身份认证 API
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/10/8 22:52
|
||||
*/
|
||||
@Log(module = "登录")
|
||||
@Tag(name = "社交身份认证 API")
|
||||
@SaIgnore
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/auth")
|
||||
public class SocialAuthController {
|
||||
|
||||
private final LoginService loginService;
|
||||
private final AuthRequestFactory authRequestFactory;
|
||||
|
||||
@Operation(summary = "社交身份登录授权", description = "社交身份登录授权")
|
||||
@Parameter(name = "source", description = "来源", example = "gitee", in = ParameterIn.PATH)
|
||||
@GetMapping("/{source}")
|
||||
public R authorize(@PathVariable String source) {
|
||||
AuthRequest authRequest = this.getAuthRequest(source);
|
||||
return R.ok("操作成功", authRequest.authorize(AuthStateUtils.createState()));
|
||||
}
|
||||
|
||||
@Operation(summary = "社交身份登录", description = "社交身份登录")
|
||||
@Parameter(name = "source", description = "来源", example = "gitee", in = ParameterIn.PATH)
|
||||
@PostMapping("/{source}")
|
||||
public LoginVO login(@PathVariable String source, @RequestBody AuthCallback callback) {
|
||||
AuthRequest authRequest = this.getAuthRequest(source);
|
||||
AuthResponse<AuthUser> response = authRequest.login(callback);
|
||||
ValidationUtils.throwIf(!response.ok(), response.getMsg());
|
||||
AuthUser authUser = response.getData();
|
||||
String token = loginService.socialLogin(authUser);
|
||||
return LoginVO.builder().token(token).build();
|
||||
}
|
||||
|
||||
private AuthRequest getAuthRequest(String source) {
|
||||
try {
|
||||
return authRequestFactory.get(source);
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException(String.format("暂不支持 [%s] 登录", source));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,8 @@
|
||||
--- ### 项目配置
|
||||
project:
|
||||
# URL
|
||||
url: http://localhost:5173
|
||||
|
||||
--- ### 服务器配置
|
||||
server:
|
||||
# HTTP 端口(默认 8080)
|
||||
@@ -73,6 +78,21 @@ spring:
|
||||
# 是否允许缓存空值(默认 true,表示允许,可以解决缓存穿透问题)
|
||||
cache-null-values: true
|
||||
|
||||
--- ### Just Auth 配置
|
||||
justauth:
|
||||
enabled: true
|
||||
type:
|
||||
GITEE:
|
||||
client-id: 5d271b7f638941812aaf8bfc2e2f08f06d6235ef934e0e39537e2364eb8452c4
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: ${project.url}/social/callback?source=gitee
|
||||
GITHUB:
|
||||
client-id: 38080dad08cfbdfacca9
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: ${project.url}/social/callback?source=github
|
||||
cache:
|
||||
type: custom
|
||||
|
||||
--- ### 邮件配置
|
||||
spring.mail:
|
||||
# 根据需要更换
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
--- ### 项目配置
|
||||
project:
|
||||
# URL
|
||||
url: https://cnadmin.charles7c.top
|
||||
|
||||
--- ### 服务器配置
|
||||
server:
|
||||
# HTTP 端口(默认 8080)
|
||||
@@ -73,6 +78,21 @@ spring:
|
||||
# 是否允许缓存空值(默认 true,表示允许,可以解决缓存穿透问题)
|
||||
cache-null-values: true
|
||||
|
||||
--- ### Just Auth 配置
|
||||
justauth:
|
||||
enabled: true
|
||||
type:
|
||||
GITEE:
|
||||
client-id: 5d271b7f638941812aaf8bfc2e2f08f06d6235ef934e0e39537e2364eb8452c4
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: ${project.url}/social/callback?source=gitee
|
||||
GITHUB:
|
||||
client-id: 38080dad08cfbdfacca9
|
||||
client-secret: 1f7d08**********5b7**********29e
|
||||
redirect-uri: ${project.url}/social/callback?source=github
|
||||
cache:
|
||||
type: custom
|
||||
|
||||
--- ### 邮件配置
|
||||
spring.mail:
|
||||
# 根据需要更换
|
||||
|
||||
@@ -8,8 +8,6 @@ project:
|
||||
version: 1.3.0-SNAPSHOT
|
||||
# 描述
|
||||
description: ContiNew Admin 中后台管理框架/脚手架,Continue New Admin,持续以最新流行技术栈构建,拥抱变化,迭代优化。
|
||||
# URL
|
||||
url: https://cnadmin.charles7c.top
|
||||
# 基本包
|
||||
basePackage: top.charles7c.cnadmin
|
||||
## 作者信息配置
|
||||
|
||||
@@ -17,3 +17,9 @@ databaseChangeLog:
|
||||
file: db/changelog/v1.2.0/continew-admin_column.sql
|
||||
- include:
|
||||
file: db/changelog/v1.2.0/continew-admin_data.sql
|
||||
- include:
|
||||
file: db/changelog/v1.3.0/continew-admin_table.sql
|
||||
- include:
|
||||
file: db/changelog/v1.3.0/continew-admin_column.sql
|
||||
- include:
|
||||
file: db/changelog/v1.3.0/continew-admin_data.sql
|
||||
|
||||
@@ -93,7 +93,7 @@ CREATE TABLE IF NOT EXISTS `sys_user` (
|
||||
`is_system` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否为系统内置数据',
|
||||
`pwd_reset_time` datetime DEFAULT NULL COMMENT '最后一次修改密码时间',
|
||||
`dept_id` bigint(20) NOT NULL COMMENT '部门ID',
|
||||
`create_user` bigint(20) NOT NULL COMMENT '创建人',
|
||||
`create_user` bigint(20) DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime NOT NULL COMMENT '创建时间',
|
||||
`update_user` bigint(20) DEFAULT NULL COMMENT '修改人',
|
||||
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- liquibase formatted sql
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- liquibase formatted sql
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
-- liquibase formatted sql
|
||||
|
||||
-- changeset Charles7c:1
|
||||
CREATE TABLE IF NOT EXISTS `sys_user_social` (
|
||||
`source` varchar(255) NOT NULL COMMENT '来源',
|
||||
`open_id` varchar(255) NOT NULL COMMENT '开放ID',
|
||||
`user_id` bigint(20) NOT NULL COMMENT '用户ID',
|
||||
`meta_json` text DEFAULT NULL COMMENT '附加信息',
|
||||
`last_login_time` datetime DEFAULT NULL COMMENT '最后登录时间',
|
||||
`create_time` datetime NOT NULL COMMENT '创建时间',
|
||||
UNIQUE INDEX `uk_source_open_id`(`source`, `open_id`) USING BTREE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户社会化关联表';
|
||||
Reference in New Issue
Block a user