mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 12:18:42 +08:00 
			
		
		
		
	去除 Spring Security 的 Admin 的 loadUsername,使用自己定义的 login0 实现
This commit is contained in:
		@@ -26,8 +26,6 @@ public interface AuthConvert {
 | 
			
		||||
 | 
			
		||||
    SpringSecurityUser convert2(AdminUserDO user);
 | 
			
		||||
 | 
			
		||||
    LoginUser convert(SpringSecurityUser bean);
 | 
			
		||||
 | 
			
		||||
    default AuthPermissionInfoRespVO convert(AdminUserDO user, List<RoleDO> roleList, List<MenuDO> menuList) {
 | 
			
		||||
        return AuthPermissionInfoRespVO.builder()
 | 
			
		||||
            .user(AuthPermissionInfoRespVO.UserVO.builder().id(user.getId()).nickname(user.getNickname()).avatar(user.getAvatar()).build())
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +1,14 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.service.auth;
 | 
			
		||||
 | 
			
		||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*;
 | 
			
		||||
import cn.iocoder.yudao.framework.security.core.service.SecurityAuthFrameworkService;
 | 
			
		||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*;
 | 
			
		||||
 | 
			
		||||
import javax.validation.Valid;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 管理后台的认证 Service 接口
 | 
			
		||||
 *
 | 
			
		||||
 * 提供用户的账号密码登录、token 的校验等认证相关的功能
 | 
			
		||||
 * 提供用户的登录、登出的能力
 | 
			
		||||
 *
 | 
			
		||||
 * @author 芋道源码
 | 
			
		||||
 */
 | 
			
		||||
@@ -24,6 +24,13 @@ public interface AdminAuthService extends SecurityAuthFrameworkService {
 | 
			
		||||
     */
 | 
			
		||||
    String login(@Valid AuthLoginReqVO reqVO, String userIp, String userAgent);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 基于 token 退出登录
 | 
			
		||||
     *
 | 
			
		||||
     * @param token token
 | 
			
		||||
     */
 | 
			
		||||
    void logout(String token);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 短信验证码发送
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,12 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.service.auth;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.util.ObjectUtil;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.security.core.LoginUser;
 | 
			
		||||
import cn.iocoder.yudao.framework.security.core.authentication.MultiUsernamePasswordAuthenticationToken;
 | 
			
		||||
import cn.iocoder.yudao.framework.security.core.authentication.SpringSecurityUser;
 | 
			
		||||
import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi;
 | 
			
		||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*;
 | 
			
		||||
@@ -19,18 +19,11 @@ import cn.iocoder.yudao.module.system.service.common.CaptchaService;
 | 
			
		||||
import cn.iocoder.yudao.module.system.service.logger.LoginLogService;
 | 
			
		||||
import cn.iocoder.yudao.module.system.service.social.SocialUserService;
 | 
			
		||||
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
 | 
			
		||||
import com.google.common.annotations.VisibleForTesting;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.context.annotation.Lazy;
 | 
			
		||||
import org.springframework.security.authentication.AuthenticationManager;
 | 
			
		||||
import org.springframework.security.authentication.BadCredentialsException;
 | 
			
		||||
import org.springframework.security.authentication.DisabledException;
 | 
			
		||||
import org.springframework.security.core.Authentication;
 | 
			
		||||
import org.springframework.security.core.AuthenticationException;
 | 
			
		||||
import org.springframework.security.core.userdetails.UserDetails;
 | 
			
		||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.util.Assert;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
import javax.validation.Validator;
 | 
			
		||||
@@ -50,11 +43,6 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
 | 
			
		||||
public class AdminAuthServiceImpl implements AdminAuthService {
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    @Lazy // 延迟加载,因为存在相互依赖的问题
 | 
			
		||||
    private AuthenticationManager authenticationManager;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") // UserService 存在重名
 | 
			
		||||
    private AdminUserService userService;
 | 
			
		||||
    @Resource
 | 
			
		||||
    private CaptchaService captchaService;
 | 
			
		||||
@@ -71,17 +59,6 @@ public class AdminAuthServiceImpl implements AdminAuthService {
 | 
			
		||||
    @Resource
 | 
			
		||||
    private SmsCodeApi smsCodeApi;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
 | 
			
		||||
        // 获取 username 对应的 AdminUserDO
 | 
			
		||||
        AdminUserDO user = userService.getUserByUsername(username);
 | 
			
		||||
        if (user == null) {
 | 
			
		||||
            throw new UsernameNotFoundException(username);
 | 
			
		||||
        }
 | 
			
		||||
        // 创建 LoginUser 对象
 | 
			
		||||
        return AuthConvert.INSTANCE.convert2(user);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String login(AuthLoginReqVO reqVO, String userIp, String userAgent) {
 | 
			
		||||
        // 判断验证码是否正确
 | 
			
		||||
@@ -124,7 +101,8 @@ public class AdminAuthServiceImpl implements AdminAuthService {
 | 
			
		||||
                LoginLogTypeEnum.LOGIN_MOBILE, userIp, userAgent);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void verifyCaptcha(AuthLoginReqVO reqVO) {
 | 
			
		||||
    @VisibleForTesting
 | 
			
		||||
    void verifyCaptcha(AuthLoginReqVO reqVO) {
 | 
			
		||||
        // 如果验证码关闭,则不进行校验
 | 
			
		||||
        if (!captchaService.isCaptchaEnable()) {
 | 
			
		||||
            return;
 | 
			
		||||
@@ -149,46 +127,36 @@ public class AdminAuthServiceImpl implements AdminAuthService {
 | 
			
		||||
        captchaService.deleteCaptchaCode(reqVO.getUuid());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private LoginUser login0(String username, String password) {
 | 
			
		||||
    @VisibleForTesting
 | 
			
		||||
    LoginUser login0(String username, String password) {
 | 
			
		||||
        final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_USERNAME;
 | 
			
		||||
        // 用户验证
 | 
			
		||||
        Authentication authentication;
 | 
			
		||||
        try {
 | 
			
		||||
            // 调用 Spring Security 的 AuthenticationManager#authenticate(...) 方法,使用账号密码进行认证
 | 
			
		||||
            // 在其内部,会调用到 loadUserByUsername 方法,获取 User 信息
 | 
			
		||||
            authentication = authenticationManager.authenticate(new MultiUsernamePasswordAuthenticationToken(
 | 
			
		||||
                    username, password, getUserType()));
 | 
			
		||||
        } catch (BadCredentialsException badCredentialsException) {
 | 
			
		||||
        // 校验账号是否存在
 | 
			
		||||
        AdminUserDO user = userService.getUserByUsername(username);
 | 
			
		||||
        if (user == null) {
 | 
			
		||||
            createLoginLog(null, username, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
 | 
			
		||||
            throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
 | 
			
		||||
        } catch (DisabledException disabledException) {
 | 
			
		||||
            createLoginLog(null, username, logTypeEnum, LoginResultEnum.USER_DISABLED);
 | 
			
		||||
            throw exception(AUTH_LOGIN_USER_DISABLED);
 | 
			
		||||
        } catch (AuthenticationException authenticationException) {
 | 
			
		||||
            log.error("[login0][username({}) 发生未知异常]", username, authenticationException);
 | 
			
		||||
            createLoginLog(null, username, logTypeEnum, LoginResultEnum.UNKNOWN_ERROR);
 | 
			
		||||
            throw exception(AUTH_LOGIN_FAIL_UNKNOWN);
 | 
			
		||||
        }
 | 
			
		||||
        Assert.notNull(authentication.getPrincipal(), "Principal 不会为空");
 | 
			
		||||
        if (!userService.isPasswordMatch(password, user.getPassword())) {
 | 
			
		||||
            createLoginLog(user.getId(), username, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
 | 
			
		||||
            throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
 | 
			
		||||
        }
 | 
			
		||||
        // 校验是否禁用
 | 
			
		||||
        if (ObjectUtil.notEqual(user.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
 | 
			
		||||
            createLoginLog(user.getId(), username, logTypeEnum, LoginResultEnum.USER_DISABLED);
 | 
			
		||||
            throw exception(AUTH_LOGIN_USER_DISABLED);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // 构建 User 对象
 | 
			
		||||
        return AuthConvert.INSTANCE.convert((SpringSecurityUser) authentication.getPrincipal())
 | 
			
		||||
                .setUserType(getUserType().getValue());
 | 
			
		||||
        return buildLoginUser(user);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void createLoginLog(Long userId, String username,
 | 
			
		||||
                                LoginLogTypeEnum logTypeEnum, LoginResultEnum loginResult) {
 | 
			
		||||
        // 获得用户
 | 
			
		||||
        if (userId == null) {
 | 
			
		||||
            AdminUserDO user = userService.getUserByUsername(username);
 | 
			
		||||
            userId = user != null ? user.getId() : null;
 | 
			
		||||
        }
 | 
			
		||||
        // 插入登录日志
 | 
			
		||||
        LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO();
 | 
			
		||||
        reqDTO.setLogType(logTypeEnum.getType());
 | 
			
		||||
        reqDTO.setTraceId(TracerUtils.getTraceId());
 | 
			
		||||
        if (userId != null) {
 | 
			
		||||
            reqDTO.setUserId(userId);
 | 
			
		||||
        }
 | 
			
		||||
        reqDTO.setUserId(userId);
 | 
			
		||||
        reqDTO.setUserType(getUserType().getValue());
 | 
			
		||||
        reqDTO.setUsername(username);
 | 
			
		||||
        reqDTO.setUserAgent(ServletUtils.getUserAgent());
 | 
			
		||||
@@ -293,4 +261,8 @@ public class AdminAuthServiceImpl implements AdminAuthService {
 | 
			
		||||
        return user != null ? user.getUsername() : null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -105,7 +105,6 @@ public interface AdminUserService {
 | 
			
		||||
     */
 | 
			
		||||
    AdminUserDO getUserByMobile(String mobile);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获得用户分页列表
 | 
			
		||||
     *
 | 
			
		||||
@@ -209,4 +208,13 @@ public interface AdminUserService {
 | 
			
		||||
     */
 | 
			
		||||
    List<AdminUserDO> getUsersByStatus(Integer status);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 判断密码是否匹配
 | 
			
		||||
     *
 | 
			
		||||
     * @param rawPassword 未加密的密码
 | 
			
		||||
     * @param encodedPassword 加密后的密码
 | 
			
		||||
     * @return 是否匹配
 | 
			
		||||
     */
 | 
			
		||||
    boolean isPasswordMatch(String rawPassword, String encodedPassword);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -148,7 +148,7 @@ public class AdminUserServiceImpl implements AdminUserService {
 | 
			
		||||
        checkOldPassword(id, reqVO.getOldPassword());
 | 
			
		||||
        // 执行更新
 | 
			
		||||
        AdminUserDO updateObj = new AdminUserDO().setId(id);
 | 
			
		||||
        updateObj.setPassword(passwordEncoder.encode(reqVO.getNewPassword())); // 加密密码
 | 
			
		||||
        updateObj.setPassword(encodePassword(reqVO.getNewPassword())); // 加密密码
 | 
			
		||||
        userMapper.updateById(updateObj);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -172,7 +172,7 @@ public class AdminUserServiceImpl implements AdminUserService {
 | 
			
		||||
        // 更新密码
 | 
			
		||||
        AdminUserDO updateObj = new AdminUserDO();
 | 
			
		||||
        updateObj.setId(id);
 | 
			
		||||
        updateObj.setPassword(passwordEncoder.encode(password)); // 加密密码
 | 
			
		||||
        updateObj.setPassword(encodePassword(password)); // 加密密码
 | 
			
		||||
        userMapper.updateById(updateObj);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -205,11 +205,6 @@ public class AdminUserServiceImpl implements AdminUserService {
 | 
			
		||||
        return userMapper.selectByUsername(username);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 通过手机号获取用户
 | 
			
		||||
     * @param mobile
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public AdminUserDO getUserByMobile(String mobile) {
 | 
			
		||||
        return userMapper.selectByMobile(mobile);
 | 
			
		||||
@@ -395,7 +390,7 @@ public class AdminUserServiceImpl implements AdminUserService {
 | 
			
		||||
        if (user == null) {
 | 
			
		||||
            throw exception(USER_NOT_EXISTS);
 | 
			
		||||
        }
 | 
			
		||||
        if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
 | 
			
		||||
        if (!isPasswordMatch(oldPassword, user.getPassword())) {
 | 
			
		||||
            throw exception(USER_PASSWORD_FAILED);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -421,7 +416,7 @@ public class AdminUserServiceImpl implements AdminUserService {
 | 
			
		||||
            AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername());
 | 
			
		||||
            if (existUser == null) {
 | 
			
		||||
                userMapper.insert(UserConvert.INSTANCE.convert(importUser)
 | 
			
		||||
                        .setPassword(passwordEncoder.encode(userInitPassword))); // 设置默认密码
 | 
			
		||||
                        .setPassword(encodePassword(userInitPassword))); // 设置默认密码
 | 
			
		||||
                respVO.getCreateUsernames().add(importUser.getUsername());
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
@@ -443,4 +438,19 @@ public class AdminUserServiceImpl implements AdminUserService {
 | 
			
		||||
        return userMapper.selectListByStatus(status);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isPasswordMatch(String rawPassword, String encodedPassword) {
 | 
			
		||||
        return passwordEncoder.matches(rawPassword, encodedPassword);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 对密码进行加密
 | 
			
		||||
     *
 | 
			
		||||
     * @param password 密码
 | 
			
		||||
     * @return 加密后的密码
 | 
			
		||||
     */
 | 
			
		||||
    private String encodePassword(String password) {
 | 
			
		||||
        return passwordEncoder.encode(password);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user