feat: 微信公众号扫码登录完成
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package com.schisandra.auth.api;
|
||||
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@FeignClient("schisandra-cloud-storage-auth")
|
||||
public interface SchisandraAuthFeignService {
|
||||
|
||||
@PostMapping("/auth/user/wechatRegister")
|
||||
Boolean wechatRegister(@RequestParam(name = "appId") String appId, @RequestParam(name = "openId") String openId, @RequestParam("clientId") String clientId);
|
||||
}
|
||||
|
@@ -3,11 +3,9 @@ package com.schisandra.auth.application.config;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.schisandra.auth.application.interceptor.LoginInterceptor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
|
||||
import java.util.List;
|
||||
@@ -15,7 +13,7 @@ import java.util.List;
|
||||
/**
|
||||
* @Classname GlobalConfig
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.auth.application.config
|
||||
* @BelongsPackage: com.schisandra.auth.application.common
|
||||
* @Author: schisandra
|
||||
* @CreateTime: 2024-02-16 15:57
|
||||
* @Description: MVC全局处理
|
||||
|
@@ -29,7 +29,7 @@ public class SmsInitConfig implements SmartLifecycle {
|
||||
@Resource
|
||||
RedisUtil redisUtil;
|
||||
|
||||
private final String smsConfigPrefix="sys.config";
|
||||
private final String smsConfigPrefix="sys.common";
|
||||
|
||||
public void init() {
|
||||
ConfigInfo configInfo = configRpc.getConfigInfo(SmsConfigContext.SMS_CONFIG_KEY);
|
||||
|
@@ -11,7 +11,7 @@ import java.util.List;
|
||||
/**
|
||||
* @Classname SmsConfig
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.auth.common.config
|
||||
* @BelongsPackage: com.schisandra.auth.common.common
|
||||
* @Author: schisandra
|
||||
* @CreateTime: 2024-05-08 18:46
|
||||
* @Description: 短信配置加载类
|
||||
|
@@ -1,7 +1,9 @@
|
||||
package com.schisandra.auth.application.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.SaTokenInfo;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.gson.Gson;
|
||||
import com.schisandra.auth.application.convert.SchisandraAuthRolePermissionDTOConverter;
|
||||
@@ -12,9 +14,9 @@ import com.schisandra.auth.application.utils.CheckRouteCaptcha;
|
||||
import com.schisandra.auth.common.entity.CaptchaResult;
|
||||
import com.schisandra.auth.common.entity.Result;
|
||||
import com.schisandra.auth.common.enums.UserRoleEnum;
|
||||
import com.schisandra.auth.domain.redis.RedisUtil;
|
||||
import com.schisandra.auth.domain.bo.SchisandraAuthRolePermissionBO;
|
||||
import com.schisandra.auth.domain.bo.SchisandraAuthUserBO;
|
||||
import com.schisandra.auth.domain.redis.RedisUtil;
|
||||
import com.schisandra.auth.domain.service.SchisandraAuthPermissionDomainService;
|
||||
import com.schisandra.auth.domain.service.SchisandraAuthRoleDomainService;
|
||||
import com.schisandra.auth.domain.service.SchisandraAuthRolePermissionDomainService;
|
||||
@@ -22,15 +24,16 @@ import com.schisandra.auth.domain.service.SchisandraAuthUserDomainService;
|
||||
import com.schisandra.auth.infra.basic.entity.SchisandraAuthPermission;
|
||||
import com.schisandra.auth.infra.basic.entity.SchisandraAuthRole;
|
||||
import com.schisandra.auth.infra.basic.entity.SchisandraAuthUser;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -63,12 +66,19 @@ public class SchisandraAuthUserController {
|
||||
|
||||
private final String AUTH_ROLE_PREFIX = "auth.role";
|
||||
|
||||
private final String WX_LOGIN_TEMPORARY_CLIENT_ID = "wechat.client.id";
|
||||
|
||||
private final String WX_LOGIN_TOKEN = "wechat.login.token";
|
||||
|
||||
@Resource
|
||||
private SchisandraAuthRolePermissionDomainService schisandraAuthRolePermissionDomainService;
|
||||
|
||||
@Resource
|
||||
private SchisandraAuthPermissionDomainService schisandraAuthPermissionDomainService;
|
||||
|
||||
@Value("${web.url}")
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* @description: 注册
|
||||
* @param: [schisandraAuthUserDTO]
|
||||
@@ -298,4 +308,69 @@ public class SchisandraAuthUserController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 微信公众号扫码登录
|
||||
* @param: []
|
||||
* @return: com.schisandra.auth.common.entity.Result
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/27 下午2:50
|
||||
*/
|
||||
@SneakyThrows
|
||||
@PostMapping("wechatRegister")
|
||||
public Boolean wechatRegister(@RequestParam(name = "appId") String appId, @RequestParam(name = "openId") String openId, @RequestParam("clientId") String clientId) {
|
||||
Boolean result = schisandraAuthUserDomainService.wechatRegister(appId, openId, clientId);
|
||||
if (result) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 生成客户端id
|
||||
* @param: []
|
||||
* @return: com.schisandra.auth.common.entity.Result<java.lang.String>
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/27 下午3:43
|
||||
*/
|
||||
@GetMapping("createClientId")
|
||||
public Result<String> createClientId() {
|
||||
String clientId = UUID.randomUUID().toString();
|
||||
String key = redisUtil.buildKey(WX_LOGIN_TEMPORARY_CLIENT_ID, clientId);
|
||||
redisUtil.setNx(key, clientId, 5L, TimeUnit.MINUTES);
|
||||
return Result.ok(clientId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 获取客户端id
|
||||
* @param: []
|
||||
* @return: com.schisandra.auth.common.entity.Result<java.lang.String>
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/27 下午3:43
|
||||
*/
|
||||
@PostMapping("getClientId")
|
||||
public Result<String> getClientId(@RequestParam("clientId") String clientId) {
|
||||
String key = redisUtil.buildKey(WX_LOGIN_TEMPORARY_CLIENT_ID, clientId);
|
||||
String result = redisUtil.get(key);
|
||||
return Result.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 获取客户端token
|
||||
* @param: [clientId]
|
||||
* @return: com.schisandra.auth.common.entity.Result<java.lang.String>
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/27 下午5:33
|
||||
*/
|
||||
@PostMapping("getClientToken")
|
||||
public Result getClientToken(String clientId) {
|
||||
String key = redisUtil.buildKey(WX_LOGIN_TOKEN, clientId);
|
||||
String result = redisUtil.get(key);
|
||||
if(StringUtils.isBlank(result)){
|
||||
return Result.fail();
|
||||
}
|
||||
return Result.ok(JSONObject.parseObject(result));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,52 @@
|
||||
package com.schisandra.auth.application.handler.oauth;
|
||||
|
||||
import com.schisandra.auth.common.enums.OauthType;
|
||||
import com.schisandra.auth.common.utils.AuthStateRedisCache;
|
||||
import com.schisandra.auth.infra.entity.OauthConfigInfo;
|
||||
import com.schisandra.auth.infra.rpc.OauthConfigRpc;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.request.AuthOschinaRequest;
|
||||
import me.zhyd.oauth.request.AuthRequest;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Classname OschinaOauthHandler
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.auth.application.handler.oauth
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-06-26 14:21
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class OschinaOauthHandler implements SchisandraOauthTypeHandler {
|
||||
@Resource
|
||||
private OauthConfigRpc oauthConfigRpc;
|
||||
|
||||
@Resource
|
||||
private AuthStateRedisCache stateRedisCache;
|
||||
|
||||
@Override
|
||||
public OauthType getHandlerType() {
|
||||
return OauthType.OSCHINA;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthRequest getAuthRequest(String type) {
|
||||
OauthConfigInfo oschina = oauthConfigRpc.getOauthConfigInfo(type);
|
||||
if (ObjectUtils.isEmpty(oschina)) {
|
||||
log.error("oschina oauth 配置信息获取失败");
|
||||
return null;
|
||||
}
|
||||
return new AuthOschinaRequest(AuthConfig.builder()
|
||||
.clientId(oschina.getClientId())
|
||||
.clientSecret(oschina.getClientSecret())
|
||||
.redirectUri(oschina.getRedirectUri())
|
||||
.build(), stateRedisCache);
|
||||
}
|
||||
}
|
@@ -0,0 +1,52 @@
|
||||
package com.schisandra.auth.application.handler.oauth;
|
||||
|
||||
import com.schisandra.auth.common.enums.OauthType;
|
||||
import com.schisandra.auth.common.utils.AuthStateRedisCache;
|
||||
import com.schisandra.auth.infra.entity.OauthConfigInfo;
|
||||
import com.schisandra.auth.infra.rpc.OauthConfigRpc;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.request.AuthRequest;
|
||||
import me.zhyd.oauth.request.AuthStackOverflowRequest;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Classname OschinaOauthHandler
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.auth.application.handler.oauth
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-06-26 14:21
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class StackOverFlowOauthHandler implements SchisandraOauthTypeHandler {
|
||||
@Resource
|
||||
private OauthConfigRpc oauthConfigRpc;
|
||||
|
||||
@Resource
|
||||
private AuthStateRedisCache stateRedisCache;
|
||||
|
||||
@Override
|
||||
public OauthType getHandlerType() {
|
||||
return OauthType.STACK_OVERFLOW;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthRequest getAuthRequest(String type) {
|
||||
OauthConfigInfo STACK_OVERFLOW = oauthConfigRpc.getOauthConfigInfo(type);
|
||||
if (ObjectUtils.isEmpty(STACK_OVERFLOW)) {
|
||||
log.error("STACK_OVERFLOW oauth 配置信息获取失败");
|
||||
return null;
|
||||
}
|
||||
return new AuthStackOverflowRequest(AuthConfig.builder()
|
||||
.clientId(STACK_OVERFLOW.getClientId())
|
||||
.clientSecret(STACK_OVERFLOW.getClientSecret())
|
||||
.redirectUri(STACK_OVERFLOW.getRedirectUri())
|
||||
.build(), stateRedisCache);
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
package com.schisandra.auth.application.handler.oauth;
|
||||
|
||||
import com.schisandra.auth.common.enums.OauthType;
|
||||
import com.schisandra.auth.common.utils.AuthStateRedisCache;
|
||||
import com.schisandra.auth.infra.entity.OauthConfigInfo;
|
||||
import com.schisandra.auth.infra.rpc.OauthConfigRpc;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.request.AuthOschinaRequest;
|
||||
import me.zhyd.oauth.request.AuthRequest;
|
||||
import me.zhyd.oauth.request.AuthWeChatEnterpriseThirdQrcodeRequest;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @Classname OschinaOauthHandler
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.auth.application.handler.oauth
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-06-26 14:21
|
||||
* @Description: TODO
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class WechatEnterpriseOauthHandler implements SchisandraOauthTypeHandler {
|
||||
@Resource
|
||||
private OauthConfigRpc oauthConfigRpc;
|
||||
|
||||
@Resource
|
||||
private AuthStateRedisCache stateRedisCache;
|
||||
|
||||
@Override
|
||||
public OauthType getHandlerType() {
|
||||
return OauthType.WECHAT_ENTERPRISE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthRequest getAuthRequest(String type) {
|
||||
OauthConfigInfo WECHAT_ENTERPRISE = oauthConfigRpc.getOauthConfigInfo(type);
|
||||
if (ObjectUtils.isEmpty(WECHAT_ENTERPRISE)) {
|
||||
log.error("WECHAT_ENTERPRISE oauth 配置信息获取失败");
|
||||
return null;
|
||||
}
|
||||
return new AuthWeChatEnterpriseThirdQrcodeRequest(AuthConfig.builder()
|
||||
.clientId(WECHAT_ENTERPRISE.getClientId())
|
||||
.clientSecret(WECHAT_ENTERPRISE.getClientSecret())
|
||||
.redirectUri(WECHAT_ENTERPRISE.getRedirectUri())
|
||||
.agentId(WECHAT_ENTERPRISE.getAgentId())
|
||||
.build(), stateRedisCache);
|
||||
}
|
||||
}
|
@@ -7,7 +7,7 @@ import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
|
@@ -1,28 +1,29 @@
|
||||
package com.schisandra.auth.application.interceptor;
|
||||
|
||||
|
||||
import com.schisandra.auth.application.context.LoginContextHolder;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @description: 登录拦截器
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/2 14:24
|
||||
*/
|
||||
public class LoginInterceptor implements HandlerInterceptor {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String userId = request.getHeader("userId");
|
||||
LoginContextHolder.set("userId",userId);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
|
||||
LoginContextHolder.remove();
|
||||
}
|
||||
}
|
||||
//package com.schisandra.auth.application.interceptor;
|
||||
//
|
||||
//
|
||||
//import com.schisandra.auth.application.context.LoginContextHolder;
|
||||
//import jakarta.servlet.http.HttpServletRequest;
|
||||
//import jakarta.servlet.http.HttpServletResponse;
|
||||
//import org.springframework.lang.Nullable;
|
||||
//import org.springframework.web.servlet.HandlerInterceptor;
|
||||
//
|
||||
//
|
||||
//
|
||||
///**
|
||||
// * @description: 登录拦截器
|
||||
// * @author: landaiqing
|
||||
// * @date: 2024/6/2 14:24
|
||||
// */
|
||||
//public class LoginInterceptor implements HandlerInterceptor {
|
||||
// @Override
|
||||
// public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
// String userId = request.getHeader("userId");
|
||||
// LoginContextHolder.set("userId",userId);
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
|
||||
// LoginContextHolder.remove();
|
||||
// }
|
||||
//}
|
||||
|
@@ -37,7 +37,7 @@ public enum OauthType {
|
||||
/**
|
||||
* 企业微信登录
|
||||
*/
|
||||
WORK_WEIXIN("work_weixin"),
|
||||
WECHAT_ENTERPRISE("WECHAT_ENTERPRISE"),
|
||||
/**
|
||||
* Github
|
||||
*/
|
||||
@@ -49,7 +49,16 @@ public enum OauthType {
|
||||
/**
|
||||
* System
|
||||
*/
|
||||
SYSTEM("SYSTEM");
|
||||
SYSTEM("SYSTEM"),
|
||||
/**
|
||||
* 开源中国
|
||||
*/
|
||||
OSCHINA("OSCHINA"),
|
||||
/**
|
||||
* STACK_OVERFLOW
|
||||
*/
|
||||
STACK_OVERFLOW("STACK_OVERFLOW");
|
||||
|
||||
|
||||
public String type;
|
||||
|
||||
|
@@ -5,6 +5,7 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -53,6 +54,18 @@ public class RedisUtil {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
public void setJson(String key, Object value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
public void setJsonNx(String key, Object value, Long time, TimeUnit timeUnit) {
|
||||
redisTemplate.opsForValue().setIfAbsent(key, value, time, timeUnit);
|
||||
}
|
||||
|
||||
public HashMap<String, String> getJson(String key) {
|
||||
return (HashMap<String, String>) redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* set(带过期)
|
||||
*/
|
||||
|
@@ -1,10 +1,12 @@
|
||||
package com.schisandra.auth.domain.service;
|
||||
|
||||
|
||||
import cn.dev33.satoken.stp.SaTokenInfo;
|
||||
import com.schisandra.auth.common.entity.Result;
|
||||
import com.schisandra.auth.domain.bo.SchisandraAuthUserBO;
|
||||
import com.schisandra.auth.infra.basic.entity.SchisandraAuthUser;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* 用户领域service
|
||||
@@ -93,5 +95,7 @@ public interface SchisandraAuthUserDomainService {
|
||||
Object deleteById(Long id);
|
||||
|
||||
SchisandraAuthUser queryByPhone(String phone);
|
||||
|
||||
Boolean wechatRegister(String appId, String openId, String clientId);
|
||||
}
|
||||
|
||||
|
@@ -2,21 +2,19 @@ package com.schisandra.auth.domain.service.impl;
|
||||
|
||||
|
||||
import cn.dev33.satoken.stp.SaLoginConfig;
|
||||
import cn.dev33.satoken.stp.SaTokenInfo;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.gson.Gson;
|
||||
import com.schisandra.auth.common.entity.Result;
|
||||
import com.schisandra.auth.common.enums.IsDeletedFlagEnum;
|
||||
import com.schisandra.auth.common.enums.UserRoleEnum;
|
||||
import com.schisandra.auth.domain.redis.RedisUtil;
|
||||
import com.schisandra.auth.domain.bo.SchisandraAuthUserBO;
|
||||
import com.schisandra.auth.domain.bo.SchisandraAuthUserRoleBO;
|
||||
import com.schisandra.auth.domain.bo.SchisandraSocialUserAuthBO;
|
||||
import com.schisandra.auth.domain.bo.SchisandraSocialUserBO;
|
||||
import com.schisandra.auth.domain.bo.*;
|
||||
import com.schisandra.auth.domain.convert.SchisandraAuthUserBOConverter;
|
||||
import com.schisandra.auth.domain.convert.SchisandraAuthUserRoleBOConverter;
|
||||
import com.schisandra.auth.domain.convert.SchisandraSocialUserAuthBOConverter;
|
||||
import com.schisandra.auth.domain.convert.SchisandraSocialUserBOConverter;
|
||||
import com.schisandra.auth.domain.redis.RedisUtil;
|
||||
import com.schisandra.auth.domain.service.SchisandraAuthUserDomainService;
|
||||
import com.schisandra.auth.infra.basic.entity.*;
|
||||
import com.schisandra.auth.infra.basic.service.*;
|
||||
@@ -30,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -61,6 +60,8 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
|
||||
private final String AUTH_ROLE_PREFIX = "auth.role";
|
||||
|
||||
private final String WX_LOGIN_TOKEN = "wechat.login.token";
|
||||
|
||||
@Resource
|
||||
RedisUtil redisUtil;
|
||||
|
||||
@@ -216,6 +217,88 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
return schisandraAuthUserService.queryByPhone(phone);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 微信注册
|
||||
* @param: []
|
||||
* @return: com.schisandra.auth.common.entity.Result
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/27 下午2:58
|
||||
*/
|
||||
@Override
|
||||
public Boolean wechatRegister(String appId, String openId,String clientId) {
|
||||
SchisandraSocialUser socialUser = schisandraSocialUserService.selectByOpenId(openId);
|
||||
if (ObjectUtils.isNotEmpty(socialUser)) {
|
||||
Long userId = socialUser.getId();
|
||||
// redis存储用户角色与权限信息
|
||||
userInfoPersistence(userId);
|
||||
StpUtil.login(userId);
|
||||
SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
|
||||
String jsonString = JSONObject.toJSONString(tokenInfo);
|
||||
String key = redisUtil.buildKey(WX_LOGIN_TOKEN, clientId);
|
||||
redisUtil.setJsonNx(key, jsonString,5L, TimeUnit.MINUTES);
|
||||
return true;
|
||||
}else{
|
||||
// 插入社会用户信息表
|
||||
SchisandraSocialUserBO socialUserBO = new SchisandraSocialUserBO();
|
||||
socialUserBO.setOpenId(openId);
|
||||
SchisandraSocialUser schisandraSocialUser = SchisandraSocialUserBOConverter.INSTANCE.convertBOToEntity(socialUserBO);
|
||||
int result = schisandraSocialUserService.insertSocialUser(schisandraSocialUser);
|
||||
if (result <= 0) {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.error("wechatRegister.insertSocialUser fail, param:{}", JSONObject.toJSONString(schisandraSocialUser));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// 插入用户信息表
|
||||
SchisandraAuthUserBO authUserBO = new SchisandraAuthUserBO();
|
||||
authUserBO.setUserName(openId);
|
||||
SchisandraAuthUser schisandraAuthUser = SchisandraAuthUserBOConverter.INSTANCE.convertBOToEntity(authUserBO);
|
||||
int insertAuthUser = schisandraAuthUserService.insertAuthUserByOauth(schisandraAuthUser);
|
||||
if (insertAuthUser <= 0) {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.error("wechatRegister.insertAuthUser fail, param:{}", JSONObject.toJSONString(schisandraAuthUser));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Long authUserId = schisandraAuthUser.getId();
|
||||
Long socialUserId = schisandraSocialUser.getId();
|
||||
// 建立社会用户与用户信息映射
|
||||
SchisandraSocialUserAuthBO socialUserAuthBO = new SchisandraSocialUserAuthBO();
|
||||
socialUserAuthBO.setUserId(authUserId);
|
||||
socialUserAuthBO.setSocialUserId(socialUserId);
|
||||
SchisandraSocialUserAuth schisandraSocialUserAuth = SchisandraSocialUserAuthBOConverter.INSTANCE.convertBOToEntity(socialUserAuthBO);
|
||||
int insertMapper = schisandraSocialUserAuthService.insert(schisandraSocialUserAuth);
|
||||
if (insertMapper <= 0) {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.error("wechatRegister.insertAuthUserMapper fail, param:{}", JSONObject.toJSONString(schisandraSocialUserAuth));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// 建立用户与角色映射关系
|
||||
SchisandraAuthUserRoleBO schisandraAuthUserRoleBO = new SchisandraAuthUserRoleBO();
|
||||
schisandraAuthUserRoleBO.setUserId(authUserId);
|
||||
schisandraAuthUserRoleBO.setRoleId(UserRoleEnum.NORMAL_USER.getCode());
|
||||
schisandraAuthUserRoleBO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
|
||||
SchisandraAuthUserRole schisandraAuthUserRole = SchisandraAuthUserRoleBOConverter.INSTANCE.convertBOToEntity(schisandraAuthUserRoleBO);
|
||||
int insert = schisandraAuthUserRoleService.insert(schisandraAuthUserRole);
|
||||
if (insert <= 0) {
|
||||
if (log.isInfoEnabled()) {
|
||||
log.error("wechatRegister.insertUserRole fail, param:{}", JSONObject.toJSONString(schisandraAuthUserRole));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// redis存储用户角色与权限信息
|
||||
userInfoPersistence(authUserId);
|
||||
StpUtil.login(authUserId);
|
||||
SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
|
||||
String jsonString = JSONObject.toJSONString(tokenInfo);
|
||||
String key = redisUtil.buildKey(WX_LOGIN_TOKEN, clientId);
|
||||
redisUtil.setJsonNx(key, jsonString,5L, TimeUnit.MINUTES);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @description: 根据第三方用户信息添加用户
|
||||
@@ -287,7 +370,7 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
|
||||
}
|
||||
|
||||
// 插入用户信息表
|
||||
SchisandraAuthUserBO authUserBO=new SchisandraAuthUserBO();
|
||||
SchisandraAuthUserBO authUserBO = new SchisandraAuthUserBO();
|
||||
authUserBO.setUserName(data.getUsername());
|
||||
authUserBO.setNickName(data.getNickname());
|
||||
authUserBO.setAvatar(data.getAvatar());
|
||||
|
@@ -48,4 +48,6 @@ public interface SchisandraSocialUserService {
|
||||
int updateById(SchisandraSocialUser schisandraSocialUser);
|
||||
|
||||
int insertSocialUser(SchisandraSocialUser schisandraSocialUser);
|
||||
|
||||
SchisandraSocialUser selectByOpenId(String openId);
|
||||
}
|
||||
|
@@ -89,10 +89,29 @@ public class SchisandraSocialUserServiceImpl implements SchisandraSocialUserServ
|
||||
return schisandraSocialUserDao.update(schisandraSocialUser, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 插入社会用户信息
|
||||
* @param: [schisandraSocialUser]
|
||||
* @return: int
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/27 下午3:05
|
||||
*/
|
||||
@Override
|
||||
public int insertSocialUser(SchisandraSocialUser schisandraSocialUser) {
|
||||
return schisandraSocialUserDao.insertSelective(schisandraSocialUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 根据appId和openId查询用户
|
||||
* @param: [appId, openId]
|
||||
* @return: com.schisandra.auth.infra.basic.entity.SchisandraSocialUser
|
||||
* @author: landaiqing
|
||||
* @date: 2024/6/27 下午3:05
|
||||
*/
|
||||
@Override
|
||||
public SchisandraSocialUser selectByOpenId(String openId) {
|
||||
return schisandraSocialUserDao.selectOneByCondition(SchisandraSocialUserTableDef.SCHISANDRA_SOCIAL_USER.OPEN_ID.eq(openId));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
/**
|
||||
* @Classname MyBatisFlexConfiguration
|
||||
* @BelongsProject: schisandra-cloud-storage
|
||||
* @BelongsPackage: com.schisandra.oss.infra.config
|
||||
* @BelongsPackage: com.schisandra.oss.infra.common
|
||||
* @Author: landaiqing
|
||||
* @CreateTime: 2024-05-31 16:14
|
||||
* @Description: Mybatis flex 配置类
|
||||
|
Reference in New Issue
Block a user