|
@@ -1,6 +1,7 @@
|
|
|
package com.ym.mec.auth.core.provider;
|
|
|
|
|
|
import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
@@ -16,9 +17,11 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import com.ym.mec.auth.api.dto.SysUserInfo;
|
|
|
import com.ym.mec.auth.api.entity.LoginEntity;
|
|
|
import com.ym.mec.auth.api.entity.SysUser;
|
|
|
+import com.ym.mec.auth.api.entity.SysUserDevice;
|
|
|
import com.ym.mec.auth.config.token.PhoneAuthenticationToken;
|
|
|
+import com.ym.mec.auth.service.SysUserDeviceService;
|
|
|
import com.ym.mec.auth.service.SysUserService;
|
|
|
-import com.ym.mec.common.security.SecurityConstants;
|
|
|
+import com.ym.mec.common.exception.BizException;
|
|
|
import com.ym.mec.common.service.IdGeneratorService;
|
|
|
|
|
|
public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider {
|
|
@@ -28,7 +31,9 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
|
|
|
private IdGeneratorService smsCodeService;
|
|
|
|
|
|
private SysUserService sysUserService;
|
|
|
-
|
|
|
+
|
|
|
+ private SysUserDeviceService sysUserDeviceService;
|
|
|
+
|
|
|
@Override
|
|
|
protected void additionalAuthenticationChecks(UserDetails userDetails, Authentication authentication) throws AuthenticationException {
|
|
|
|
|
@@ -48,7 +53,7 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
|
|
|
|
|
|
String smsCode = loginEntity.getSmsCode();
|
|
|
|
|
|
- String phone = StringUtils.substringAfter(username, SecurityConstants.PHONE_PRINCIPAL_PREFIX);
|
|
|
+ String phone = loginEntity.getPhone();
|
|
|
|
|
|
// 验证码验证
|
|
|
boolean b = smsCodeService.verifyValidCode(phone, smsCode);
|
|
@@ -59,26 +64,53 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
|
|
|
String clientId = loginEntity.getClientId();
|
|
|
|
|
|
Boolean isRegister = loginEntity.getIsRegister();
|
|
|
+
|
|
|
+ String deviceNum = loginEntity.getDeviceNum();
|
|
|
|
|
|
- SysUserInfo userInfo = sysUserService.queryUserInfoByPhone(loginEntity.getPhone());
|
|
|
+ SysUserInfo userInfo = sysUserService.queryUserInfoByPhone(phone);
|
|
|
|
|
|
if (userInfo == null) {
|
|
|
if (isRegister == false || StringUtils.equals("SYSTEM", clientId)) {
|
|
|
throw new LockedException("用户不存在");
|
|
|
}
|
|
|
- sysUserService.initUser(loginEntity.getPhone(), clientId);
|
|
|
+ if (StringUtils.isNotBlank(deviceNum) && !StringUtils.equals("STUDENT", clientId)) {
|
|
|
+ // 检查设备
|
|
|
+ List<SysUserDevice> sysUserDeviceList = sysUserDeviceService.queryByDeviceNum(deviceNum);
|
|
|
+
|
|
|
+ if (sysUserDeviceList != null && sysUserDeviceList.size() > 0) {
|
|
|
+ throw new BadCredentialsException("当前设备已绑定账号,请更换设备");
|
|
|
+ }
|
|
|
+
|
|
|
+ userInfo = sysUserService.initUser(loginEntity.getPhone(), clientId);
|
|
|
+
|
|
|
+ SysUserDevice sysUserDevice = new SysUserDevice();
|
|
|
+ sysUserDevice.setUserId(userInfo.getSysUser().getId());
|
|
|
+ sysUserDevice.setDeviceNum(deviceNum);
|
|
|
+ sysUserDevice.setBindTime(new Date());
|
|
|
+ sysUserDeviceService.insert(sysUserDevice);
|
|
|
+ } else {
|
|
|
+ userInfo = sysUserService.initUser(loginEntity.getPhone(), clientId);
|
|
|
+ }
|
|
|
} else {
|
|
|
+ SysUser user = userInfo.getSysUser();
|
|
|
+ if (user == null) {
|
|
|
+ throw new LockedException("用户不存在");
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(deviceNum) && !StringUtils.equals("STUDENT", clientId)) {
|
|
|
+ // 检查设备
|
|
|
+ try {
|
|
|
+ sysUserDeviceService.bindDevice(user.getId(), deviceNum);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new BadCredentialsException("当前设备已绑定账号,请更换设备");
|
|
|
+ }
|
|
|
+ }
|
|
|
if (!userInfo.getSysUser().getUserType().contains(clientId)) {
|
|
|
if (isRegister == false || StringUtils.equals("SYSTEM", clientId)) {
|
|
|
throw new LockedException("用户不存在");
|
|
|
} else {
|
|
|
- SysUser user = sysUserService.queryByPhone(phone);
|
|
|
- if(user == null){
|
|
|
- throw new LockedException("用户不存在");
|
|
|
- }
|
|
|
- user.setUserType(user.getUserType()+","+clientId);
|
|
|
+ user.setUserType(user.getUserType() + "," + clientId);
|
|
|
user.setUpdateTime(new Date());
|
|
|
-
|
|
|
+
|
|
|
// 添加userType以及附加信息
|
|
|
if (StringUtils.equals("STUDENT", clientId)) {
|
|
|
user.setOrganId(sysUserService.getLesseeOrganId());
|
|
@@ -130,4 +162,8 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
|
|
|
public void setSmsCodeService(IdGeneratorService smsCodeService) {
|
|
|
this.smsCodeService = smsCodeService;
|
|
|
}
|
|
|
+
|
|
|
+ public void setSysUserDeviceService(SysUserDeviceService sysUserDeviceService) {
|
|
|
+ this.sysUserDeviceService = sysUserDeviceService;
|
|
|
+ }
|
|
|
}
|