remove
This commit is contained in:
@@ -22,6 +22,8 @@ public interface BladeAuthConstant {
|
||||
|
||||
String SMS_SIMPLE_VALUE = "sms.simple";
|
||||
|
||||
String INIT_PASSWD_VALUE = "account.initPassword";
|
||||
|
||||
String INVITATION_CODE = "invitation_code";
|
||||
|
||||
String BINARY_TEMPLATE = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
|
||||
|
||||
@@ -4,10 +4,13 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springblade.common.cache.ParamCache;
|
||||
import org.springblade.core.oauth2.exception.UserInvalidException;
|
||||
import org.springblade.core.oauth2.props.OAuth2Properties;
|
||||
import org.springblade.core.oauth2.provider.OAuth2Request;
|
||||
import org.springblade.core.oauth2.service.OAuth2User;
|
||||
import org.springblade.core.oauth2.service.OAuth2UserService;
|
||||
import org.springblade.core.redis.debounce.RedisDebounce;
|
||||
import org.springblade.core.redis.ratelimiter.RateLimiter;
|
||||
import org.springblade.core.sms.model.SmsCode;
|
||||
import org.springblade.core.sms.model.SmsData;
|
||||
import org.springblade.core.tool.api.R;
|
||||
@@ -23,8 +26,10 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.springblade.core.oauth2.constant.OAuth2TokenConstant.USER_PHONE_NOT_FOUND;
|
||||
import static org.springblade.modules.auth.constant.BladeAuthConstant.REGISTER_USER_VALUE;
|
||||
import static org.springblade.modules.auth.constant.BladeAuthConstant.SMS_SIMPLE_VALUE;
|
||||
import static org.springblade.modules.resource.utils.SmsUtil.*;
|
||||
|
||||
@@ -69,21 +74,11 @@ public class Oauth2SmsEndpoint {
|
||||
if (StringUtil.isBlank(decryptedPhone)) {
|
||||
return R.fail(USER_PHONE_NOT_FOUND);
|
||||
}
|
||||
Boolean simpleSMSOpen = Func.toBoolean(ParamCache.getValue(SMS_SIMPLE_VALUE), false);
|
||||
// 校验手机是否已注册,防止恶意发送验证码
|
||||
OAuth2Request request = OAuth2Request.create();
|
||||
request.setTenantId(tenantId);
|
||||
OAuth2User oAuth2User = userService.loadByPhone(decryptedPhone, request);
|
||||
if (oAuth2User == null && simpleSMSOpen) {
|
||||
try {
|
||||
Boolean sendSms = dynpsService.sendSms(decryptedPhone);
|
||||
return sendSms ? R.success(SEND_SUCCESS) : R.fail(SEND_FAIL);
|
||||
} catch (Exception e) {
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
}else if (oAuth2User != null && simpleSMSOpen){
|
||||
return R.fail("该手机号码注册");
|
||||
} else if (oAuth2User == null) {
|
||||
if (oAuth2User == null) {
|
||||
return R.fail(USER_PHONE_NOT_FOUND);
|
||||
}
|
||||
// 用户存在则发送验证码
|
||||
@@ -92,4 +87,38 @@ public class Oauth2SmsEndpoint {
|
||||
return smsCode.isSuccess() ? R.data(smsCode, SEND_SUCCESS) : R.fail(SEND_FAIL);
|
||||
}
|
||||
|
||||
@RedisDebounce(key = "sms:debounce", param = "#phone", interval = 60, includeRemainingTime = true, message = "短信发送过于频繁")
|
||||
@PostMapping("/oauth/sms/send-register")
|
||||
public R sendRegister(@RequestParam String tenantId, @RequestParam String phone) {
|
||||
// 校验手机加密认证,防止恶意发送验证码
|
||||
String decryptedPhone = SM2Util.decrypt(phone, properties.getPublicKey(), properties.getPrivateKey());
|
||||
if (StringUtil.isBlank(decryptedPhone)) {
|
||||
return R.fail(USER_PHONE_NOT_FOUND);
|
||||
}
|
||||
Boolean simpleSMSOpen = Func.toBoolean(ParamCache.getValue(SMS_SIMPLE_VALUE), false);
|
||||
|
||||
Boolean registerOpen = Func.toBoolean(ParamCache.getValue(REGISTER_USER_VALUE), false);
|
||||
|
||||
// 校验手机是否已注册,防止恶意发送验证码
|
||||
OAuth2Request request = OAuth2Request.create();
|
||||
request.setTenantId(tenantId);
|
||||
OAuth2User oAuth2User = userService.loadByPhone(decryptedPhone, request);
|
||||
if (oAuth2User != null) {
|
||||
return R.fail("该用户已注册");
|
||||
}
|
||||
if (simpleSMSOpen && registerOpen) {
|
||||
try {
|
||||
Boolean sendSms = dynpsService.sendSms(decryptedPhone);
|
||||
return sendSms ? R.success(SEND_SUCCESS) : R.fail(SEND_FAIL);
|
||||
} catch (Exception e) {
|
||||
String msg = e.getMessage();
|
||||
if (e.getMessage().startsWith("您的访问次数")) {
|
||||
msg = "您的访问次数已超限";
|
||||
}
|
||||
return R.fail(msg);
|
||||
}
|
||||
}
|
||||
throw new UserInvalidException("注册功能暂未开启,请联系管理员");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
/**
|
||||
* BladeX Commercial License Agreement
|
||||
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
|
||||
* <p>
|
||||
* Use of this software is governed by the Commercial License Agreement
|
||||
* obtained after purchasing a license from BladeX.
|
||||
* <p>
|
||||
* 1. This software is for development use only under a valid license
|
||||
* from BladeX.
|
||||
* <p>
|
||||
* 2. Redistribution of this software's source code to any third party
|
||||
* without a commercial license is strictly prohibited.
|
||||
* <p>
|
||||
* 3. Licensees may copyright their own code but cannot use segments
|
||||
* from this software for such purposes. Copyright of this software
|
||||
* remains with BladeX.
|
||||
* <p>
|
||||
* Using this software signifies agreement to this License, and the software
|
||||
* must not be used for illegal purposes.
|
||||
* <p>
|
||||
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
|
||||
* not liable for any claims arising from secondary or illegal development.
|
||||
* <p>
|
||||
* Author: Chill Zhuang (bladejava@qq.com)
|
||||
*/
|
||||
package org.springblade.modules.auth.granter;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springblade.common.cache.ParamCache;
|
||||
import org.springblade.core.launch.constant.TokenConstant;
|
||||
import org.springblade.core.oauth2.exception.ExceptionCode;
|
||||
import org.springblade.core.oauth2.exception.UserInvalidException;
|
||||
import org.springblade.core.oauth2.granter.AbstractTokenGranter;
|
||||
import org.springblade.core.oauth2.handler.PasswordHandler;
|
||||
import org.springblade.core.oauth2.props.OAuth2Properties;
|
||||
import org.springblade.core.oauth2.provider.OAuth2Request;
|
||||
import org.springblade.core.oauth2.provider.OAuth2Token;
|
||||
import org.springblade.core.oauth2.service.OAuth2Client;
|
||||
import org.springblade.core.oauth2.service.OAuth2ClientService;
|
||||
import org.springblade.core.oauth2.service.OAuth2User;
|
||||
import org.springblade.core.oauth2.service.OAuth2UserService;
|
||||
import org.springblade.core.oauth2.service.impl.OAuth2UserDetail;
|
||||
import org.springblade.core.tool.utils.*;
|
||||
import org.springblade.modules.auth.provider.UserType;
|
||||
import org.springblade.modules.auth.utils.CodeUtil;
|
||||
import org.springblade.modules.resource.utils.AliyunDynpsService;
|
||||
import org.springblade.modules.system.pojo.entity.User;
|
||||
import org.springblade.modules.system.service.IUserService;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static org.springblade.modules.auth.constant.BladeAuthConstant.*;
|
||||
|
||||
/**
|
||||
* RegisterTokenGranter
|
||||
*
|
||||
* @author BladeX
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AppRegisterTokenGranter extends AbstractTokenGranter {
|
||||
|
||||
private final IUserService service;
|
||||
private final OAuth2Properties properties;
|
||||
private final AliyunDynpsService dynpsService;
|
||||
|
||||
public AppRegisterTokenGranter(OAuth2ClientService clientService, OAuth2UserService userService, PasswordHandler passwordHandler, IUserService service, OAuth2Properties properties, AliyunDynpsService dynpsService) {
|
||||
super(clientService, userService, passwordHandler);
|
||||
this.service = service;
|
||||
this.properties = properties;
|
||||
this.dynpsService = dynpsService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String type() {
|
||||
return "app_register";
|
||||
}
|
||||
|
||||
@Override
|
||||
public OAuth2User user(OAuth2Request request) {
|
||||
// 校验注册功能是否开启
|
||||
Boolean registerOpen = Func.toBoolean(ParamCache.getValue(REGISTER_USER_VALUE), false);
|
||||
if (!registerOpen) {
|
||||
throw new UserInvalidException("注册功能暂未开启,请联系管理员");
|
||||
}
|
||||
|
||||
Boolean simpleSMSOpen = Func.toBoolean(ParamCache.getValue(SMS_SIMPLE_VALUE), false);
|
||||
if (!simpleSMSOpen) {
|
||||
throw new UserInvalidException("注册功能暂未开启,请联系管理员");
|
||||
}
|
||||
|
||||
// 用户注册信息
|
||||
User user = new User();
|
||||
user.setUserType(UserType.of(request.getUserType()).getCategory());
|
||||
user.setTenantId(request.getTenantId());
|
||||
user.setAccount(request.getPhone());
|
||||
user.setName(request.getName());
|
||||
user.setRealName(request.getName());
|
||||
user.setPhone(request.getPhone());
|
||||
user.setEmail(request.getEmail());
|
||||
|
||||
if (UserType.APP.getCategory() != user.getUserType()) {
|
||||
throw new UserInvalidException(ExceptionCode.INVALID_USER.getMessage());
|
||||
}
|
||||
|
||||
String code = request.getCode();
|
||||
Assert.isTrue(code.contains(StringPool.COMMA), ExceptionCode.INVALID_USER.getMessage());
|
||||
String[] splitCode = code.split(StringPool.COMMA);
|
||||
Assert.isTrue(splitCode.length == 2, ExceptionCode.INVALID_USER.getMessage());
|
||||
String smsCode = splitCode[0];
|
||||
String invitationCode = splitCode[1];
|
||||
if (StringUtil.isBlank(smsCode)) {
|
||||
throw new UserInvalidException("无效验证码");
|
||||
}
|
||||
Boolean verified;
|
||||
try {
|
||||
verified = dynpsService.verifySms(request.getPhone(), smsCode);
|
||||
} catch (Exception e) {
|
||||
throw new UserInvalidException(e.getMessage());
|
||||
}
|
||||
if (verified) {
|
||||
if (StringUtil.isNotBlank(invitationCode) && !StringUtil.equals(invitationCode, "0")) {
|
||||
if (!CodeUtil.isValidBase36Nullable(invitationCode)) {
|
||||
throw new UserInvalidException("无效邀请码");
|
||||
}
|
||||
long invitorId = Long.parseLong(invitationCode, 36);
|
||||
User invitor = service.getById(invitorId);
|
||||
if (invitor != null) {
|
||||
throw new UserInvalidException("无效邀请码");
|
||||
}
|
||||
}
|
||||
user.setPassword(DigestUtil.encrypt(ParamCache.getValue(INIT_PASSWD_VALUE)));
|
||||
user.setRoleId("1123598816738675202");
|
||||
user.setDeptId("2018922617095507970");
|
||||
user.setPostId("1123598817738675208");
|
||||
} else {
|
||||
throw new UserInvalidException("验证码验证失败");
|
||||
}
|
||||
|
||||
|
||||
// 执行用户注册
|
||||
if (service.registerUser(user)) {
|
||||
// 构建oauth2所需用户信息
|
||||
return convertOAuth2UserDetail(user, client(request));
|
||||
}
|
||||
throw new UserInvalidException(ExceptionCode.INVALID_USER.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public OAuth2Token token(OAuth2User user, OAuth2Request request) {
|
||||
// 移除注册后返回的令牌与刷新令牌,防止外部攻击采用注册接口获取令牌并调用低权接口
|
||||
// 注意:
|
||||
// 1. 框架已默认开启严格模式,blade.secure.strict-token=true,不移除令牌则不受影响,注册令牌会被框架校验并拒绝
|
||||
// 2. 若自行关闭严格模式,blade.secure.strict-token=false,必须将令牌移除,否则注册获取令牌后可调用低权接口
|
||||
OAuth2Token token = super.token(user, request);
|
||||
token.getArgs().remove(TokenConstant.ACCESS_TOKEN);
|
||||
token.getArgs().remove(TokenConstant.REFRESH_TOKEN);
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
private OAuth2UserDetail convertOAuth2UserDetail(User user, OAuth2Client client) {
|
||||
OAuth2UserDetail userDetail = new OAuth2UserDetail();
|
||||
userDetail.setUserId(String.valueOf(user.getId()));
|
||||
userDetail.setTenantId(user.getTenantId());
|
||||
userDetail.setName(user.getName());
|
||||
userDetail.setRealName(user.getName());
|
||||
userDetail.setAccount(user.getAccount());
|
||||
userDetail.setPassword(user.getPassword());
|
||||
userDetail.setPhone(user.getPhone());
|
||||
userDetail.setEmail(user.getEmail());
|
||||
userDetail.setAuthorities(Collections.singletonList(REGISTER));
|
||||
userDetail.setClient(client);
|
||||
return userDetail;
|
||||
}
|
||||
}
|
||||
@@ -25,10 +25,8 @@
|
||||
*/
|
||||
package org.springblade.modules.auth.granter;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springblade.common.cache.ParamCache;
|
||||
import org.springblade.common.constant.CommonConstant;
|
||||
import org.springblade.core.launch.constant.TokenConstant;
|
||||
import org.springblade.core.oauth2.exception.ExceptionCode;
|
||||
import org.springblade.core.oauth2.exception.UserInvalidException;
|
||||
@@ -42,42 +40,33 @@ import org.springblade.core.oauth2.service.OAuth2ClientService;
|
||||
import org.springblade.core.oauth2.service.OAuth2User;
|
||||
import org.springblade.core.oauth2.service.OAuth2UserService;
|
||||
import org.springblade.core.oauth2.service.impl.OAuth2UserDetail;
|
||||
import org.springblade.core.oauth2.utils.OAuth2Util;
|
||||
import org.springblade.core.secure.TokenInfo;
|
||||
import org.springblade.core.social.props.SocialProperties;
|
||||
import org.springblade.core.tool.utils.*;
|
||||
import org.springblade.core.tool.utils.Func;
|
||||
import org.springblade.core.tool.utils.SM2Util;
|
||||
import org.springblade.modules.auth.provider.UserType;
|
||||
import org.springblade.modules.auth.utils.CodeUtil;
|
||||
import org.springblade.modules.resource.utils.AliyunDynpsService;
|
||||
import org.springblade.modules.system.pojo.entity.User;
|
||||
import org.springblade.modules.system.service.IUserService;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static org.springblade.modules.auth.constant.BladeAuthConstant.REGISTER_USER_VALUE;
|
||||
import static org.springblade.modules.auth.constant.BladeAuthConstant.SMS_SIMPLE_VALUE;
|
||||
|
||||
/**
|
||||
* RegisterTokenGranter
|
||||
*
|
||||
* @author BladeX
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RegisterTokenGranter extends AbstractTokenGranter {
|
||||
|
||||
private final IUserService service;
|
||||
private final OAuth2Properties properties;
|
||||
private final AliyunDynpsService dynpsService;
|
||||
|
||||
public RegisterTokenGranter(OAuth2ClientService clientService, OAuth2UserService userService, PasswordHandler passwordHandler, IUserService service, OAuth2Properties properties, AliyunDynpsService dynpsService) {
|
||||
public RegisterTokenGranter(OAuth2ClientService clientService, OAuth2UserService userService, PasswordHandler passwordHandler, IUserService service, OAuth2Properties properties) {
|
||||
super(clientService, userService, passwordHandler);
|
||||
this.service = service;
|
||||
this.properties = properties;
|
||||
this.dynpsService = dynpsService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -93,59 +82,19 @@ public class RegisterTokenGranter extends AbstractTokenGranter {
|
||||
throw new UserInvalidException("注册功能暂未开启,请联系管理员");
|
||||
}
|
||||
|
||||
Boolean simpleSMSOpen = Func.toBoolean(ParamCache.getValue(SMS_SIMPLE_VALUE), false);
|
||||
// 用户注册信息
|
||||
User user = new User();
|
||||
user.setUserType(UserType.of(request.getUserType()).getCategory());
|
||||
user.setTenantId(request.getTenantId());
|
||||
user.setAccount(request.getPhone());
|
||||
user.setAccount(request.getUsername());
|
||||
user.setPassword(SM2Util.decrypt(request.getPassword(), properties.getPublicKey(), properties.getPrivateKey()));
|
||||
user.setName(request.getName());
|
||||
user.setRealName(request.getName());
|
||||
user.setPhone(request.getPhone());
|
||||
user.setEmail(request.getEmail());
|
||||
|
||||
if (UserType.APP.getCategory() != user.getUserType()) {
|
||||
user.setPassword(SM2Util.decrypt(request.getPassword(), properties.getPublicKey(), properties.getPrivateKey()));
|
||||
// 校验用户格式
|
||||
validateUser(user);
|
||||
} else {
|
||||
if (simpleSMSOpen) {
|
||||
String code = request.getCode();
|
||||
Assert.isTrue(code.contains(StringPool.COMMA), ExceptionCode.INVALID_USER.getMessage());
|
||||
String[] splitCode = code.split(StringPool.COMMA);
|
||||
Assert.isTrue(splitCode.length == 2, ExceptionCode.INVALID_USER.getMessage());
|
||||
String smsCode = splitCode[0];
|
||||
String invitationCode = splitCode[1];
|
||||
if (StringUtil.isBlank(smsCode)) {
|
||||
throw new UserInvalidException("无效验证码");
|
||||
}
|
||||
Boolean verified;
|
||||
try {
|
||||
verified = dynpsService.verifySms(request.getPhone(), smsCode);
|
||||
} catch (Exception e) {
|
||||
throw new UserInvalidException(e.getMessage());
|
||||
}
|
||||
if (verified) {
|
||||
if (StringUtil.isNotBlank(invitationCode) && !StringUtil.equals(invitationCode, "0")) {
|
||||
if (!CodeUtil.isValidBase36Nullable(invitationCode)) {
|
||||
throw new UserInvalidException("无效邀请码");
|
||||
}
|
||||
long invitorId = Long.parseLong(invitationCode, 36);
|
||||
User invitor = service.getById(invitorId);
|
||||
if (invitor != null) {
|
||||
throw new UserInvalidException("无效邀请码");
|
||||
}
|
||||
}
|
||||
user.setPassword(DigestUtil.encrypt(smsCode));
|
||||
user.setRoleId("1123598816738675202");
|
||||
user.setDeptId("2018922617095507970");
|
||||
user.setPostId("1123598817738675208");
|
||||
} else {
|
||||
throw new UserInvalidException("验证码验证失败");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// 校验用户格式
|
||||
validateUser(user);
|
||||
|
||||
// 执行用户注册
|
||||
if (service.registerUser(user)) {
|
||||
|
||||
@@ -11,8 +11,11 @@ import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springblade.core.redis.ratelimiter.RateLimiter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AliyunDynpsService {
|
||||
@@ -47,6 +50,7 @@ public class AliyunDynpsService {
|
||||
}
|
||||
}
|
||||
|
||||
@RateLimiter(value = "sms:limiter", param = "#phone", max = 3, ttl = 12, timeUnit = TimeUnit.HOURS)// 功能分类
|
||||
public Boolean sendSms(String phone) {
|
||||
|
||||
SendSmsVerifyCodeRequest sendSmsVerifyCodeRequest = new SendSmsVerifyCodeRequest()
|
||||
|
||||
@@ -460,7 +460,11 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
|
||||
if (tenant == null || tenant.getId() == null) {
|
||||
throw new OAuth2Exception("租户信息错误!");
|
||||
}
|
||||
user.setStatus(StatusType.ACTIVE.getType());
|
||||
user.setRealName(user.getName());
|
||||
user.setRoleId(StringPool.MINUS_ONE);
|
||||
user.setDeptId(StringPool.MINUS_ONE);
|
||||
user.setPostId(StringPool.MINUS_ONE);
|
||||
user.setStatus(StatusType.IN_ACTIVE.getType());
|
||||
return this.submit(user);
|
||||
}
|
||||
|
||||
|
||||
@@ -212,6 +212,8 @@ blade:
|
||||
#redis序列化方式
|
||||
redis:
|
||||
serializer-type: protostuff
|
||||
rate-limiter:
|
||||
enabled: true
|
||||
#日志配置
|
||||
log:
|
||||
request:
|
||||
|
||||
Reference in New Issue
Block a user