This commit is contained in:
ovo 2024-12-07 19:46:23 +08:00
parent 9f16537549
commit 11bec69f6b
5 changed files with 97 additions and 82 deletions

View File

@ -73,6 +73,7 @@ public class OperationLogAspect {
* 保存日志 * 保存日志
*/ */
private void saveLog(ProceedingJoinPoint joinPoint, OperationLog operationLog, long beginTime, SysLog sysLog) { private void saveLog(ProceedingJoinPoint joinPoint, OperationLog operationLog, long beginTime, SysLog sysLog) {
try {
// 获取当前请求 // 获取当前请求
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) { if (attributes != null) {
@ -111,6 +112,9 @@ public class OperationLogAspect {
// 保存日志 // 保存日志
sysLogMapper.insert(sysLog); sysLogMapper.insert(sysLog);
} catch (Exception e) {
log.error("记录操作日志失败", e);
}
} }
/** /**

View File

@ -107,6 +107,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
* 设置认证信息到 SecurityContext * 设置认证信息到 SecurityContext
*/ */
private void setAuthentication(HttpServletRequest request, String jwt) { private void setAuthentication(HttpServletRequest request, String jwt) {
try {
Long userId = jwtUtil.getUserIdFromToken(jwt); Long userId = jwtUtil.getUserIdFromToken(jwt);
if (SecurityContextHolder.getContext().getAuthentication() == null) { if (SecurityContextHolder.getContext().getAuthentication() == null) {
UserDetails userDetails = userDetailsService.loadUserById(userId); UserDetails userDetails = userDetailsService.loadUserById(userId);
@ -119,6 +120,9 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
SecurityContextHolder.getContext().setAuthentication(authentication); SecurityContextHolder.getContext().setAuthentication(authentication);
} }
} catch (Exception e) {
log.error("设置用户认证信息失败", e);
}
} }
/** /**

View File

@ -1,8 +1,7 @@
package com.guwan.backend.security; package com.guwan.backend.security;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.guwan.backend.dto.user.UserDTO;
import com.guwan.backend.entity.User; import com.guwan.backend.service.UserService;
import com.guwan.backend.mapper.UserMapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
@ -17,41 +16,26 @@ import java.util.List;
@RequiredArgsConstructor @RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService { public class UserDetailsServiceImpl implements UserDetailsService {
private final UserMapper userMapper; private final UserService userService;
@Override @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userMapper.selectOne( UserDTO user = userService.findByUsername(username);
new LambdaQueryWrapper<User>()
.eq(User::getUsername, username)
);
if (user == null) { if (user == null) {
throw new UsernameNotFoundException("用户不存在"); throw new UsernameNotFoundException("用户不存在");
} }
return convertToUserDetails(user);
if (user.getStatus() != 1) {
throw new UsernameNotFoundException("账号已被禁用");
}
List<SimpleGrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
return new CustomUserDetails(
user.getId(),
user.getUsername(),
user.getPassword(),
authorities,
user.getStatus() == 1
);
} }
public UserDetails loadUserById(Long userId) throws UsernameNotFoundException { public UserDetails loadUserById(Long userId) throws UsernameNotFoundException {
User user = userMapper.selectById(userId); UserDTO user = userService.getUserById(userId);
if (user == null) { if (user == null) {
throw new UsernameNotFoundException("用户不存在"); throw new UsernameNotFoundException("用户不存在");
} }
return convertToUserDetails(user);
}
private UserDetails convertToUserDetails(UserDTO user) {
List<SimpleGrantedAuthority> authorities = new ArrayList<>(); List<SimpleGrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER")); authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

View File

@ -26,4 +26,7 @@ public interface UserService {
void resetPassword(String email); void resetPassword(String email);
public String refreshToken(String token); public String refreshToken(String token);
UserDTO findByUsername(String username);
UserDTO findByEmail(String email);
UserDTO findByPhone(String phone);
} }

View File

@ -219,25 +219,45 @@ public class UserServiceImpl implements UserService {
return null; return null;
} }
private User findByUsername(String username) { @Override
return userMapper.selectOne( public UserDTO findByUsername(String username) {
User user = userMapper.selectOne(
new LambdaQueryWrapper<User>() new LambdaQueryWrapper<User>()
.eq(User::getUsername, username) .eq(User::getUsername, username)
); );
return convertToDTO(user);
} }
private User findByEmail(String email) { @Override
return userMapper.selectOne( public UserDTO findByEmail(String email) {
User user = userMapper.selectOne(
new LambdaQueryWrapper<User>() new LambdaQueryWrapper<User>()
.eq(User::getEmail, email) .eq(User::getEmail, email)
); );
return convertToDTO(user);
} }
private User findByPhone(String phone) { @Override
return userMapper.selectOne( public UserDTO findByPhone(String phone) {
User user = userMapper.selectOne(
new LambdaQueryWrapper<User>() new LambdaQueryWrapper<User>()
.eq(User::getPhone, phone) .eq(User::getPhone, phone)
); );
return convertToDTO(user);
}
private User findUserByUsername(String username) {
UserDTO userDTO = findByUsername(username);
return userDTO != null ? convertToEntity(userDTO) : null;
}
private User convertToEntity(UserDTO dto) {
if (dto == null) {
return null;
}
User user = new User();
BeanUtils.copyProperties(dto, user);
return user;
} }
private UserDTO convertToDTO(User user) { private UserDTO convertToDTO(User user) {