mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-19 17:35:54 +08:00
[功能]:1、添加http通用接口forest 2、引入justoauth 处理第三方登录 3、大致完成qq登录代码 4、前端界面修改适配第三方登录逻辑
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.ruoyi.iot.service;
|
||||
|
||||
import com.ruoyi.iot.model.login.AuthRequestWrap;
|
||||
|
||||
/**
|
||||
* AuthRequest简单工程类接口
|
||||
*
|
||||
* @author json
|
||||
* @date 2022-04-12
|
||||
*/
|
||||
public interface IAuthRequestFactory {
|
||||
|
||||
AuthRequestWrap getAuthRequest(String source);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.ruoyi.iot.service;
|
||||
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.domain.model.BindLoginBody;
|
||||
import com.ruoyi.common.core.domain.model.BindRegisterBody;
|
||||
import me.zhyd.oauth.model.AuthCallback;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 第三方登录Service接口
|
||||
* 处理登录跳转业务逻辑
|
||||
*
|
||||
* @author json
|
||||
* @date 2022-04-12
|
||||
*/
|
||||
public interface ISocialLoginService {
|
||||
|
||||
/**
|
||||
* 第三方登录跳转
|
||||
*
|
||||
* @param source 平台
|
||||
* @param httpServletRequest 当前请求
|
||||
* @return 跳转路径
|
||||
*/
|
||||
String renderAuth(String source, HttpServletRequest httpServletRequest);
|
||||
|
||||
/**
|
||||
* 第三方登录callback
|
||||
*
|
||||
* @param source 平台
|
||||
* @param authCallback 回调参数
|
||||
* @param httpServletRequest 当前请求
|
||||
* @return 跳转路径
|
||||
*/
|
||||
String callback(String source, AuthCallback authCallback, HttpServletRequest httpServletRequest);
|
||||
|
||||
/**
|
||||
* 检查是否bindId
|
||||
*
|
||||
* @param bindId 绑定id
|
||||
* @return
|
||||
*/
|
||||
AjaxResult checkBindId(String bindId);
|
||||
|
||||
/**
|
||||
* 获得错误显示
|
||||
*
|
||||
* @param errorId errorId
|
||||
* @return
|
||||
*/
|
||||
AjaxResult getErrorMsg(String errorId);
|
||||
|
||||
/**
|
||||
* 跳转直接登录
|
||||
*
|
||||
* @param loginId 登录id
|
||||
* @return
|
||||
*/
|
||||
AjaxResult socialLogin(String loginId);
|
||||
|
||||
/**
|
||||
* 绑定登录api
|
||||
*
|
||||
* @param bindLoginBody 绑定账户参数
|
||||
* @return
|
||||
*/
|
||||
AjaxResult bindLogin(BindLoginBody bindLoginBody);
|
||||
|
||||
/**
|
||||
* 注册绑定api
|
||||
*
|
||||
* @param bindRegisterBody
|
||||
* @return
|
||||
*/
|
||||
AjaxResult bindRegister(BindRegisterBody bindRegisterBody);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.ruoyi.iot.service;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.iot.domain.SocialPlatform;
|
||||
|
||||
/**
|
||||
* 第三方登录平台控制Service接口
|
||||
*
|
||||
* @author json
|
||||
* @date 2022-04-12
|
||||
*/
|
||||
public interface ISocialPlatformService
|
||||
{
|
||||
/**
|
||||
* 查询第三方登录平台控制
|
||||
*
|
||||
* @param socialPlatformId 第三方登录平台控制主键
|
||||
* @return 第三方登录平台控制
|
||||
*/
|
||||
public SocialPlatform selectSocialPlatformBySocialPlatformId(Long socialPlatformId);
|
||||
|
||||
/**
|
||||
* 查询第三方登录平台控制
|
||||
*
|
||||
* @param platform 第三方登录平台名称
|
||||
* @return 第三方登录平台控制
|
||||
*/
|
||||
public SocialPlatform selectSocialPlatformByPlatform(String platform);
|
||||
|
||||
/**
|
||||
* 查询第三方登录平台控制列表
|
||||
*
|
||||
* @param socialPlatform 第三方登录平台控制
|
||||
* @return 第三方登录平台控制集合
|
||||
*/
|
||||
public List<SocialPlatform> selectSocialPlatformList(SocialPlatform socialPlatform);
|
||||
|
||||
/**
|
||||
* 新增第三方登录平台控制
|
||||
*
|
||||
* @param socialPlatform 第三方登录平台控制
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertSocialPlatform(SocialPlatform socialPlatform);
|
||||
|
||||
/**
|
||||
* 修改第三方登录平台控制
|
||||
*
|
||||
* @param socialPlatform 第三方登录平台控制
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateSocialPlatform(SocialPlatform socialPlatform);
|
||||
|
||||
/**
|
||||
* 批量删除第三方登录平台控制
|
||||
*
|
||||
* @param socialPlatformIds 需要删除的第三方登录平台控制主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteSocialPlatformBySocialPlatformIds(Long[] socialPlatformIds);
|
||||
|
||||
/**
|
||||
* 删除第三方登录平台控制信息
|
||||
*
|
||||
* @param socialPlatformId 第三方登录平台控制主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteSocialPlatformBySocialPlatformId(Long socialPlatformId);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.ruoyi.iot.service;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.iot.domain.SocialUser;
|
||||
|
||||
/**
|
||||
* 用户第三方用户信息Service接口
|
||||
*
|
||||
* @author json
|
||||
* @date 2022-04-18
|
||||
*/
|
||||
public interface ISocialUserService
|
||||
{
|
||||
/**
|
||||
* 查询用户第三方用户信息
|
||||
*
|
||||
* @param socialUserId 用户第三方用户信息主键
|
||||
* @return 用户第三方用户信息
|
||||
*/
|
||||
public SocialUser selectSocialUserBySocialUserId(Long socialUserId);
|
||||
|
||||
/**
|
||||
* 查询用户第三方用户信息列表
|
||||
*
|
||||
* @param socialUser 用户第三方用户信息
|
||||
* @return 用户第三方用户信息集合
|
||||
*/
|
||||
public List<SocialUser> selectSocialUserList(SocialUser socialUser);
|
||||
|
||||
/**
|
||||
* 新增用户第三方用户信息
|
||||
*
|
||||
* @param socialUser 用户第三方用户信息
|
||||
* @return 结果
|
||||
*/
|
||||
public int insertSocialUser(SocialUser socialUser);
|
||||
|
||||
/**
|
||||
* 修改用户第三方用户信息
|
||||
*
|
||||
* @param socialUser 用户第三方用户信息
|
||||
* @return 结果
|
||||
*/
|
||||
public int updateSocialUser(SocialUser socialUser);
|
||||
|
||||
/**
|
||||
* 批量删除用户第三方用户信息
|
||||
*
|
||||
* @param socialUserIds 需要删除的用户第三方用户信息主键集合
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteSocialUserBySocialUserIds(Long[] socialUserIds);
|
||||
|
||||
/**
|
||||
* 删除用户第三方用户信息信息
|
||||
*
|
||||
* @param socialUserId 用户第三方用户信息主键
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteSocialUserBySocialUserId(Long socialUserId);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.ruoyi.iot.service.impl;
|
||||
|
||||
import com.ruoyi.common.enums.SocialPlatformType;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.iot.domain.SocialPlatform;
|
||||
import com.ruoyi.iot.model.login.AuthRequestWrap;
|
||||
import com.ruoyi.iot.service.IAuthRequestFactory;
|
||||
import com.ruoyi.iot.service.ISocialPlatformService;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.request.AuthQqRequest;
|
||||
import me.zhyd.oauth.request.AuthRequest;
|
||||
import me.zhyd.oauth.request.AuthWeChatMpRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Service
|
||||
public class AuthRequestFactoryImpl implements IAuthRequestFactory {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AuthRequestFactoryImpl.class);
|
||||
|
||||
@Autowired
|
||||
private ISocialPlatformService iSocialPlatformService;
|
||||
|
||||
@Autowired
|
||||
private AuthStateRedisCache authStateRedisCache;
|
||||
|
||||
/**
|
||||
* 获得对于AUthRequest
|
||||
*
|
||||
* @param source 登录方式
|
||||
* @return 对应AuthRequest
|
||||
*/
|
||||
@Override
|
||||
public AuthRequestWrap getAuthRequest(String source) {
|
||||
AuthRequestWrap authRequestWrap = new AuthRequestWrap();
|
||||
AuthRequest authRequest;
|
||||
try {
|
||||
SocialPlatformType socialPlatformType = SocialPlatformType.valueOf(source.toUpperCase(Locale.ROOT));
|
||||
SocialPlatform socialPlatform = iSocialPlatformService.selectSocialPlatformByPlatform(source);
|
||||
authRequestWrap.setSocialPlatform(socialPlatform);
|
||||
AuthConfig authConfig = AuthConfig.builder()
|
||||
.clientId(socialPlatform.getClientId())
|
||||
.clientSecret(socialPlatform.getSecretKey())
|
||||
.redirectUri(socialPlatform.getRedirectUri())
|
||||
.build();
|
||||
switch (socialPlatformType) {
|
||||
case QQ: {
|
||||
authRequest = new AuthQqRequest(authConfig, authStateRedisCache);
|
||||
break;
|
||||
}
|
||||
case Wechat: {
|
||||
authRequest = new AuthWeChatMpRequest(authConfig, authStateRedisCache);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw new ServiceException("source: " + source + ",暂不支持");
|
||||
}
|
||||
}
|
||||
authRequestWrap.setAuthRequest(authRequest);
|
||||
return authRequestWrap;
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.ruoyi.iot.service.impl;
|
||||
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.common.utils.sign.Md5Utils;
|
||||
import me.zhyd.oauth.cache.AuthCacheConfig;
|
||||
import me.zhyd.oauth.cache.AuthStateCache;
|
||||
import me.zhyd.oauth.utils.AuthStateUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 扩展Redis版的state缓存
|
||||
*
|
||||
* @author json
|
||||
* @date 2022/04/12
|
||||
*/
|
||||
@Component
|
||||
public class AuthStateRedisCache implements AuthStateCache {
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
@Override
|
||||
public void cache(String key, String value) {
|
||||
redisCache.setCacheObject(key, getValue(value), (int) AuthCacheConfig.timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cache(String key, String value, long timeout) {
|
||||
redisCache.setCacheObject(key, getValue(value), (int) timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return redisCache.getCacheObject(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(String key) {
|
||||
return redisCache.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义state
|
||||
* @param oldState
|
||||
* @return state
|
||||
*/
|
||||
private String getValue(String oldState) {
|
||||
return Md5Utils.hash(oldState + System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
package com.ruoyi.iot.service.impl;
|
||||
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.domain.model.BindLoginBody;
|
||||
import com.ruoyi.common.core.domain.model.BindRegisterBody;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.sign.Md5Utils;
|
||||
import com.ruoyi.framework.web.service.SysLoginService;
|
||||
import com.ruoyi.framework.web.service.SysRegisterService;
|
||||
import com.ruoyi.framework.web.service.TokenService;
|
||||
import com.ruoyi.iot.domain.SocialPlatform;
|
||||
import com.ruoyi.iot.domain.SocialUser;
|
||||
import com.ruoyi.iot.model.login.AuthRequestWrap;
|
||||
import com.ruoyi.iot.model.login.BindIdValue;
|
||||
import com.ruoyi.iot.model.login.LoginIdValue;
|
||||
import com.ruoyi.iot.service.IAuthRequestFactory;
|
||||
import com.ruoyi.iot.service.ISocialLoginService;
|
||||
import com.ruoyi.iot.service.ISocialUserService;
|
||||
import com.ruoyi.system.service.ISysConfigService;
|
||||
import com.ruoyi.system.service.ISysUserService;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthCallback;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.utils.AuthStateUtils;
|
||||
import me.zhyd.oauth.utils.RandomUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.ruoyi.common.constant.HttpStatus.NO_MESSAGE_ALERT;
|
||||
import static com.ruoyi.common.core.domain.AjaxResult.error;
|
||||
|
||||
/**
|
||||
* 第三方登录Service业务层处理
|
||||
*
|
||||
* @author json
|
||||
* @date 2022-04-12
|
||||
*/
|
||||
@Service
|
||||
public class SocialLoginServiceImpl implements ISocialLoginService {
|
||||
|
||||
public static final Integer BIND_EXPIRE_TIME = 60 * 60;
|
||||
public static final Integer LOGIN_SOCIAL_EXPIRE_TIME = 60;
|
||||
public static final String ONLINE_STATUS = "0"; //1 offline
|
||||
public static final String DEL_FLAG = "0"; //1 offline
|
||||
|
||||
//redis key: uuid+source
|
||||
public static final String BIND_REDIS_KEY = "login:bind:user:";
|
||||
//redis key : userId+random 32
|
||||
public static final String LOGIN_SOCIAL_REDIS_KEY = "login:social:user:";
|
||||
//redis key : msg+ code+currentTime
|
||||
public static final String LOGIN_ERROR_MSG_REDIS_KEY = "login:error:msg:";
|
||||
|
||||
public static final String HTTPS = "https://";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SocialLoginServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
@Autowired
|
||||
private ISysConfigService iSysConfigService;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
@Autowired
|
||||
private SysRegisterService sysRegisterService;
|
||||
|
||||
@Autowired
|
||||
private SysLoginService sysLoginService;
|
||||
|
||||
@Autowired
|
||||
private ISysUserService iSysUserService;
|
||||
|
||||
@Autowired
|
||||
private IAuthRequestFactory iAuthRequestFactory;
|
||||
|
||||
@Autowired
|
||||
private ISocialUserService iSocialUserService;
|
||||
|
||||
|
||||
@Override
|
||||
public String renderAuth(String source, HttpServletRequest httpServletRequest) {
|
||||
AuthRequestWrap authRequestWrap = null;
|
||||
try {
|
||||
authRequestWrap = iAuthRequestFactory.getAuthRequest(source);
|
||||
checkSocialPlatform(authRequestWrap.getSocialPlatform());
|
||||
return authRequestWrap.getAuthRequest().authorize(AuthStateUtils.createState());
|
||||
} catch (AuthException authException) {
|
||||
//返回错误信息
|
||||
log.error("", authException);
|
||||
if (authRequestWrap != null) {
|
||||
String errorId = genErrorId(authException.getMessage());
|
||||
return authRequestWrap.getSocialPlatform().getErrorMsgUri() + errorId;
|
||||
} else {
|
||||
return httpServletRequest.getProtocol() + httpServletRequest.getServerName() + httpServletRequest.getServerPort();
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
//这类错误 直接不返回,重定向到主页
|
||||
log.error("", exception);
|
||||
return HTTPS + httpServletRequest.getServerName();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String callback(String source, AuthCallback authCallback, HttpServletRequest httpServletRequest) {
|
||||
AuthRequestWrap authRequestWrap = null;
|
||||
try {
|
||||
authRequestWrap = iAuthRequestFactory.getAuthRequest(source);
|
||||
checkSocialPlatform(authRequestWrap.getSocialPlatform());
|
||||
AuthResponse<AuthUser> authResponse = authRequestWrap.getAuthRequest().login(authCallback);
|
||||
String bindId = null;
|
||||
String loginId = null;
|
||||
if (authResponse.ok()) {
|
||||
|
||||
SocialUser socialUser = findSocialUser(authResponse.getData().getUuid(), authResponse.getData().getSource());
|
||||
createOrUpdateSocialUser(socialUser, authResponse.getData());
|
||||
if (socialUser == null) {
|
||||
//第一次登录
|
||||
bindId = genBindId(authResponse.getData());
|
||||
} else if (socialUser.getSysUserId() == null || socialUser.getSysUserId() <= 0) {
|
||||
//初次绑定
|
||||
bindId = genBindId(authResponse.getData());
|
||||
} else {
|
||||
//查看是否已经绑定
|
||||
SysUser sysUser = iSysUserService.selectUserById(socialUser.getSysUserId());
|
||||
if (sysUser == null) {
|
||||
bindId = genBindId(authResponse.getData());
|
||||
} else {
|
||||
//直接登录跳转
|
||||
loginId = genLoginId(sysUser);
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotEmpty(bindId)) {
|
||||
return authRequestWrap.getSocialPlatform().getBindUri() + bindId;
|
||||
} else {
|
||||
return authRequestWrap.getSocialPlatform().getRedirectLoginUri() + loginId;
|
||||
}
|
||||
} else {
|
||||
log.error("登录授权异常,code:{}, msg:{}", authResponse.getCode(), authResponse.getMsg());
|
||||
String errorId = genErrorId(authResponse.getMsg());
|
||||
return authRequestWrap.getSocialPlatform().getErrorMsgUri() + errorId;
|
||||
}
|
||||
} catch (AuthException authException) {
|
||||
//返回错误信息
|
||||
log.error("", authException);
|
||||
if (authRequestWrap != null) {
|
||||
String errorId = genErrorId(authException.getMessage());
|
||||
return authRequestWrap.getSocialPlatform().getErrorMsgUri() + errorId;
|
||||
} else {
|
||||
return httpServletRequest.getServerName() + httpServletRequest.getServerPort();
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
log.error("", exception);
|
||||
return HTTPS + httpServletRequest.getServerName();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult checkBindId(String bindId) {
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
ajax.put("bindAccount", false);
|
||||
if (StringUtils.isEmpty(bindId)) {
|
||||
return ajax;
|
||||
}
|
||||
BindIdValue bindValue = redisCache.getCacheObject(BIND_REDIS_KEY + bindId);
|
||||
if (bindValue == null) {
|
||||
return ajax;
|
||||
}
|
||||
ajax.put("bindAccount", true);
|
||||
return AjaxResult.success(bindId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult getErrorMsg(String errorId) {
|
||||
String errorMsg = redisCache.getCacheObject(LOGIN_ERROR_MSG_REDIS_KEY + errorId);
|
||||
if (StringUtils.isEmpty(errorMsg)) {
|
||||
return error(NO_MESSAGE_ALERT, "");
|
||||
} else {
|
||||
return error(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult socialLogin(String loginId) {
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
String loginKey = LOGIN_SOCIAL_REDIS_KEY + loginId;
|
||||
LoginIdValue loginIdValue = redisCache.getCacheObject(loginKey);
|
||||
if (loginIdValue != null) {
|
||||
//login
|
||||
String token = sysLoginService.redirectLogin(loginIdValue.getUsername(), loginIdValue.getPassword());
|
||||
ajax.put(Constants.TOKEN, token);
|
||||
} else {
|
||||
log.info("loginId:{} ", loginId);
|
||||
return error(NO_MESSAGE_ALERT, "数据错误");
|
||||
}
|
||||
return ajax;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult bindLogin(BindLoginBody bindLoginBody) {
|
||||
BindIdValue bindValue = redisCache.getCacheObject(BIND_REDIS_KEY + bindLoginBody.getBindId());
|
||||
SocialUser socialUser = findSocialUser(bindValue.getUuid(), bindValue.getSource());
|
||||
AjaxResult checkAjax = checkSocialUser(socialUser, bindLoginBody.getBindId());
|
||||
if (checkAjax != null) {
|
||||
return checkAjax;
|
||||
}
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
// 生成令牌
|
||||
String token = sysLoginService.login(bindLoginBody.getUsername(), bindLoginBody.getPassword(), bindLoginBody.getCode(),
|
||||
bindLoginBody.getUuid());
|
||||
LoginUser loginUser = tokenService.getLoginUserByToken(token);
|
||||
//绑定和更新
|
||||
SocialUser updateSocialUser = new SocialUser();
|
||||
updateSocialUser.setSysUserId(loginUser.getUserId());
|
||||
updateSocialUser.setSocialUserId(socialUser.getSocialUserId());
|
||||
iSocialUserService.updateSocialUser(updateSocialUser);
|
||||
ajax.put(Constants.TOKEN, token);
|
||||
redisCache.deleteObject(BIND_REDIS_KEY + bindLoginBody.getBindId());
|
||||
return ajax;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult bindRegister(BindRegisterBody bindRegisterBody) {
|
||||
if (!("true".equals(iSysConfigService.selectConfigByKey("sys.account.registerUser")))) {
|
||||
return error("当前系统没有开启注册功能!");
|
||||
}
|
||||
BindIdValue bindValue = redisCache.getCacheObject(BIND_REDIS_KEY + bindRegisterBody.getBindId());
|
||||
SocialUser socialUser = findSocialUser(bindValue.getUuid(), bindValue.getSource());
|
||||
AjaxResult checkAjax = checkSocialUser(socialUser, bindRegisterBody.getBindId());
|
||||
if (checkAjax != null) {
|
||||
return checkAjax;
|
||||
}
|
||||
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
String msg = sysRegisterService.register(bindRegisterBody);
|
||||
if (StringUtils.isEmpty(msg)) {
|
||||
SysUser sysUser = iSysUserService.selectUserByUserName(bindRegisterBody.getUsername());
|
||||
//绑定和更新
|
||||
SocialUser updateSocialUser = new SocialUser();
|
||||
updateSocialUser.setSysUserId(sysUser.getUserId());
|
||||
updateSocialUser.setSocialUserId(socialUser.getSocialUserId());
|
||||
iSocialUserService.updateSocialUser(updateSocialUser);
|
||||
redisCache.deleteObject(BIND_REDIS_KEY + bindRegisterBody.getBindId());
|
||||
}
|
||||
return StringUtils.isEmpty(msg) ? ajax : error(msg);
|
||||
}
|
||||
|
||||
private void checkSocialPlatform(SocialPlatform socialPlatform) {
|
||||
if (socialPlatform != null && (!socialPlatform.getStatus().equals(ONLINE_STATUS) || !socialPlatform.getDelFlag().equals(DEL_FLAG))) {
|
||||
throw new AuthException("当前第三方登录平台被禁用");
|
||||
}
|
||||
}
|
||||
|
||||
public SocialUser findSocialUser(String uuid, String source) {
|
||||
SocialUser socialUser = new SocialUser();
|
||||
socialUser.setSource(source);
|
||||
socialUser.setUuid(uuid);
|
||||
List<SocialUser> socialUserList = iSocialUserService.selectSocialUserList(socialUser);
|
||||
return socialUserList == null || socialUserList.isEmpty() ? null : socialUserList.get(0);
|
||||
|
||||
}
|
||||
|
||||
public void createOrUpdateSocialUser(SocialUser socialUser, AuthUser authUser) {
|
||||
if (socialUser != null) {
|
||||
//更新数据
|
||||
SocialUser updateSocialUser = new SocialUser();
|
||||
updateSocialUser.setSocialUserId(socialUser.getSocialUserId());
|
||||
replaceSocialUser(updateSocialUser, authUser);
|
||||
updateSocialUser.setUpdateBy("System");
|
||||
updateSocialUser.setUpdateTime(DateUtils.getNowDate());
|
||||
iSocialUserService.updateSocialUser(updateSocialUser);
|
||||
} else {
|
||||
//创建
|
||||
SocialUser saveSocialUser = new SocialUser();
|
||||
replaceSocialUser(saveSocialUser, authUser);
|
||||
saveSocialUser.setDelFlag("0");
|
||||
saveSocialUser.setStatus("0");
|
||||
saveSocialUser.setCreateBy("System");
|
||||
saveSocialUser.setCreateTime(DateUtils.getNowDate());
|
||||
iSocialUserService.insertSocialUser(saveSocialUser);
|
||||
}
|
||||
}
|
||||
|
||||
private void replaceSocialUser(SocialUser socialUser, AuthUser authUser) {
|
||||
|
||||
socialUser.setUuid(authUser.getUuid());
|
||||
socialUser.setSource(authUser.getSource());
|
||||
socialUser.setAccessToken(authUser.getToken().getAccessToken());
|
||||
//nullable
|
||||
socialUser.setExpireIn((long) authUser.getToken().getExpireIn());
|
||||
socialUser.setRefreshToken(authUser.getToken().getRefreshToken());
|
||||
socialUser.setOpenId(authUser.getToken().getOpenId());
|
||||
socialUser.setUid(authUser.getToken().getUid());
|
||||
socialUser.setAccessCode(authUser.getToken().getAccessCode());
|
||||
socialUser.setUnionId(authUser.getToken().getUnionId());
|
||||
socialUser.setCode(authUser.getToken().getCode());
|
||||
socialUser.setAvatar(authUser.getAvatar());
|
||||
socialUser.setUsername(authUser.getUsername());
|
||||
socialUser.setNickname(authUser.getNickname());
|
||||
}
|
||||
|
||||
private String genBindId(AuthUser authUser) {
|
||||
String bindId = Md5Utils.hash(authUser.getUuid() + authUser.getSource());
|
||||
String key = BIND_REDIS_KEY + bindId;
|
||||
BindIdValue bindIdValue = new BindIdValue();
|
||||
bindIdValue.setSource(authUser.getSource());
|
||||
bindIdValue.setUuid(authUser.getUuid());
|
||||
redisCache.setCacheObject(key, bindIdValue, BIND_EXPIRE_TIME, TimeUnit.SECONDS);
|
||||
return bindId;
|
||||
}
|
||||
|
||||
private String genLoginId(SysUser sysUser) {
|
||||
String loginId = Md5Utils.hash(sysUser.getUserId() + RandomUtil.randomString(32));
|
||||
String key = LOGIN_SOCIAL_REDIS_KEY + loginId;
|
||||
LoginIdValue loginIdValue = new LoginIdValue();
|
||||
loginIdValue.setPassword(sysUser.getPassword());
|
||||
loginIdValue.setUsername(sysUser.getUserName());
|
||||
redisCache.setCacheObject(key, loginIdValue, LOGIN_SOCIAL_EXPIRE_TIME, TimeUnit.SECONDS);
|
||||
return loginId;
|
||||
}
|
||||
|
||||
private String genErrorId(String msg) {
|
||||
String errorId = Md5Utils.hash(msg + RandomUtil.randomString(32));
|
||||
String key = LOGIN_ERROR_MSG_REDIS_KEY + errorId;
|
||||
redisCache.setCacheObject(key, msg, LOGIN_SOCIAL_EXPIRE_TIME, TimeUnit.SECONDS);
|
||||
return errorId;
|
||||
}
|
||||
|
||||
private AjaxResult checkSocialUser(SocialUser socialUser, String bindId) {
|
||||
if (socialUser == null) {
|
||||
log.info("bindId不存在, bindId: {}", bindId);
|
||||
return error("绑定账户不存在");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.ruoyi.iot.service.impl;
|
||||
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.iot.domain.SocialPlatform;
|
||||
import com.ruoyi.iot.mapper.SocialPlatformMapper;
|
||||
import com.ruoyi.iot.service.ISocialPlatformService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 第三方登录平台控制Service业务层处理
|
||||
*
|
||||
* @author kerwincui
|
||||
* @date 2022-04-11
|
||||
*/
|
||||
@Service
|
||||
public class SocialPlatformServiceImpl implements ISocialPlatformService
|
||||
{
|
||||
@Autowired
|
||||
private SocialPlatformMapper socialPlatformMapper;
|
||||
|
||||
/**
|
||||
* 查询第三方登录平台控制
|
||||
*
|
||||
* @param socialPlatformId 第三方登录平台控制主键
|
||||
* @return 第三方登录平台控制
|
||||
*/
|
||||
@Override
|
||||
public SocialPlatform selectSocialPlatformBySocialPlatformId(Long socialPlatformId)
|
||||
{
|
||||
return socialPlatformMapper.selectSocialPlatformBySocialPlatformId(socialPlatformId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SocialPlatform selectSocialPlatformByPlatform(String platform) {
|
||||
return socialPlatformMapper.selectSocialPlatformByPlatform(platform);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询第三方登录平台控制列表
|
||||
*
|
||||
* @param socialPlatform 第三方登录平台控制
|
||||
* @return 第三方登录平台控制
|
||||
*/
|
||||
@Override
|
||||
public List<SocialPlatform> selectSocialPlatformList(SocialPlatform socialPlatform)
|
||||
{
|
||||
return socialPlatformMapper.selectSocialPlatformList(socialPlatform);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增第三方登录平台控制
|
||||
*
|
||||
* @param socialPlatform 第三方登录平台控制
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertSocialPlatform(SocialPlatform socialPlatform)
|
||||
{
|
||||
socialPlatform.setCreateTime(DateUtils.getNowDate());
|
||||
socialPlatform.setDelFlag("0");
|
||||
return socialPlatformMapper.insertSocialPlatform(socialPlatform);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改第三方登录平台控制
|
||||
*
|
||||
* @param socialPlatform 第三方登录平台控制
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateSocialPlatform(SocialPlatform socialPlatform)
|
||||
{
|
||||
socialPlatform.setUpdateTime(DateUtils.getNowDate());
|
||||
return socialPlatformMapper.updateSocialPlatform(socialPlatform);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除第三方登录平台控制
|
||||
*
|
||||
* @param socialPlatformIds 需要删除的第三方登录平台控制主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteSocialPlatformBySocialPlatformIds(Long[] socialPlatformIds)
|
||||
{
|
||||
return socialPlatformMapper.deleteSocialPlatformBySocialPlatformIds(socialPlatformIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除第三方登录平台控制信息
|
||||
*
|
||||
* @param socialPlatformId 第三方登录平台控制主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteSocialPlatformBySocialPlatformId(Long socialPlatformId)
|
||||
{
|
||||
return socialPlatformMapper.deleteSocialPlatformBySocialPlatformId(socialPlatformId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.ruoyi.iot.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.iot.mapper.SocialUserMapper;
|
||||
import com.ruoyi.iot.domain.SocialUser;
|
||||
import com.ruoyi.iot.service.ISocialUserService;
|
||||
|
||||
/**
|
||||
* 用户第三方用户信息Service业务层处理
|
||||
*
|
||||
* @author json
|
||||
* @date 2022-04-18
|
||||
*/
|
||||
@Service
|
||||
public class SocialUserServiceImpl implements ISocialUserService
|
||||
{
|
||||
@Autowired
|
||||
private SocialUserMapper socialUserMapper;
|
||||
|
||||
/**
|
||||
* 查询用户第三方用户信息
|
||||
*
|
||||
* @param socialUserId 用户第三方用户信息主键
|
||||
* @return 用户第三方用户信息
|
||||
*/
|
||||
@Override
|
||||
public SocialUser selectSocialUserBySocialUserId(Long socialUserId)
|
||||
{
|
||||
return socialUserMapper.selectSocialUserBySocialUserId(socialUserId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户第三方用户信息列表
|
||||
*
|
||||
* @param socialUser 用户第三方用户信息
|
||||
* @return 用户第三方用户信息
|
||||
*/
|
||||
@Override
|
||||
public List<SocialUser> selectSocialUserList(SocialUser socialUser)
|
||||
{
|
||||
return socialUserMapper.selectSocialUserList(socialUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增用户第三方用户信息
|
||||
*
|
||||
* @param socialUser 用户第三方用户信息
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int insertSocialUser(SocialUser socialUser)
|
||||
{
|
||||
socialUser.setCreateTime(DateUtils.getNowDate());
|
||||
return socialUserMapper.insertSocialUser(socialUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改用户第三方用户信息
|
||||
*
|
||||
* @param socialUser 用户第三方用户信息
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int updateSocialUser(SocialUser socialUser)
|
||||
{
|
||||
socialUser.setUpdateTime(DateUtils.getNowDate());
|
||||
return socialUserMapper.updateSocialUser(socialUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除用户第三方用户信息
|
||||
*
|
||||
* @param socialUserIds 需要删除的用户第三方用户信息主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteSocialUserBySocialUserIds(Long[] socialUserIds)
|
||||
{
|
||||
return socialUserMapper.deleteSocialUserBySocialUserIds(socialUserIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户第三方用户信息信息
|
||||
*
|
||||
* @param socialUserId 用户第三方用户信息主键
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteSocialUserBySocialUserId(Long socialUserId)
|
||||
{
|
||||
return socialUserMapper.deleteSocialUserBySocialUserId(socialUserId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user