|
@@ -29,66 +29,65 @@ import com.ym.mec.common.service.IdGeneratorService;
|
|
|
|
|
|
public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider {
|
|
|
|
|
|
- private UserDetailsService userDetailsService;
|
|
|
+ private UserDetailsService userDetailsService;
|
|
|
|
|
|
- private IdGeneratorService smsCodeService;
|
|
|
+ private IdGeneratorService smsCodeService;
|
|
|
|
|
|
- private SysUserService sysUserService;
|
|
|
-
|
|
|
- private SysUserDeviceService sysUserDeviceService;
|
|
|
+ private SysUserService sysUserService;
|
|
|
|
|
|
- private RedisCache<String,Object> redisCache;
|
|
|
+ private SysUserDeviceService sysUserDeviceService;
|
|
|
|
|
|
- @Override
|
|
|
- protected void additionalAuthenticationChecks(UserDetails userDetails, Authentication authentication) throws AuthenticationException {
|
|
|
+ private RedisCache<String,Object> redisCache;
|
|
|
|
|
|
- if (authentication.getCredentials() == null) {
|
|
|
- throw new BadCredentialsException(this.messages.getMessage("PhoneAuthenticationProvider.badCredentials", "Bad credentials"));
|
|
|
- }
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ protected void additionalAuthenticationChecks(UserDetails userDetails, Authentication authentication) throws AuthenticationException {
|
|
|
|
|
|
- @Override
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public UserDetails retrieveUser(String username, Authentication authentication) throws AuthenticationException {
|
|
|
- LoginEntity loginEntity = (LoginEntity) authentication.getCredentials();
|
|
|
- if (loginEntity == null) {
|
|
|
- throw new BadCredentialsException("Bad credentials");
|
|
|
- }
|
|
|
- String smsCode = loginEntity.getSmsCode();
|
|
|
- String phone = loginEntity.getPhone();
|
|
|
- String clientId = loginEntity.getClientId();
|
|
|
+ if (authentication.getCredentials() == null) {
|
|
|
+ throw new BadCredentialsException(this.messages.getMessage("PhoneAuthenticationProvider.badCredentials", "Bad credentials"));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 验证码验证
|
|
|
- if (!clientId.startsWith("QR_") && !smsCodeService.verifyValidCode(phone, smsCode)) {
|
|
|
- throw new BadCredentialsException("验证码校验失败");
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ protected UserDetails retrieveUser(String username, Authentication authentication) throws AuthenticationException {
|
|
|
+ LoginEntity loginEntity = (LoginEntity) authentication.getCredentials();
|
|
|
+ if (loginEntity == null) {
|
|
|
+ throw new BadCredentialsException("Bad credentials");
|
|
|
+ }
|
|
|
+ String smsCode = loginEntity.getSmsCode();
|
|
|
+ String phone = loginEntity.getPhone();
|
|
|
+ String clientId = loginEntity.getClientId();
|
|
|
|
|
|
+ // 验证码验证
|
|
|
+ if (!clientId.startsWith("QR_") && !smsCodeService.verifyValidCode(phone, smsCode)) {
|
|
|
+ throw new BadCredentialsException("验证码校验失败");
|
|
|
+ }
|
|
|
|
|
|
- Boolean isRegister = loginEntity.getIsRegister();
|
|
|
-
|
|
|
- String deviceNum = loginEntity.getDeviceNum();
|
|
|
|
|
|
+ Boolean isRegister = loginEntity.getIsRegister();
|
|
|
|
|
|
+ String deviceNum = loginEntity.getDeviceNum();
|
|
|
|
|
|
- SysUserInfo userInfo;
|
|
|
|
|
|
- if (clientId.startsWith("QR_")) {
|
|
|
- Object data = redisCache.get(loginEntity.getPhone());
|
|
|
- if (data == null) {
|
|
|
- throw new LockedException("用户不存在");
|
|
|
- } else {
|
|
|
- redisCache.delete(loginEntity.getPhone());
|
|
|
- QRLoginDto loginDto = (QRLoginDto) data;
|
|
|
- if (loginDto.getPrivateKey().equals(loginEntity.getSmsCode())) {
|
|
|
- userInfo = loginDto.getUserInfo();
|
|
|
- username = username.replaceAll(loginDto.getCode(),userInfo.getSysUser().getPhone());
|
|
|
- } else {
|
|
|
- throw new LockedException("用户不存在");
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- userInfo = sysUserService.queryUserInfoByPhone(phone);
|
|
|
- }
|
|
|
+ SysUserInfo userInfo;
|
|
|
+
|
|
|
+ if (clientId.startsWith("QR_")) {
|
|
|
+ Object data = redisCache.get(loginEntity.getPhone());
|
|
|
+ if (data == null) {
|
|
|
+ throw new LockedException("用户不存在");
|
|
|
+ } else {
|
|
|
+ redisCache.delete(loginEntity.getPhone());
|
|
|
+ QRLoginDto loginDto = (QRLoginDto) data;
|
|
|
+ if (loginDto.getPrivateKey().equals(loginEntity.getSmsCode())) {
|
|
|
+ userInfo = loginDto.getUserInfo();
|
|
|
+ username = username.replaceAll(loginDto.getCode(),userInfo.getSysUser().getPhone());
|
|
|
+ } else {
|
|
|
+ throw new LockedException("用户不存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ userInfo = sysUserService.queryUserInfoByPhone(phone);
|
|
|
+ }
|
|
|
|
|
|
if (userInfo == null) {
|
|
|
|
|
@@ -100,9 +99,9 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
|
|
|
if (isRegister == false || StringUtils.equals("SYSTEM", clientId)) {
|
|
|
throw new LockedException("用户不存在");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
userInfo = sysUserService.initUser(loginEntity.getTenantId(), loginEntity.getOrganId(), loginEntity.getPhone(), clientId);
|
|
|
-
|
|
|
+
|
|
|
if (StringUtils.isNotBlank(deviceNum)) {
|
|
|
sysUserDeviceService.bindDevice(clientId, userInfo.getSysUser().getId(), deviceNum, userInfo.getSysUser().getTenantId());
|
|
|
}
|
|
@@ -135,75 +134,80 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
|
|
|
// !loginEntity.getTenantId().equals(user.getTenantId())){
|
|
|
// throw new LockedException("用户已注册");
|
|
|
// }
|
|
|
- if (StringUtils.isNotBlank(deviceNum)) {
|
|
|
- sysUserDeviceService.bindDevice(clientId, user.getId(), deviceNum, userInfo.getSysUser().getTenantId());
|
|
|
- }
|
|
|
-
|
|
|
- if (clientId.startsWith("QR_")) {
|
|
|
- } else if (!userInfo.getSysUser().getUserType().contains(clientId)) {
|
|
|
- if (isRegister == false || StringUtils.equals("SYSTEM", clientId)) {
|
|
|
- throw new LockedException("用户不存在");
|
|
|
- } else {
|
|
|
- user.setUserType(user.getUserType() + "," + clientId);
|
|
|
- user.setUpdateTime(new Date());
|
|
|
-
|
|
|
- // 添加userType以及附加信息
|
|
|
- if (StringUtils.equals("STUDENT", clientId)) {
|
|
|
- user.setOrganId(Integer.parseInt(loginEntity.getOrganId()));
|
|
|
- sysUserService.saveStudent(user.getId(),loginEntity.getTenantId());
|
|
|
- } else if (StringUtils.equals("TEACHER", clientId)) {
|
|
|
- sysUserService.saveTeacher(user.getId(),loginEntity.getTenantId());
|
|
|
- }
|
|
|
- sysUserService.update(user);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- UserDetails loadedUser;
|
|
|
- try {
|
|
|
- loadedUser = userDetailsService.loadUserByUsername(username);
|
|
|
- } catch (UsernameNotFoundException e) {
|
|
|
- throw e;
|
|
|
- } catch (Exception e) {
|
|
|
- throw new InternalAuthenticationServiceException(e.getMessage(), e);
|
|
|
- }
|
|
|
-
|
|
|
- if (loadedUser == null) {
|
|
|
- throw new InternalAuthenticationServiceException("账户不存在");
|
|
|
- } else {
|
|
|
- return loadedUser;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) {
|
|
|
- PhoneAuthenticationToken result = new PhoneAuthenticationToken(principal, authentication.getCredentials(), user.getAuthorities());
|
|
|
- result.setDetails(authentication.getDetails());
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean supports(Class<?> aClass) {
|
|
|
- return PhoneAuthenticationToken.class.isAssignableFrom(aClass);
|
|
|
- }
|
|
|
-
|
|
|
- public void setUserDetailsService(UserDetailsService userDetailsService) {
|
|
|
- this.userDetailsService = userDetailsService;
|
|
|
- }
|
|
|
-
|
|
|
- public void setRedisCache(RedisCache<String, Object> redisCache) {
|
|
|
- this.redisCache = redisCache;
|
|
|
- }
|
|
|
-
|
|
|
- public void setSysUserService(SysUserService sysUserService) {
|
|
|
- this.sysUserService = sysUserService;
|
|
|
- }
|
|
|
-
|
|
|
- public void setSmsCodeService(IdGeneratorService smsCodeService) {
|
|
|
- this.smsCodeService = smsCodeService;
|
|
|
- }
|
|
|
-
|
|
|
- public void setSysUserDeviceService(SysUserDeviceService sysUserDeviceService) {
|
|
|
- this.sysUserDeviceService = sysUserDeviceService;
|
|
|
- }
|
|
|
+ if (StringUtils.isNotBlank(deviceNum)) {
|
|
|
+ sysUserDeviceService.bindDevice(clientId, user.getId(), deviceNum, userInfo.getSysUser().getTenantId());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (clientId.startsWith("QR_")) {
|
|
|
+ } else if (!userInfo.getSysUser().getUserType().contains(clientId)) {
|
|
|
+ if (isRegister == false || StringUtils.equals("SYSTEM", clientId)) {
|
|
|
+ throw new LockedException("用户不存在");
|
|
|
+ } else {
|
|
|
+ user.setUserType(user.getUserType() + "," + clientId);
|
|
|
+ user.setUpdateTime(new Date());
|
|
|
+
|
|
|
+ // 添加userType以及附加信息
|
|
|
+ if (StringUtils.equals("STUDENT", clientId)) {
|
|
|
+ if (StringUtils.isBlank(loginEntity.getOrganId())) {
|
|
|
+ user.setOrganId(sysUserService.getLesseeOrganId());
|
|
|
+ loginEntity.setTenantId(1);
|
|
|
+ } else {
|
|
|
+ user.setOrganId(Integer.parseInt(loginEntity.getOrganId()));
|
|
|
+ }
|
|
|
+ sysUserService.saveStudent(user.getId(),loginEntity.getTenantId());
|
|
|
+ } else if (StringUtils.equals("TEACHER", clientId)) {
|
|
|
+ sysUserService.saveTeacher(user.getId(),loginEntity.getTenantId());
|
|
|
+ }
|
|
|
+ sysUserService.update(user);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ UserDetails loadedUser;
|
|
|
+ try {
|
|
|
+ loadedUser = userDetailsService.loadUserByUsername(username);
|
|
|
+ } catch (UsernameNotFoundException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new InternalAuthenticationServiceException(e.getMessage(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (loadedUser == null) {
|
|
|
+ throw new InternalAuthenticationServiceException("账户不存在");
|
|
|
+ } else {
|
|
|
+ return loadedUser;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) {
|
|
|
+ PhoneAuthenticationToken result = new PhoneAuthenticationToken(principal, authentication.getCredentials(), user.getAuthorities());
|
|
|
+ result.setDetails(authentication.getDetails());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean supports(Class<?> aClass) {
|
|
|
+ return PhoneAuthenticationToken.class.isAssignableFrom(aClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUserDetailsService(UserDetailsService userDetailsService) {
|
|
|
+ this.userDetailsService = userDetailsService;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setRedisCache(RedisCache<String, Object> redisCache) {
|
|
|
+ this.redisCache = redisCache;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setSysUserService(SysUserService sysUserService) {
|
|
|
+ this.sysUserService = sysUserService;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setSmsCodeService(IdGeneratorService smsCodeService) {
|
|
|
+ this.smsCodeService = smsCodeService;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setSysUserDeviceService(SysUserDeviceService sysUserDeviceService) {
|
|
|
+ this.sysUserDeviceService = sysUserDeviceService;
|
|
|
+ }
|
|
|
}
|