feat:
1. add AppRegisterTokenGranter
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package org.springblade.modules.app.controller;
|
||||
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||
import io.swagger.v3.oas.annotations.Hidden;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springblade.core.tool.api.R;
|
||||
import org.springblade.modules.system.service.IUserService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Hidden
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/app-user")
|
||||
@Tag(name = "APP用户接口", description = "APP用户接口")
|
||||
public class APPUserController {
|
||||
|
||||
private final IUserService userService;
|
||||
|
||||
|
||||
/**
|
||||
* 是否修改过密码
|
||||
*/
|
||||
@GetMapping("/has-changed")
|
||||
@ApiOperationSupport(order = 1)
|
||||
@Operation(summary = "是否修改过密码")
|
||||
public R<Boolean> hasChangePassword() {
|
||||
return R.data(userService.hasChangePassword());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,12 +26,14 @@
|
||||
package org.springblade.modules.auth.granter;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
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.granter.PasswordTokenGranter;
|
||||
import org.springblade.core.oauth2.handler.PasswordHandler;
|
||||
import org.springblade.core.oauth2.props.OAuth2Properties;
|
||||
import org.springblade.core.oauth2.provider.OAuth2Request;
|
||||
@@ -41,6 +43,7 @@ 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.secure.utils.AuthUtil;
|
||||
import org.springblade.core.tool.utils.*;
|
||||
import org.springblade.modules.auth.provider.UserType;
|
||||
import org.springblade.modules.auth.utils.CodeUtil;
|
||||
@@ -62,16 +65,14 @@ import static org.springblade.modules.auth.constant.BladeAuthConstant.*;
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AppRegisterTokenGranter extends AbstractTokenGranter {
|
||||
public class AppRegisterTokenGranter extends PasswordTokenGranter {
|
||||
|
||||
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) {
|
||||
public AppRegisterTokenGranter(OAuth2ClientService clientService, OAuth2UserService userService, PasswordHandler passwordHandler, IUserService service, AliyunDynpsService dynpsService) {
|
||||
super(clientService, userService, passwordHandler);
|
||||
this.service = service;
|
||||
this.properties = properties;
|
||||
this.dynpsService = dynpsService;
|
||||
}
|
||||
|
||||
@@ -92,20 +93,20 @@ public class AppRegisterTokenGranter extends AbstractTokenGranter {
|
||||
if (!simpleSMSOpen) {
|
||||
throw new UserInvalidException("注册功能暂未开启,请联系管理员");
|
||||
}
|
||||
|
||||
String userType = request.getUserType();
|
||||
if (StringUtil.isBlank(userType) || UserType.of(request.getUserType()).getCategory() != 2) {
|
||||
throw new UserInvalidException(ExceptionCode.INVALID_USER.getMessage());
|
||||
}
|
||||
// 用户注册信息
|
||||
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());
|
||||
}
|
||||
user.setName(StringUtil.random(10));
|
||||
user.setPassword(DigestUtil.encrypt(ParamCache.getValue(INIT_PASSWD_VALUE)));
|
||||
user.setRoleId("1123598816738675202");
|
||||
user.setDeptId("2018922617095507970");
|
||||
user.setPostId("1123598817738675208");
|
||||
|
||||
String code = request.getCode();
|
||||
Assert.isTrue(code.contains(StringPool.COMMA), ExceptionCode.INVALID_USER.getMessage());
|
||||
@@ -132,50 +133,21 @@ public class AppRegisterTokenGranter extends AbstractTokenGranter {
|
||||
if (invitor != null) {
|
||||
throw new UserInvalidException("无效邀请码");
|
||||
}
|
||||
|
||||
//todo: 邀请人
|
||||
}
|
||||
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)) {
|
||||
if (service.registerAppUser(user)) {
|
||||
request.setUsername(user.getAccount());
|
||||
// 构建oauth2所需用户信息
|
||||
return convertOAuth2UserDetail(user, client(request));
|
||||
return super.user(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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,6 +232,7 @@ public interface IUserService extends BaseService<User> {
|
||||
*/
|
||||
boolean registerUser(User user);
|
||||
|
||||
boolean registerAppUser(User user);
|
||||
/**
|
||||
* 配置用户平台
|
||||
*
|
||||
@@ -298,4 +299,7 @@ public interface IUserService extends BaseService<User> {
|
||||
* @return
|
||||
*/
|
||||
List<UserVO> leaderList(String tenantId, String realName);
|
||||
|
||||
Boolean hasChangePassword();
|
||||
|
||||
}
|
||||
|
||||
@@ -468,6 +468,21 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
|
||||
return this.submit(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean registerAppUser(User user) {
|
||||
Tenant tenant = SysCache.getTenant(user.getTenantId());
|
||||
if (tenant == null || tenant.getId() == null) {
|
||||
throw new OAuth2Exception("租户信息错误!");
|
||||
}
|
||||
user.setRealName(user.getName());
|
||||
user.setRoleId("1123598816738675202");
|
||||
user.setDeptId("2018922617095507970");
|
||||
user.setPostId("1123598817738675208");
|
||||
user.setStatus(StatusType.ACTIVE.getType());
|
||||
return this.submit(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePlatform(Long userId, Integer userType, String userExt) {
|
||||
if (userType.equals(UserType.WEB.getCategory())) {
|
||||
@@ -598,4 +613,9 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
|
||||
return UserWrapper.build().listVO(this.list(queryWrapper));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean hasChangePassword() {
|
||||
User user = getById(AuthUtil.getUserId());
|
||||
return user.getCreateTime() == user.getUpdateTime();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,11 +176,11 @@ blade:
|
||||
#token配置
|
||||
token:
|
||||
#是否有状态
|
||||
state: false
|
||||
state: true
|
||||
#是否单用户登录
|
||||
single: false
|
||||
single: true
|
||||
#单用户登录范围
|
||||
single-level: all
|
||||
single-level: client
|
||||
#token签名 使用 @org.springblade.test.SignKeyGenerator 获取
|
||||
sign-key: IALpfRv4h0oTVL1fTfQbu9eAHDDyqTJR
|
||||
#token加密 使用 @org.springblade.test.CryptoKeyGenerator 获取
|
||||
@@ -285,7 +285,7 @@ blade:
|
||||
|
||||
- client-id: app
|
||||
path-patterns:
|
||||
- /formal-app/customer/**
|
||||
- /app-user/**
|
||||
- /formal-app/mall/**
|
||||
- /formal-app/order/**
|
||||
|
||||
|
||||
Reference in New Issue
Block a user