refactor: 代码优化

This commit is contained in:
Jie Zheng 2025-01-13 17:04:22 +08:00
parent d2bb69798a
commit cfbbc7c857
6 changed files with 10 additions and 29 deletions

View File

@ -15,6 +15,7 @@
*/ */
package me.zhengjie.utils; package me.zhengjie.utils;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
@ -91,7 +92,7 @@ public class SecurityUtils {
*/ */
public static String getDataScopeType() { public static String getDataScopeType() {
List<Long> dataScopes = getCurrentUserDataScope(); List<Long> dataScopes = getCurrentUserDataScope();
if(dataScopes.size() != 0){ if(CollUtil.isEmpty(dataScopes)){
return ""; return "";
} }
return DataScopeEnum.ALL.getValue(); return DataScopeEnum.ALL.getValue();

View File

@ -17,12 +17,11 @@ package me.zhengjie.modules.security.security;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.security.config.SecurityProperties; import me.zhengjie.modules.security.config.SecurityProperties;
import me.zhengjie.modules.security.service.UserCacheManager; import me.zhengjie.modules.security.service.UserCacheManager;
import me.zhengjie.modules.security.service.dto.OnlineUserDto; import me.zhengjie.modules.security.service.dto.OnlineUserDto;
import me.zhengjie.modules.security.service.OnlineUserService; import me.zhengjie.modules.security.service.OnlineUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -38,9 +37,8 @@ import java.util.Objects;
/** /**
* @author / * @author /
*/ */
@Slf4j
public class TokenFilter extends GenericFilterBean { public class TokenFilter extends GenericFilterBean {
private static final Logger log = LoggerFactory.getLogger(TokenFilter.class);
private final TokenProvider tokenProvider; private final TokenProvider tokenProvider;
private final SecurityProperties properties; private final SecurityProperties properties;

View File

@ -18,14 +18,12 @@ package me.zhengjie.modules.security.service;
import lombok.RequiredArgsConstructor; 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.exception.EntityNotFoundException;
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.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 org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
@ -45,24 +43,14 @@ 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){
User user; User user = userService.getLoginData(username);
try {
user = userService.getLoginData(username);
} catch (EntityNotFoundException e) {
// SpringSecurity会自动转换UsernameNotFoundException为BadCredentialsException
throw new UsernameNotFoundException(username, e);
}
if (user == null) { if (user == null) {
throw new UsernameNotFoundException(""); throw new BadRequestException("用户不存在");
} else { } else {
if (!user.getEnabled()) { if (!user.getEnabled()) {
throw new BadRequestException("账号未激活!"); throw new BadRequestException("账号未激活!");
} }
jwtUserDto = new JwtUserDto( jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), roleService.buildAuthorities(user));
user,
dataService.getDeptIds(user),
roleService.mapToGrantedAuthorities(user)
);
// 添加缓存数据 // 添加缓存数据
userCacheManager.addUserCache(username, jwtUserDto); userCacheManager.addUserCache(username, jwtUserDto);
} }

View File

@ -114,7 +114,7 @@ public interface RoleService extends IService<Role> {
* @param user 用户信息 * @param user 用户信息
* @return 权限信息 * @return 权限信息
*/ */
List<AuthorityDto> mapToGrantedAuthorities(User user); List<AuthorityDto> buildAuthorities(User user);
/** /**
* 验证是否被用户关联 * 验证是否被用户关联

View File

@ -166,7 +166,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
@Override @Override
@Cacheable(key = "'auth:' + #p0.id") @Cacheable(key = "'auth:' + #p0.id")
public List<AuthorityDto> mapToGrantedAuthorities(User user) { public List<AuthorityDto> buildAuthorities(User user) {
Set<String> permissions = new HashSet<>(); Set<String> permissions = new HashSet<>();
// 如果是管理员直接返回 // 如果是管理员直接返回
if (user.getIsAdmin()) { if (user.getIsAdmin()) {

View File

@ -26,7 +26,6 @@ import me.zhengjie.modules.system.domain.Job;
import me.zhengjie.modules.system.domain.Role; import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.domain.User; import me.zhengjie.modules.system.domain.User;
import me.zhengjie.exception.EntityExistException; import me.zhengjie.exception.EntityExistException;
import me.zhengjie.exception.EntityNotFoundException;
import me.zhengjie.modules.system.domain.vo.UserQueryCriteria; import me.zhengjie.modules.system.domain.vo.UserQueryCriteria;
import me.zhengjie.modules.system.mapper.UserJobMapper; import me.zhengjie.modules.system.mapper.UserJobMapper;
import me.zhengjie.modules.system.mapper.UserMapper; import me.zhengjie.modules.system.mapper.UserMapper;
@ -191,12 +190,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
@Override @Override
public User getLoginData(String userName) { public User getLoginData(String userName) {
User user = userMapper.findByUsername(userName); return userMapper.findByUsername(userName);
if (user == null) {
throw new EntityNotFoundException(User.class, "name", userName);
} else {
return user;
}
} }
@Override @Override