feat: OAuth update
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package com.schisandra.auth.application.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.SaTokenInfo;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.schisandra.auth.application.factory.OauthTypeHandlerFactory;
|
||||
import com.schisandra.auth.application.handler.oauth.SchisandraOauthTypeHandler;
|
||||
import com.schisandra.auth.common.entity.Result;
|
||||
import com.schisandra.auth.common.redis.RedisUtil;
|
||||
import com.schisandra.auth.domain.service.SchisandraAuthUserDomainService;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* ClassName:SchisandraOauthController
|
||||
@@ -40,9 +41,15 @@ public class SchisandraOauthController {
|
||||
@Resource
|
||||
private SchisandraAuthUserDomainService schisandraAuthUserDomainService;
|
||||
|
||||
@Resource
|
||||
RedisUtil redisUtil;
|
||||
|
||||
@Value("${web.url}")
|
||||
private String url;
|
||||
|
||||
private static final String OAUTH_KEY_PREFIX = "oauth.user";
|
||||
|
||||
|
||||
/**
|
||||
* @description: 获取授权链接并跳转到第三方授权页面
|
||||
* @param: []
|
||||
@@ -78,7 +85,22 @@ public class SchisandraOauthController {
|
||||
AuthResponse<AuthUser> authResponse = authRequest.login(callback);
|
||||
AuthUser data = authResponse.getData();
|
||||
schisandraAuthUserDomainService.insertAuthUserByOauth(data, type);
|
||||
response.sendRedirect(url + "splash?token=" + StpUtil.getTokenValue());
|
||||
response.sendRedirect(url + "loading");
|
||||
}
|
||||
|
||||
@GetMapping("userInfo")
|
||||
public Result getUserInfo() {
|
||||
String tokenValue = StpUtil.getTokenValue();
|
||||
String key = redisUtil.buildKey(OAUTH_KEY_PREFIX, tokenValue);
|
||||
if (redisUtil.exist(key)) {
|
||||
String userId = redisUtil.get(key);
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("userId", userId);
|
||||
map.put("token", tokenValue);
|
||||
return Result.ok(map);
|
||||
}
|
||||
return Result.fail("用户信息过期或回去失败!");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@@ -17,35 +17,35 @@ public enum OauthType {
|
||||
/**
|
||||
* 微信登录
|
||||
*/
|
||||
WECHAT("wechat"),
|
||||
WECHAT("WECHAT"),
|
||||
/**
|
||||
* QQ登录
|
||||
*/
|
||||
QQ("qq"),
|
||||
QQ("QQ"),
|
||||
/**
|
||||
* 微博登录
|
||||
*/
|
||||
WEIBO("weibo"),
|
||||
WEIBO("WEIBO"),
|
||||
/**
|
||||
* 支付宝登录
|
||||
*/
|
||||
ALIPAY("alipay"),
|
||||
ALIPAY("ALIPAY"),
|
||||
/**
|
||||
* 钉钉登录
|
||||
*/
|
||||
DINGTALK("dingtalk"),
|
||||
DINGTALK("DINGTALK"),
|
||||
/**
|
||||
* 企业微信登录
|
||||
*/
|
||||
WORK_WEIXIN("work_weixin"),
|
||||
WORK_WEIXIN("WORK_WEIXIN"),
|
||||
/**
|
||||
* Github
|
||||
*/
|
||||
GitHUB("gitHub"),
|
||||
GitHUB("GITHUB"),
|
||||
/**
|
||||
* Gitee
|
||||
*/
|
||||
Gitee("gitee");
|
||||
Gitee("GITEE");
|
||||
|
||||
public String type;
|
||||
|
||||
|
@@ -8,6 +8,7 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.schisandra.auth.common.enums.IsDeletedFlagEnum;
|
||||
import com.schisandra.auth.common.enums.UserRoleEnum;
|
||||
import com.schisandra.auth.common.enums.UserStatusEnum;
|
||||
import com.schisandra.auth.common.redis.RedisUtil;
|
||||
import com.schisandra.auth.domain.bo.SchisandraAuthSocialUserBO;
|
||||
import com.schisandra.auth.domain.bo.SchisandraAuthSocialUserMapperBO;
|
||||
import com.schisandra.auth.domain.bo.SchisandraAuthUserBO;
|
||||
@@ -29,11 +30,16 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import org.apache.commons.codec.digest.Md5Crypt;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
/**
|
||||
* 领域service实现了
|
||||
*
|
||||
@@ -56,6 +62,11 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
@Resource
|
||||
private SchisandraAuthSocialUserMapperService schisandraAuthSocialUserMapperService;
|
||||
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
private static final String OAUTH_KEY_PREFIX = "oauth.user";
|
||||
|
||||
|
||||
/**
|
||||
* @description: 注册用户
|
||||
@@ -155,8 +166,9 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
public void insertAuthUserByOauth(AuthUser data, String type) {
|
||||
String uuid = data.getUuid();
|
||||
AuthToken token = data.getToken();
|
||||
int count = schisandraAuthSocialUserService.selectByUuidAndType(uuid, type);
|
||||
if (count <= 0) {
|
||||
SchisandraAuthSocialUser authSocialUser = schisandraAuthSocialUserService.selectByUuidAndType(uuid, type);
|
||||
Long socialUserId = authSocialUser.getId();
|
||||
if (ObjectUtils.isEmpty(authSocialUser)) {
|
||||
SchisandraAuthUserBO schisandraAuthUserBO = new SchisandraAuthUserBO();
|
||||
schisandraAuthUserBO.setAvatar(data.getAvatar());
|
||||
schisandraAuthUserBO.setBlog(data.getBlog());
|
||||
@@ -233,6 +245,8 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
return;
|
||||
}
|
||||
StpUtil.login(data.getUuid(), SaLoginConfig.setToken(token.getAccessToken()));
|
||||
String key = redisUtil.buildKey(OAUTH_KEY_PREFIX, token.getAccessToken()+"."+token.getUid());
|
||||
redisUtil.setNx(key, String.valueOf(authUserId), 60L * 5, SECONDS);
|
||||
}
|
||||
SchisandraAuthSocialUserBO schisandraAuthSocialUserBO = new SchisandraAuthSocialUserBO();
|
||||
schisandraAuthSocialUserBO.setAccessToken(token.getAccessToken());
|
||||
@@ -248,9 +262,7 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
schisandraAuthSocialUserBO.setOauthToken(token.getOauthToken());
|
||||
schisandraAuthSocialUserBO.setOauthTokenSecret(token.getOauthTokenSecret());
|
||||
schisandraAuthSocialUserBO.setRefreshToken(token.getRefreshToken());
|
||||
schisandraAuthSocialUserBO.setScope(token.getScope());
|
||||
schisandraAuthSocialUserBO.setTokenType(token.getTokenType());
|
||||
schisandraAuthSocialUserBO.setUnionId(token.getUnionId());
|
||||
schisandraAuthSocialUserBO.setId(socialUserId);
|
||||
SchisandraAuthSocialUser schisandraAuthSocialUser = SchisandraAuthSocialUserBOConverter.INSTANCE.convertBOToEntity(schisandraAuthSocialUserBO);
|
||||
int update = schisandraAuthSocialUserService.updateByOauth(schisandraAuthSocialUser);
|
||||
if (update <= 0) {
|
||||
@@ -259,7 +271,14 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
}
|
||||
return;
|
||||
}
|
||||
Long socialId = schisandraAuthSocialUser.getId();
|
||||
SchisandraAuthSocialUserMapper schisandraAuthSocialUserMapper = new SchisandraAuthSocialUserMapper();
|
||||
schisandraAuthSocialUserMapper.setSocialUserId(socialId);
|
||||
SchisandraAuthSocialUserMapper result = schisandraAuthSocialUserMapperService.queryByCondition(schisandraAuthSocialUserMapper);
|
||||
Long userId = result.getUserId();
|
||||
StpUtil.login(data.getUuid(), SaLoginConfig.setToken(token.getAccessToken()));
|
||||
String key = redisUtil.buildKey(OAUTH_KEY_PREFIX, token.getAccessToken()+"."+token.getUid());
|
||||
redisUtil.setNx(key, String.valueOf(userId), 60L * 5, SECONDS);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@ public interface SchisandraAuthSocialUserService {
|
||||
*/
|
||||
SchisandraAuthSocialUser queryByCondition(SchisandraAuthSocialUser schisandraAuthSocialUser);
|
||||
|
||||
int selectByUuidAndType(String uuid,String type);
|
||||
SchisandraAuthSocialUser selectByUuidAndType(String uuid,String type);
|
||||
|
||||
int insertAuthSocialUserByOauth(SchisandraAuthSocialUser schisandraAuthSocialUser);
|
||||
|
||||
|
@@ -108,8 +108,8 @@ public class SchisandraAuthSocialUserServiceImpl implements SchisandraAuthSocial
|
||||
}
|
||||
|
||||
@Override
|
||||
public int selectByUuidAndType(String uuid, String type) {
|
||||
return schisandraAuthSocialUserDao.selectCount(new QueryWrapper<SchisandraAuthSocialUser>()
|
||||
public SchisandraAuthSocialUser selectByUuidAndType(String uuid, String type) {
|
||||
return schisandraAuthSocialUserDao.selectOne(new QueryWrapper<SchisandraAuthSocialUser>()
|
||||
.eq("uuid", uuid).eq("source", type).eq("is_deleted", 0));
|
||||
}
|
||||
|
||||
@@ -121,8 +121,7 @@ public class SchisandraAuthSocialUserServiceImpl implements SchisandraAuthSocial
|
||||
@Override
|
||||
public int updateByOauth(SchisandraAuthSocialUser schisandraAuthSocialUser) {
|
||||
return schisandraAuthSocialUserDao.update(schisandraAuthSocialUser, new UpdateWrapper<SchisandraAuthSocialUser>()
|
||||
.eq("access_token", schisandraAuthSocialUser.getAccessToken())
|
||||
.eq("refresh_token", schisandraAuthSocialUser.getRefreshToken()));
|
||||
.eq("id", schisandraAuthSocialUser.getId()).eq("is_deleted", 0));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user