前台用户账号密码登录

This commit is contained in:
墨大叔 2024-07-20 14:58:55 +08:00
parent 608ff95255
commit a44c907099
5 changed files with 72 additions and 55 deletions

View File

@ -177,6 +177,11 @@ public class Constants
*/
public static final String USER_TYPE_WX = "USER_TYPE_WX";
/**
* 登录用户为APP账密登录
*/
public static final String USER_TYPE_APP = "USER_TYPE_APP";
/**
* 登录用户为PC
*/

View File

@ -220,30 +220,24 @@ public class SysLoginService
* @param password
* @return
*/
@Deprecated
public String appLogin(String username, String password) {
// 登录前置校验
loginPreCheck(username, password);
// 用户验证
Authentication authentication = null;
try
{
try {
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
AuthenticationContextHolder.setContext(authenticationToken);
// 把用户登录类型放在上下文中的details属性中在UserDetailsServiceImpl.loadUserByUsername中获取
authenticationToken.setDetails(Constants.USER_TYPE_WX);
authenticationToken.setDetails(Constants.USER_TYPE_APP);
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
authentication = authenticationManager.authenticate(authenticationToken);
}
catch (Exception e)
{
if (e instanceof BadCredentialsException)
{
catch (Exception e) {
if (e instanceof BadCredentialsException) {
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
throw new UserPasswordNotMatchException();
}
else
{
} else {
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
throw new ServiceException(e.getMessage());
}
@ -285,7 +279,7 @@ public class SysLoginService
Authentication authentication = null;
try {
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(), Constants.CUSTOM_LOGIN_WX);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getPhonenumber(), Constants.CUSTOM_LOGIN_WX);
// 用户名和密码等信息保存在一个上下文中只要是同一线程等会就能拿到用户名和密码也就是能在loadUserByUsername(String username)方法中进行密码验证等
AuthenticationContextHolder.setContext(authenticationToken);
// 把用户登录类型放在上下文中的details属性中在UserDetailsServiceImpl.loadUserByUsername中获取

View File

@ -2,6 +2,7 @@ package com.ruoyi.framework.web.service;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SmUser;
import com.ruoyi.common.enums.LoginType;
import com.ruoyi.framework.security.context.AuthenticationContextHolder;
import com.ruoyi.ss.user.service.ISmUserService;
import org.slf4j.Logger;
@ -52,47 +53,24 @@ public class UserDetailsServiceImpl implements UserDetailsService
if (authentication != null && authentication.getDetails() != null) {
String userType = (String) authentication.getDetails();
if(Constants.USER_TYPE_PC.equals(userType)){
// PC用户登录
// PC 用户登录
SysUser user = userService.selectUserByUserName(username);
if (StringUtils.isNull(user))
{
log.info("登录用户:{} 不存在.", username);
throw new ServiceException(MessageUtils.message("user.not.exists"));
}
else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
log.info("登录用户:{} 已被删除.", username);
throw new ServiceException(MessageUtils.message("user.password.delete"));
}
else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
log.info("登录用户:{} 已被停用.", username);
throw new ServiceException(MessageUtils.message("user.blocked"));
}
this.checkUser(user, username);
passwordService.validate(user);
return createLoginUser(user);
} else if(Constants.USER_TYPE_WX.equals(userType)) {
// app 用户微信登录
SmUser user = smUserService.selectUserByPhone(username);
this.checkUser(user, username);
return createLoginUser(user);
} else if(Constants.USER_TYPE_APP.equals(userType)) {
// app 用户手机号密码登录
SmUser user = smUserService.selectUserByPhone(username);
this.checkUser(user, username);
passwordService.validate(user);
return createLoginUser(user);
} else {
// app用户登录
SmUser user = smUserService.selectUserByUserName(username);
if (StringUtils.isNull(user))
{
log.info("登录用户:{} 不存在.", username);
throw new ServiceException(MessageUtils.message("user.not.exists"));
}
else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
log.info("登录用户:{} 已被删除.", username);
throw new ServiceException(MessageUtils.message("user.password.delete"));
}
else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
log.info("登录用户:{} 已被停用.", username);
throw new ServiceException(MessageUtils.message("user.blocked"));
}
return createLoginUser(user);
throw new ServiceException("不支持的登录方式");
}
} else {
@ -101,6 +79,48 @@ public class UserDetailsServiceImpl implements UserDetailsService
}
}
/**
* 校验用户账号是否正常
*/
private void checkUser(SysUser user, String username) {
if (StringUtils.isNull(user))
{
log.info("登录用户:{} 不存在.", username);
throw new ServiceException(MessageUtils.message("user.not.exists"));
}
else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
log.info("登录用户:{} 已被删除.", username);
throw new ServiceException(MessageUtils.message("user.password.delete"));
}
else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
log.info("登录用户:{} 已被停用.", username);
throw new ServiceException(MessageUtils.message("user.blocked"));
}
}
/**
* 校验用户账号是否正常
*/
private void checkUser(SmUser user, String username) {
if (StringUtils.isNull(user))
{
log.info("登录用户:{} 不存在.", username);
throw new ServiceException(MessageUtils.message("user.not.exists"));
}
else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
log.info("登录用户:{} 已被删除.", username);
throw new ServiceException(MessageUtils.message("user.password.delete"));
}
else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
log.info("登录用户:{} 已被停用.", username);
throw new ServiceException(MessageUtils.message("user.blocked"));
}
}
public UserDetails createLoginUser(SysUser user)
{
return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));

View File

@ -310,13 +310,11 @@ public class SmUserServiceImpl implements ISmUserService
ServiceUtil.assertion(user == null, "用户不存在");
}
// 判断是否重复注册
// 判断手机号是否重复
SmUserVo repeatPhone = selectUserByPhone(data.getPhonenumber());
ServiceUtil.assertion(repeatPhone != null && !Objects.equals(repeatPhone.getUserId(), data.getUserId()), "用户手机号重复");
SmUserVo repeatIdCard = selectUserByIdCard(data.getIdentityCard());
ServiceUtil.assertion(repeatIdCard != null && !Objects.equals(repeatIdCard.getUserId(), data.getUserId()), "用户身份证号重复");
// 判断微信openId是否重复
SmUserVo repeatWxOpenId = selectUserByWxOpenId(data.getWxOpenId());
ServiceUtil.assertion(repeatWxOpenId != null && repeatWxOpenId.getWxOpenId() != null &&
!Objects.equals(repeatWxOpenId.getUserId(), data.getUserId()), "用户微信openId重复");

View File

@ -20,9 +20,9 @@ wx:
# apiV3密钥
apiV3Key: 49819e0f0abdb2df3246f7b27f264d75
# 通知回调地址
notifyUrl: https://kg.chuantewulian.cn/prod-api/app/pay/notify/wx # 正式环境
notifyUrl: https://kg.chuangtewl.com/prod-api/app/pay/notify/wx # 正式环境
# 退款通知回调地址
refundNotifyUrl: https://kg.chuantewulian.cn/prod-api/app/pay/notify/wx/refund
refundNotifyUrl: https://kg.chuangtewl.com/prod-api/app/pay/notify/wx/refund
# 密钥所在位置
privateKeyPath: /www/wwwroot/smart-switch/wxpay/apiclient_key.pem
# 证书序列号