mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-11 16:57:12 +08:00
feat: 支持第三方账号登录
Just Auth(开箱即用的整合第三方登录的开源组件,脱离繁琐的第三方登录 SDK,让登录变得 So easy!)
This commit is contained in:
@@ -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>
|
||||
|
@@ -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));
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
@@ -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";
|
||||
}
|
||||
|
@@ -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
|
||||
*/
|
||||
@@ -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 描述类字段后缀
|
||||
*/
|
||||
|
@@ -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));
|
||||
@@ -65,8 +66,10 @@ public class LoginHelper {
|
||||
// 登录并缓存用户信息
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user