feat: OAuth update

This commit is contained in:
landaiqing
2024-05-29 22:40:38 +08:00
parent 1618807b80
commit 5ed7942c1d
10 changed files with 134 additions and 89 deletions

View File

@@ -8,13 +8,13 @@ import com.schisandra.auth.common.entity.Result;
import com.schisandra.auth.common.redis.RedisUtil;
import com.schisandra.auth.domain.bo.SchisandraAuthUserBO;
import com.schisandra.auth.domain.service.SchisandraAuthUserDomainService;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Objects;
/**
* @Classname SchisandraAuthUserController
@@ -34,6 +34,8 @@ public class SchisandraAuthUserController {
@Resource
private SchisandraAuthUserDomainService schisandraAuthUserDomainService;
private final String AUTH_PHONE_PREFIX = "auth.phone";
/**
* @description: 注册
* @param: [schisandraAuthUserDTO]
@@ -46,23 +48,35 @@ public class SchisandraAuthUserController {
if (log.isInfoEnabled()) {
log.info("UserController.register.dto:{}", JSON.toJSONString(schisandraAuthUserDTO));
}
if (redisUtil.exist("auth.phone." + schisandraAuthUserDTO.getPhone())) {
if (redisUtil.get("auth.phone." + schisandraAuthUserDTO.getPhone()) != schisandraAuthUserDTO.getActiveCode()) {
if (schisandraAuthUserDTO.getPhone() == null) {
log.error("UserController.register.phone is null");
return null;
}
SchisandraAuthUser schisandraAuthUser = schisandraAuthUserDomainService.queryByPhone(schisandraAuthUserDTO.getPhone());
if (ObjectUtils.isNotEmpty(schisandraAuthUser)) {
return Result.fail("该手机号已注册");
} else {
String key = redisUtil.buildKey(AUTH_PHONE_PREFIX, schisandraAuthUserDTO.getPhone());
if (redisUtil.exist(key)) {
if (!Objects.equals(redisUtil.get(key), schisandraAuthUserDTO.getActiveCode())) {
return Result.fail("验证码错误,请重新验证");
}
try {
SchisandraAuthUserBO authUserBO = SchisandraAuthUserDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthUserDTO);
if (schisandraAuthUserDomainService.register(authUserBO)) {
return Result.ok("注册成功");
} else {
return Result.fail("注册失败");
}
} catch (Exception e) {
log.error("UserController.register.error:{}", e.getMessage(), e);
return Result.fail("注册用户失败");
}
} else {
return Result.fail("验证码错误,请重新验证");
}
} else {
return Result.fail("验证码错误,请重新验证");
}
try {
SchisandraAuthUserBO authUserBO = SchisandraAuthUserDTOConverter.INSTANCE.convertDTOToBO(schisandraAuthUserDTO);
if (schisandraAuthUserDomainService.register(authUserBO)) {
return Result.fail("注册用户成功");
} else {
return Result.fail("注册用户失败");
}
} catch (Exception e) {
return Result.fail("注册用户失败");
}
}
@PostMapping("login")
@@ -74,5 +88,23 @@ public class SchisandraAuthUserController {
return Result.ok(s);
}
/**
* @description: 通过手机号判断用户是否注册过
* @param: [phone]
* @return: com.schisandra.auth.common.entity.Result<java.lang.String>
* @author: landaiqing
* @date: 2024/5/29 21:42
*/
@PostMapping("isRegisterByPhone/{phone}")
public Result<String> sendCode(@PathVariable("phone") String phone) {
log.info("UserController.isRegisterByPhone.phone:{}", phone);
Preconditions.checkNotNull(phone, "手机号不能为空");
SchisandraAuthUser schisandraAuthUser = schisandraAuthUserDomainService.queryByPhone(phone);
if (ObjectUtils.isEmpty(schisandraAuthUser)) {
return null;
}
return Result.fail("该手机号已注册,请直接登录");
}
}

View File

