feat:
This commit is contained in:
parent
12542ae115
commit
34b48cc27d
|
@ -1,7 +1,10 @@
|
||||||
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.dto.user.UserDTO;
|
||||||
import com.guwan.backend.service.UserService;
|
import com.guwan.backend.entity.User;
|
||||||
|
import com.guwan.backend.mapper.UserMapper;
|
||||||
|
import com.guwan.backend.security.CustomUserDetails;
|
||||||
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;
|
||||||
|
@ -16,30 +19,33 @@ import java.util.List;
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class UserDetailsServiceImpl implements UserDetailsService {
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||||
|
|
||||||
private final UserService userService;
|
private final UserMapper userMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
UserDTO user = userService.findByUsername(username);
|
User user = userMapper.selectOne(
|
||||||
|
new LambdaQueryWrapper<User>()
|
||||||
|
.eq(User::getUsername, username)
|
||||||
|
);
|
||||||
|
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw new UsernameNotFoundException("用户不存在");
|
throw new UsernameNotFoundException("用户不存在");
|
||||||
}
|
}
|
||||||
return convertToUserDetailsWithoutPassword(user);
|
|
||||||
|
return convertToUserDetails(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserDetails loadUserById(Long userId) throws UsernameNotFoundException {
|
public UserDetails loadUserById(Long userId) throws UsernameNotFoundException {
|
||||||
UserDTO user = userService.getUserById(userId);
|
User user = userMapper.selectById(userId);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw new UsernameNotFoundException("用户不存在");
|
throw new UsernameNotFoundException("用户不存在");
|
||||||
}
|
}
|
||||||
return convertToUserDetailsWithoutPassword(user);
|
return convertToUserDetails(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private UserDetails convertToUserDetails(User user) {
|
||||||
|
|
||||||
private UserDetails convertToUserDetailsWithoutPassword(UserDTO user) {
|
|
||||||
List<SimpleGrantedAuthority> authorities = new ArrayList<>();
|
List<SimpleGrantedAuthority> authorities = new ArrayList<>();
|
||||||
authorities.add(new SimpleGrantedAuthority("Admin"));
|
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
|
||||||
|
|
||||||
return new CustomUserDetails(
|
return new CustomUserDetails(
|
||||||
user.getId(),
|
user.getId(),
|
||||||
|
|
|
@ -13,6 +13,7 @@ import com.guwan.backend.service.UserService;
|
||||||
import com.guwan.backend.util.JwtUtil;
|
import com.guwan.backend.util.JwtUtil;
|
||||||
import com.guwan.backend.util.RedisUtil;
|
import com.guwan.backend.util.RedisUtil;
|
||||||
import com.guwan.backend.util.RedisUtils;
|
import com.guwan.backend.util.RedisUtils;
|
||||||
|
import com.guwan.backend.util.SecurityUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
@ -37,6 +38,7 @@ public class UserServiceImpl implements UserService {
|
||||||
private final RedisUtil redisUtil;
|
private final RedisUtil redisUtil;
|
||||||
private final RedisUtils redisUtils;
|
private final RedisUtils redisUtils;
|
||||||
private final EmailService emailService;
|
private final EmailService emailService;
|
||||||
|
private final SecurityUtil securityUtil;
|
||||||
|
|
||||||
private static final String USER_CACHE_KEY = "user:";
|
private static final String USER_CACHE_KEY = "user:";
|
||||||
private static final long USER_CACHE_DURATION = 3600L; // 1小时
|
private static final long USER_CACHE_DURATION = 3600L; // 1小时
|
||||||
|
@ -161,22 +163,7 @@ public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long getCurrentUserId() {
|
public Long getCurrentUserId() {
|
||||||
// 从SecurityContext中获取认证信息
|
return securityUtil.getCurrentUserId();
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
||||||
|
|
||||||
// 如果没有认证信息,返回null
|
|
||||||
if (authentication == null || !authentication.isAuthenticated() ||
|
|
||||||
authentication instanceof AnonymousAuthenticationToken) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取UserDetails
|
|
||||||
Object principal = authentication.getPrincipal();
|
|
||||||
if (principal instanceof CustomUserDetails) {
|
|
||||||
return ((CustomUserDetails) principal).getUserId();
|
|
||||||
}
|
|
||||||
//demo
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.guwan.backend.util;
|
||||||
|
|
||||||
|
import com.guwan.backend.security.CustomUserDetails;
|
||||||
|
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SecurityUtil {
|
||||||
|
|
||||||
|
public Long getCurrentUserId() {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
|
||||||
|
if (authentication == null || !authentication.isAuthenticated() ||
|
||||||
|
authentication instanceof AnonymousAuthenticationToken) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object principal = authentication.getPrincipal();
|
||||||
|
if (principal instanceof CustomUserDetails) {
|
||||||
|
return ((CustomUserDetails) principal).getUserId();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue