refactor: 代码优化
This commit is contained in:
parent
d2bb69798a
commit
cfbbc7c857
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package me.zhengjie.utils;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@ -91,7 +92,7 @@ public class SecurityUtils {
|
||||
*/
|
||||
public static String getDataScopeType() {
|
||||
List<Long> dataScopes = getCurrentUserDataScope();
|
||||
if(dataScopes.size() != 0){
|
||||
if(CollUtil.isEmpty(dataScopes)){
|
||||
return "";
|
||||
}
|
||||
return DataScopeEnum.ALL.getValue();
|
||||
|
@ -17,12 +17,11 @@ package me.zhengjie.modules.security.security;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import io.jsonwebtoken.ExpiredJwtException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.modules.security.config.SecurityProperties;
|
||||
import me.zhengjie.modules.security.service.UserCacheManager;
|
||||
import me.zhengjie.modules.security.service.dto.OnlineUserDto;
|
||||
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.context.SecurityContextHolder;
|
||||
import org.springframework.util.StringUtils;
|
||||
@ -38,9 +37,8 @@ import java.util.Objects;
|
||||
/**
|
||||
* @author /
|
||||
*/
|
||||
@Slf4j
|
||||
public class TokenFilter extends GenericFilterBean {
|
||||
private static final Logger log = LoggerFactory.getLogger(TokenFilter.class);
|
||||
|
||||
|
||||
private final TokenProvider tokenProvider;
|
||||
private final SecurityProperties properties;
|
||||
|
@ -18,14 +18,12 @@ package me.zhengjie.modules.security.service;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.exception.EntityNotFoundException;
|
||||
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.RoleService;
|
||||
import me.zhengjie.modules.system.service.UserService;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
@ -45,24 +43,14 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
public JwtUserDto loadUserByUsername(String username) {
|
||||
JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
|
||||
if(jwtUserDto == null){
|
||||
User user;
|
||||
try {
|
||||
user = userService.getLoginData(username);
|
||||
} catch (EntityNotFoundException e) {
|
||||
// SpringSecurity会自动转换UsernameNotFoundException为BadCredentialsException
|
||||
throw new UsernameNotFoundException(username, e);
|
||||
}
|
||||
User user = userService.getLoginData(username);
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("");
|
||||
throw new BadRequestException("用户不存在");
|
||||
} else {
|
||||
if (!user.getEnabled()) {
|
||||
throw new BadRequestException("账号未激活!");
|
||||
}
|
||||
jwtUserDto = new JwtUserDto(
|
||||
user,
|
||||
dataService.getDeptIds(user),
|
||||
roleService.mapToGrantedAuthorities(user)
|
||||
);
|
||||
jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), roleService.buildAuthorities(user));
|
||||
// 添加缓存数据
|
||||
userCacheManager.addUserCache(username, jwtUserDto);
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ public interface RoleService extends IService<Role> {
|
||||
* @param user 用户信息
|
||||
* @return 权限信息
|
||||
*/
|
||||
List<AuthorityDto> mapToGrantedAuthorities(User user);
|
||||
List<AuthorityDto> buildAuthorities(User user);
|
||||
|
||||
/**
|
||||
* 验证是否被用户关联
|
||||
|
@ -166,7 +166,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
||||
|
||||
@Override
|
||||
@Cacheable(key = "'auth:' + #p0.id")
|
||||
public List<AuthorityDto> mapToGrantedAuthorities(User user) {
|
||||
public List<AuthorityDto> buildAuthorities(User user) {
|
||||
Set<String> permissions = new HashSet<>();
|
||||
// 如果是管理员直接返回
|
||||
if (user.getIsAdmin()) {
|
||||
|
@ -26,7 +26,6 @@ import me.zhengjie.modules.system.domain.Job;
|
||||
import me.zhengjie.modules.system.domain.Role;
|
||||
import me.zhengjie.modules.system.domain.User;
|
||||
import me.zhengjie.exception.EntityExistException;
|
||||
import me.zhengjie.exception.EntityNotFoundException;
|
||||
import me.zhengjie.modules.system.domain.vo.UserQueryCriteria;
|
||||
import me.zhengjie.modules.system.mapper.UserJobMapper;
|
||||
import me.zhengjie.modules.system.mapper.UserMapper;
|
||||
@ -191,12 +190,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
|
||||
@Override
|
||||
public User getLoginData(String userName) {
|
||||
User user = userMapper.findByUsername(userName);
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException(User.class, "name", userName);
|
||||
} else {
|
||||
return user;
|
||||
}
|
||||
return userMapper.findByUsername(userName);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user