fix: 修复缓存导致的用户登录提示密码错误问题

pull/875/head
Jie Zheng 2025-01-17 15:23:29 +08:00
parent 10b43563aa
commit 7b2fa3c679
7 changed files with 9 additions and 81 deletions

View File

@ -19,10 +19,11 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.security.service.dto.JwtUserDto; import me.zhengjie.modules.security.service.dto.JwtUserDto;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.DataService; import me.zhengjie.modules.system.service.DataService;
import me.zhengjie.modules.system.service.RoleService; import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.UserService; import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserLoginDto; import me.zhengjie.modules.system.service.dto.UserDto;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -43,7 +44,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
public JwtUserDto loadUserByUsername(String username) { public JwtUserDto loadUserByUsername(String username) {
JwtUserDto jwtUserDto = userCacheManager.getUserCache(username); JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
if(jwtUserDto == null){ if(jwtUserDto == null){
UserLoginDto user = userService.getLoginData(username); UserDto user = userService.getLoginData(username);
if (user == null) { if (user == null) {
throw new BadRequestException("用户不存在"); throw new BadRequestException("用户不存在");
} else { } else {

View File

@ -20,7 +20,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import me.zhengjie.modules.system.service.dto.UserLoginDto; import me.zhengjie.modules.system.service.dto.UserDto;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -35,7 +35,7 @@ import java.util.stream.Collectors;
public class JwtUserDto implements UserDetails { public class JwtUserDto implements UserDetails {
@ApiModelProperty(value = "用户") @ApiModelProperty(value = "用户")
private final UserLoginDto user; private final UserDto user;
@ApiModelProperty(value = "数据权限") @ApiModelProperty(value = "数据权限")
private final List<Long> dataScopes; private final List<Long> dataScopes;
@ -51,12 +51,6 @@ public class JwtUserDto implements UserDetails {
return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet()); return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet());
} }
@Override
@JSONField(serialize = false)
public String getPassword() {
return user.getPassword();
}
@Override @Override
@JSONField(serialize = false) @JSONField(serialize = false)
public String getUsername() { public String getUsername() {

View File

@ -18,7 +18,6 @@ package me.zhengjie.modules.system.service;
import me.zhengjie.utils.PageResult; import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.system.domain.User; import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.dto.UserDto; import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.modules.system.service.dto.UserLoginDto;
import me.zhengjie.modules.system.service.dto.UserQueryCriteria; import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -72,7 +71,7 @@ public interface UserService {
* @param userName / * @param userName /
* @return / * @return /
*/ */
UserLoginDto getLoginData(String userName); UserDto getLoginData(String userName);
/** /**
* *

View File

@ -68,8 +68,8 @@ public class UserDto extends BaseDTO implements Serializable {
@ApiModelProperty(value = "头像路径") @ApiModelProperty(value = "头像路径")
private String avatarPath; private String avatarPath;
@ApiModelProperty(value = "密码")
@JSONField(serialize = false) @JSONField(serialize = false)
@ApiModelProperty(value = "密码")
private String password; private String password;
@ApiModelProperty(value = "是否启用") @ApiModelProperty(value = "是否启用")

View File

@ -1,34 +0,0 @@
/*
* Copyright 2019-2025 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.system.service.dto;
import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModelProperty;
/**
* @author Zheng Jie
* @description 使
* @date 2022-05-26
**/
public class UserLoginDto extends UserDto {
@ApiModelProperty(value = "密码")
@JSONField(serialize = false)
private String password;
@ApiModelProperty(value = "是否为管理员")
private Boolean isAdmin;
}

View File

@ -27,7 +27,6 @@ import me.zhengjie.exception.EntityNotFoundException;
import me.zhengjie.modules.system.repository.UserRepository; import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.UserService; import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.*; import me.zhengjie.modules.system.service.dto.*;
import me.zhengjie.modules.system.service.mapstruct.UserLoginMapper;
import me.zhengjie.modules.system.service.mapstruct.UserMapper; import me.zhengjie.modules.system.service.mapstruct.UserMapper;
import me.zhengjie.utils.*; import me.zhengjie.utils.*;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -57,7 +56,6 @@ public class UserServiceImpl implements UserService {
private final RedisUtils redisUtils; private final RedisUtils redisUtils;
private final UserCacheManager userCacheManager; private final UserCacheManager userCacheManager;
private final OnlineUserService onlineUserService; private final OnlineUserService onlineUserService;
private final UserLoginMapper userLoginMapper;
@Override @Override
public PageResult<UserDto> queryAll(UserQueryCriteria criteria, Pageable pageable) { public PageResult<UserDto> queryAll(UserQueryCriteria criteria, Pageable pageable) {
@ -182,12 +180,12 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public UserLoginDto getLoginData(String userName) { public UserDto getLoginData(String userName) {
User user = userRepository.findByUsername(userName); User user = userRepository.findByUsername(userName);
if (user == null) { if (user == null) {
return null; return null;
} else { } else {
return userLoginMapper.toDto(user); return userMapper.toDto(user);
} }
} }

View File

@ -1,30 +0,0 @@
/*
* Copyright 2019-2025 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.system.service.mapstruct;
import me.zhengjie.base.BaseMapper;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.dto.UserLoginDto;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
/**
* @author Zheng Jie
* @date 2018-11-23
*/
@Mapper(componentModel = "spring",uses = {RoleMapper.class, DeptMapper.class, JobMapper.class},unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface UserLoginMapper extends BaseMapper<UserLoginDto, User> {
}