@@ -7,10 +7,9 @@ import com.schisandra.auth.common.redis.RedisUtil;
import com.schisandra.auth.common.utils.SmsCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.comm.utils.SmsUtils;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -23,34 +22,36 @@ public class SchisandraSmsController {
@Resource
private RedisUtil redisUtil;
private final String authPhonePrefix="auth.phone";
private final String smsConfigPrefix="sys.config.sms";
/**
* @description: 发送短信验证码
* @param: [phone]
* @return: com.schisandra.auth.common.bo.Result
* @author zlg
* @date: 2024/5/8 22:53
*/
@GetMapping("/sendByTemplate")
public Result sendByTemplate(String phone) {
String prefix = redisUtil.buildKey(authPhonePrefix, phone);
String code = SmsCodeUtils.generateValidateCode(4).toString();
if (!redisUtil.exist(prefix)){
String key = redisUtil.buildKey(smsConfigPrefix, SmsConfigContext.SMS_CONFIG_KEY);
private final String AUTH_PHONE_PREFIX = "auth.phone";
private final String SMS_CONFIG_PREFIX = "sys.config";
/**
* @description: 发送短信验证码
* @param: [phone]
* @return: com.schisandra.auth.common.bo.Result
* @author zlg
* @date: 2024/5/8 22:53
*/
@PostMapping("/sendByTemplate/{phone}")
public Result sendByTemplate(@PathVariable("phone") String phone) {
String prefix = redisUtil.buildKey(AUTH_PHONE_PREFIX, phone);
String code = SmsUtils.getRandomInt(4);
if (!redisUtil.exist(prefix)) {
String key = redisUtil.buildKey(SMS_CONFIG_PREFIX, SmsConfigContext.SMS_CONFIG_KEY);
String configId = redisUtil.get(key);
if (configId == null){
if (configId == null) {
log.error("短信配置不存在!");
Result.fail("短信接口异常!");
}
SmsResponse smsResponse=SmsFactory.getSmsBlend(configId).sendMessage(phone,code);
if (smsResponse.isSuccess()){
redisUtil.setNx(prefix, code, 60L* 5,SECONDS);
return Result.ok("短信发送成功5 分钟内有效!");
}else {
SmsResponse smsResponse = SmsFactory.getSmsBlend(configId).sendMessage(phone, code);
if (smsResponse.isSuccess()) {
redisUtil.setNx(prefix, code, 60L * 5, SECONDS);
return Result.ok("短信发送成功5 分钟内有效,请注意查收");
} else {
return Result.fail("短信发送失败,请重试!");
}
}else {
return Result.fail("已发送,请注意查看!");
} else {
return Result.fail("短信已发送,请注意查看或稍后重试");
}
}

View File

@@ -6,7 +6,7 @@ import java.io.Serializable;
import java.util.Date;
/**
* dto
* dto
*
* @author landaiqing
* @since 2024-05-23 20:00:28
@@ -15,57 +15,57 @@ import java.util.Date;
public class SchisandraAuthUserDTO implements Serializable {
/**
*
*
*/
private Long id;
/**
*
*
*/
private String userName;
/**
*
*
*/
private String nickName;
/**
*
*
*/
private String email;
/**
*
*
*/
private String phone;
/**
*
*
*/
private String password;
/**
*
*
*/
private String gender;
/**
*
*
*/
private String avatar;
/**
*
*
*/
private Integer status;
/**
*
*
*/
private String introduce;
/**
*
*
*/
private String extJson;
@@ -90,26 +90,28 @@ public class SchisandraAuthUserDTO implements Serializable {
private Date updateTime;
/**
*
*
*/
private Integer isDeleted;
/**
*
*
*/
private String blog;
/**
*
*
*/
private String location;
/**
*
*
*/
private String company;
private String activeCode;
private String confirmPassword;
}

View File

@@ -49,6 +49,7 @@ public class AlibabaSmsHandler implements SchisandraSmsTypeHandler{
alibabaConfig.setTemplateId(schisandraSmsConfigDTO.getTemplateId());
alibabaConfig.setTemplateName(schisandraSmsConfigDTO.getTemplateName());
alibabaConfig.setSdkAppId(schisandraSmsConfigDTO.getSdkAppId());
alibabaConfig.setMaximum(Math.toIntExact(schisandraSmsConfigDTO.getMaximum()));
return alibabaConfig;
}
}

View File

@@ -56,6 +56,8 @@ public class TencentSmsHandler implements SchisandraSmsTypeHandler{
tencentConfig.setMaxRetries(schisandraSmsConfigDTO.getMaxRetries());
tencentConfig.setMaximum(Math.toIntExact(schisandraSmsConfigDTO.getMaximum()));
tencentConfig.setWeight(schisandraSmsConfigDTO.getWeight());
tencentConfig.setMaximum(Math.toIntExact(schisandraSmsConfigDTO.getMaximum()));
tencentConfig.setMaxRetries(Math.toIntExact(schisandraSmsConfigDTO.getMaxRetries()));
return tencentConfig;
}
}

View File

@@ -5,15 +5,15 @@ import lombok.Getter;
@Getter
public enum UserRoleEnum {
normal_user(1,"普通用户"),
admin(2,"管理员"),
super_admin(3,"超级管理员");
NORMAL_USER(1L,"普通用户"),
ADMIN(2L,"管理员"),
SUPER_ADMIN(3L,"超级管理员");
public int code;
public Long code;
public String desc;
UserRoleEnum(int code, String desc){
UserRoleEnum(Long code, String desc){
this.code = code;
this.desc = desc;
}

View File

@@ -3,6 +3,7 @@ package com.schisandra.auth.domain.service;
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;
/**
@@ -73,5 +74,7 @@ public interface SchisandraAuthUserDomainService {
* @author msz
*/
Object deleteById(Long id);
SchisandraAuthUser queryByPhone(String phone);
}

View File

@@ -63,11 +63,6 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
@Resource
private SchisandraAuthSocialUserMapperService schisandraAuthSocialUserMapperService;
@Resource
private RedisUtil redisUtil;
private static final String OAUTH_KEY_PREFIX = "oauth.user";
/**
* @description: 注册用户
@@ -78,21 +73,18 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
*/
@Override
public Boolean register(SchisandraAuthUserBO schisandraAuthUserBO) {
SchisandraAuthUser schisandraAuthUser = schisandraAuthUserService.queryByPhone(schisandraAuthUserBO.getPhone());
if (schisandraAuthUser != null) {
return false;
SchisandraAuthUser authUser = SchisandraAuthUserBOConverter.INSTANCE.convertBOToEntity(schisandraAuthUserBO);
if (schisandraAuthUserService.insert(authUser)) {
SchisandraAuthUserRoleBO schisandraAuthUserRoleBO = new SchisandraAuthUserRoleBO();
schisandraAuthUserRoleBO.setUserId(authUser.getId());
schisandraAuthUserRoleBO.setRoleId(UserRoleEnum.NORMAL_USER.getCode());
SchisandraAuthUserRole schisandraAuthUserRole = SchisandraAuthUserRoleBOConverter.INSTANCE.convertBOToEntity(schisandraAuthUserRoleBO);
return schisandraAuthUserRoleService.insert(schisandraAuthUserRole) > 1;
} else {
SchisandraAuthUser schisandraAuthUser1 = SchisandraAuthUserBOConverter.INSTANCE.convertBOToEntity(schisandraAuthUserBO);
if (schisandraAuthUserService.insert(schisandraAuthUser1)) {
SchisandraAuthUserRoleBO schisandraAuthUserRoleBO = new SchisandraAuthUserRoleBO();
schisandraAuthUserRoleBO.setUserId(schisandraAuthUserService.queryByPhone(schisandraAuthUserBO.getPhone()).getId());
schisandraAuthUserRoleBO.setRoleId(1L);
return schisandraAuthUserRoleService.insert(SchisandraAuthUserRoleBOConverter.INSTANCE.convertBOToEntity(schisandraAuthUserRoleBO)) > 1;
} else {
return false;
}
return false;
}
}
@Override
@@ -155,6 +147,18 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
return flag;
}
/**
* @description: 根据手机号查询用户信息
* @param: [phone]
* @return: java.lang.Boolean
* @author: landaiqing
* @date: 2024/5/29 21:41
*/
@Override
public SchisandraAuthUser queryByPhone(String phone) {
return schisandraAuthUserService.queryByPhone(phone);
}
/**
* @description: 根据第三方用户信息添加用户信息
* @param: [data, type]
@@ -275,7 +279,7 @@ public class SchisandraAuthUserDomainServiceImpl implements SchisandraAuthUserDo
// 建立用户与角色映射关系
SchisandraAuthUserRoleBO schisandraAuthUserRoleBO = new SchisandraAuthUserRoleBO();
schisandraAuthUserRoleBO.setUserId(authUserId);
schisandraAuthUserRoleBO.setRoleId((long) UserRoleEnum.normal_user.getCode());
schisandraAuthUserRoleBO.setRoleId((long) UserRoleEnum.NORMAL_USER.getCode());
schisandraAuthUserRoleBO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
SchisandraAuthUserRole schisandraAuthUserRole = SchisandraAuthUserRoleBOConverter.INSTANCE.convertBOToEntity(schisandraAuthUserRoleBO);
int insert = schisandraAuthUserRoleService.insert(schisandraAuthUserRole);

View File

@@ -1,6 +1,7 @@
package com.schisandra.auth.infra.basic.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.schisandra.auth.infra.basic.dao.SchisandraAuthUserDao;
import com.schisandra.auth.infra.basic.entity.SchisandraAuthUser;
@@ -24,7 +25,7 @@ public class SchisandraAuthUserServiceImpl implements SchisandraAuthUserService
@Override
public SchisandraAuthUser queryByPhone(String phone) {
return null;
return this.schisandraAuthUserDao.selectOne(new QueryWrapper<SchisandraAuthUser>().eq("phone", phone).eq("is_deleted", 0));
}
/**
@@ -47,7 +48,7 @@ public class SchisandraAuthUserServiceImpl implements SchisandraAuthUserService
@Override
public Boolean insert(SchisandraAuthUser schisandraAuthUser) {
return this.schisandraAuthUserDao.insert(schisandraAuthUser)>0;
return this.schisandraAuthUserDao.insert(schisandraAuthUser) > 0;
}
/**
@@ -100,15 +101,14 @@ public class SchisandraAuthUserServiceImpl implements SchisandraAuthUserService
.eq(Objects.nonNull(schisandraAuthUser.getIsDeleted()), SchisandraAuthUser::getIsDeleted, schisandraAuthUser.getIsDeleted())
.eq(Objects.nonNull(schisandraAuthUser.getBlog()), SchisandraAuthUser::getBlog, schisandraAuthUser.getBlog())
.eq(Objects.nonNull(schisandraAuthUser.getLocation()), SchisandraAuthUser::getLocation, schisandraAuthUser.getLocation())
.eq(Objects.nonNull(schisandraAuthUser.getCompany()), SchisandraAuthUser::getCompany, schisandraAuthUser.getCompany())
;
.eq(Objects.nonNull(schisandraAuthUser.getCompany()), SchisandraAuthUser::getCompany, schisandraAuthUser.getCompany());
return schisandraAuthUserDao.selectOne(queryWrapper);
}
@Override
public int insertAuthUserByOauth(SchisandraAuthUser schisandraAuthUser) {
return schisandraAuthUserDao.insert(schisandraAuthUser);
return schisandraAuthUserDao.insert(schisandraAuthUser);
}
}

View File

@@ -87,7 +87,7 @@ mybatis-plus:
# 前端地址
web:
url: http://127.0.0.1:5173/
url: http://127.0.0.1:8080/
feign:
client: