Skip to content

Commit

Permalink
feat: 支持第三方账号登录
Browse files Browse the repository at this point in the history
Just Auth(开箱即用的整合第三方登录的开源组件,脱离繁琐的第三方登录 SDK,让登录变得 So easy!)
  • Loading branch information
Charles7c committed Oct 14, 2023
1 parent 71e20e9 commit 05cb609
Show file tree
Hide file tree
Showing 41 changed files with 826 additions and 49 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ContiNew Admin (Continue New Admin)中后台管理框架/脚手架,持续
> **Note**
> 更多功能和优化正在赶来💦,最新项目计划和进展请关注 [GitHub Project](https://github.com/Charles7c/continew-admin/projects)[CHANGELOG.md](https://github.com/Charles7c/continew-admin/blob/dev/CHANGELOG.md)
- 用户管理:提供用户的相关配置,新增用户后,默认密码为 123456
- 用户管理:提供用户的相关配置,新增用户后,默认密码为 123456,且支持第三方账号登录
- 部门管理:可配置系统组织架构,树形表格展示
- 角色管理:对权限与菜单进行分配,可根据部门设置角色的数据权限
- 菜单管理:已实现菜单动态路由,后端可配置化,支持多级菜单
Expand Down Expand Up @@ -235,6 +235,7 @@ pnpm dev
| <a href="https://github.com/liquibase/liquibase" target="_blank">Liquibase</a> | 4.9.1 | 用于管理数据库版本,跟踪、管理和应用数据库变化。 |
| <a href="https://redis.io/" target="_blank">Redis</a> | 6.2.7 | 高性能的 key-value 数据库。 |
| <a href="https://github.com/redisson/redisson/wiki/Redisson%E9%A1%B9%E7%9B%AE%E4%BB%8B%E7%BB%8D" target="_blank">Redisson</a> | 3.20.1 | 不仅仅是一个 Redis Java 客户端,同其他 Redis Java 客户端有着很大的区别,相比之下其他客户端提供的功能还仅仅停留在作为数据库驱动层面上,比如仅针对 Redis 提供连接方式,发送命令和处理返回结果等。而 Redisson 充分的利用了 Redis 键值数据库提供的一系列优势,基于 Java 实用工具包中常用接口,为使用者提供了一系列具有分布式特性的常用工具类。使得原本作为协调单机多线程并发程序的工具包获得了协调分布式多机多线程并发系统的能力,大大降低了设计和研发大规模分布式系统的难度。同时结合各富特色的分布式服务,更进一步简化了分布式环境中程序相互之间的协作。 |
| <a href="https://justauth.cn/" target="_blank">Just Auth</a> | 1.16.5 | 开箱即用的整合第三方登录的开源组件,脱离繁琐的第三方登录 SDK,让登录变得 So easy! |
| <a href="https://easyexcel.opensource.alibaba.com/" target="_blank">Easy Excel</a> | 3.3.2 | 一个基于 Java 的、快速、简洁、解决大文件内存溢出的 Excel 处理工具。 |
| Easy Captcha | 1.6.2 | Java 图形验证码,支持 gif、中文、算术等类型,可用于 Java Web、JavaSE 等项目。 |
| <a href="https://doc.xiaominfo.com/" target="_blank">Knife4j</a> | 4.3.0 | 前身是 swagger-bootstrap-ui,集 Swagger2 和 OpenAPI3 为一体的增强解决方案。本项目使用的是 <a href="https://gitee.com/xiaoym/swagger-bootstrap-ui-demo/tree/master/knife4j-springdoc-openapi-demo" target="_blank">knife4j-openapi3-spring-boot-starter</a> 基于 OpenAPI3 规范,在 Spring Boot < 3.0.0-M1 的单体架构下可以直接引用此 starter,该模块包含了 UI 部分,底层基于 springdoc-openapi 项目。 |
Expand Down
10 changes: 10 additions & 0 deletions continew-admin-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ limitations under the License.
</dependency>

<!-- ################ 工具库相关 ################ -->
<!-- Just Auth(开箱即用的整合第三方登录的开源组件,脱离繁琐的第三方登录 SDK,让登录变得 So easy!) -->
<dependency>
<groupId>com.xkcoding.justauth</groupId>
<artifactId>justauth-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
</dependency>

<!-- Easy Excel(一个基于 Java 的、快速、简洁、解决大文件内存溢出的 Excel 处理工具) -->
<dependency>
<groupId>com.alibaba</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.common.config.justauth;

import java.time.Duration;

import top.charles7c.cnadmin.common.constant.CacheConsts;
import top.charles7c.cnadmin.common.util.RedisUtils;

import me.zhyd.oauth.cache.AuthStateCache;

/**
* Just Auth 自定义 State 缓存实现(Redis)
*
* @author Charles7c
* @since 2023/10/8 22:17
*/
public class JustAuthRedisStateCache implements AuthStateCache {

/**
* 存入缓存
*
* @param key
* 缓存 key
* @param value
* 缓存内容
*/
@Override
public void cache(String key, String value) {
// 参考:在 JustAuth 中,内置了一个基于 map 的 state 缓存器,默认缓存有效期为 3 分钟
RedisUtils.setCacheObject(RedisUtils.formatKey(CacheConsts.SOCIAL_AUTH_STATE_KEY_PREFIX, key), value,
Duration.ofMinutes(3));
}

/**
* 存入缓存
*
* @param key
* 缓存 key
* @param value
* 缓存内容
* @param timeout
* 指定缓存过期时间(毫秒)
*/
@Override
public void cache(String key, String value, long timeout) {
RedisUtils.setCacheObject(RedisUtils.formatKey(CacheConsts.SOCIAL_AUTH_STATE_KEY_PREFIX, key), value,
Duration.ofMillis(timeout));
}

/**
* 获取缓存内容
*
* @param key
* 缓存 key
* @return 缓存内容
*/
@Override
public String get(String key) {
return RedisUtils.getCacheObject(RedisUtils.formatKey(CacheConsts.SOCIAL_AUTH_STATE_KEY_PREFIX, key));
}

/**
* 是否存在 key,如果对应 key 的 value 值已过期,也返回 false
*
* @param key
* 缓存 key
* @return true:存在 key,并且 value 没过期;false:key 不存在或者已过期
*/
@Override
public boolean containsKey(String key) {
return RedisUtils.hasKey(RedisUtils.formatKey(CacheConsts.SOCIAL_AUTH_STATE_KEY_PREFIX, key));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.common.config.justauth;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import me.zhyd.oauth.cache.AuthStateCache;

/**
* Just Auth State 缓存配置
*
* @author Charles7c
* @since 2023/10/8 22:17
*/
@Configuration
public class JustAuthStateConfiguration {

/**
* Just Auth State 缓存 Redis 适配
*/
@Bean
public AuthStateCache authStateCache() {
return new JustAuthRedisStateCache();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public class CacheConsts {
* 仪表盘缓存键前缀
*/
public static final String DASHBOARD_KEY_PREFIX = "DASHBOARD";

/**
* 社交身份认证状态键前缀
*/
public static final String SOCIAL_AUTH_STATE_KEY_PREFIX = "SOCIAL_AUTH_STATE";
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class SysConsts {
*/
public static final String ADMIN_ROLE_CODE = "admin";

/**
* 顶级部门 ID
*/
public static final Long SUPER_DEPT_ID = 1L;

/**
* 顶级父 ID
*/
Expand All @@ -53,6 +58,11 @@ public class SysConsts {
*/
public static final String LOGIN_URI = "/auth/login";

/**
* 退出 URI
*/
public static final String LOGOUT_URI = "/auth/logout";

/**
* VO 描述类字段后缀
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public class LoginHelper {
*
* @param loginUser
* 登录用户信息
* @return 令牌
*/
public static void login(LoginUser loginUser) {
public static String login(LoginUser loginUser) {
// 记录登录信息
HttpServletRequest request = ServletUtils.getRequest();
loginUser.setClientIp(ServletUtil.getClientIP(request));
Expand All @@ -65,8 +66,10 @@ public static void login(LoginUser loginUser) {
// 登录并缓存用户信息
StpUtil.login(loginUser.getId());
SaHolder.getStorage().set(CacheConsts.LOGIN_USER_KEY, loginUser);
loginUser.setToken(StpUtil.getTokenValue());
String tokenValue = StpUtil.getTokenValue();
loginUser.setToken(tokenValue);
StpUtil.getTokenSession().set(CacheConsts.LOGIN_USER_KEY, loginUser);
return tokenValue;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,16 @@ private void logDescription(LogDO logDO, HandlerMethod handlerMethod) {
private void logRequest(LogDO logDO, HttpServletRequest request) {
logDO.setRequestUrl(StrUtil.isBlank(request.getQueryString()) ? request.getRequestURL().toString()
: request.getRequestURL().append(StringConsts.QUESTION_MARK).append(request.getQueryString()).toString());
logDO.setRequestMethod(request.getMethod());
String method = request.getMethod();
logDO.setRequestMethod(method);
logDO.setRequestHeaders(this.desensitize(ServletUtil.getHeaderMap(request)));
String requestBody = this.getRequestBody(request);
logDO.setCreateUser(ObjectUtil.defaultIfNull(logDO.getCreateUser(), LoginHelper.getUserId()));
if (null == logDO.getCreateUser() && SysConsts.LOGIN_URI.equals(request.getRequestURI())) {
String requestURI = request.getRequestURI();
if (requestURI.startsWith("/auth") && !SysConsts.LOGOUT_URI.equals(requestURI)) {
logDO.setCreateUser(null);
}
if (null == logDO.getCreateUser() && SysConsts.LOGIN_URI.equals(requestURI)) {
LoginRequest loginRequest = JSONUtil.toBean(requestBody, LoginRequest.class);
logDO.setCreateUser(
ExceptionUtils.exToNull(() -> userService.getByUsername(loginRequest.getUsername()).getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import top.charles7c.cnadmin.auth.model.vo.RouteVO;

import me.zhyd.oauth.model.AuthUser;

/**
* 登录业务接口
*
Expand All @@ -39,6 +41,15 @@ public interface LoginService {
*/
String login(String username, String password);

/**
* 社交身份登录
*
* @param authUser
* 社交身份信息
* @return 令牌
*/
String socialLogin(AuthUser authUser);

/**
* 构建路由树
*
Expand Down
Loading

0 comments on commit 05cb609

Please sign in to comment.