This commit is contained in:
lixiaodong
2023-07-13 20:15:41 +08:00
parent 54d8d9489a
commit ac6921a278
11 changed files with 102 additions and 65 deletions

View File

@@ -4,13 +4,8 @@ import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import net.lab1024.sa.common.config.UrlConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
/**
* sa-token 配置
@@ -19,25 +14,21 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
* @date 2023/7/13 16:57
*/
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {
public class SaTokenConfig {
/**
* 注册 Sa-Token 拦截器,定义详细认证规则
*
* @param registry
* 定义 Sa-Token 拦截器,定义详细认证规则
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
SaInterceptor interceptor = new SaInterceptor(handler -> {
// 每个路由 都做为一个权限点
UrlConfig.URL_LIST.forEach(url -> SaRouter.match(url, r -> StpUtil.checkPermission(url)));
});
@Bean
public SaInterceptor saInterceptor() {
// 关闭注解鉴权 只做路由拦截校验
interceptor.isAnnotation(false);
registry.addInterceptor(interceptor).addPathPatterns("/**");
return new SaInterceptor(handler -> {
/**
* 每个路由 都做为一个权限点
* TODO listen from定义的api
* from menu数据表已选择的api
*/
UrlConfig.AUTH_URL_LIST.forEach(url -> SaRouter.match(url, r -> StpUtil.checkPermission(url)));
}).isAnnotation(false);
}
}

View File

@@ -1,12 +1,18 @@
package net.lab1024.sa.admin.module.system.employee.service;
import cn.dev33.satoken.stp.StpInterface;
import net.lab1024.sa.admin.module.system.employee.dao.EmployeeDao;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.admin.module.system.menu.constant.MenuTypeEnum;
import net.lab1024.sa.admin.module.system.menu.domain.vo.MenuVO;
import net.lab1024.sa.admin.module.system.role.service.RoleEmployeeService;
import net.lab1024.sa.admin.module.system.role.service.RoleMenuService;
import net.lab1024.sa.common.module.support.token.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
/**
* 员工权限校验
@@ -18,7 +24,7 @@ import java.util.List;
* @Copyright 1024创新实验室 https://1024lab.net
*/
@Service
public class EmployeePermissionService {
public class EmployeePermissionService implements StpInterface {
@Autowired
private RoleEmployeeService roleEmployeeService;
@@ -26,6 +32,9 @@ public class EmployeePermissionService {
@Autowired
private RoleMenuService roleMenuService;
@Autowired
private EmployeeDao employeeDao;
/**
* 查询用户拥有的前端菜单项 用于登陆返回 前端动态路由配置
*
@@ -37,4 +46,20 @@ public class EmployeePermissionService {
return roleMenuService.getMenuList(roleIdList, administratorFlag);
}
@Override
public List<String> getPermissionList(Object loginId, String loginType) {
Long employeeId = TokenService.getUserId((String) loginId);
// TODO listen 待做权限缓存
EmployeeEntity employeeEntity = employeeDao.selectById(employeeId);
List<MenuVO> menuList = this.getEmployeeMenuAndPointsList(employeeId, employeeEntity.getAdministratorFlag());
return menuList.stream()
.filter(e -> MenuTypeEnum.POINTS.equalsValue(e.getMenuType()))
.map(MenuVO::getApiPerms).distinct().collect(Collectors.toList());
}
@Override
public List<String> getRoleList(Object loginId, String loginType) {
return null;
}
}

View File

@@ -1,6 +1,5 @@
package net.lab1024.sa.admin.module.system.login.controller;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.extra.servlet.ServletUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -13,7 +12,6 @@ import net.lab1024.sa.common.common.constant.RequestHeaderConst;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.common.util.SmartRequestUtil;
import net.lab1024.sa.common.module.support.captcha.domain.CaptchaVO;
import org.checkerframework.checker.units.qual.N;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@@ -58,20 +56,8 @@ public class LoginController {
@ApiOperation("获取登录结果信息 @author 卓大")
@GetMapping("/login/getLoginInfo")
public ResponseDTO<LoginEmployeeDetail> getLoginInfo() {
/* Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
return ResponseDTO.error(UserErrorCode.LOGIN_STATE_INVALID);
}
Object principal = authentication.getPrincipal();
if (!(principal instanceof LoginEmployeeDetail)) {
return ResponseDTO.error(UserErrorCode.LOGIN_STATE_INVALID);
}
LoginEmployeeDetail loginEmployeeDetail = (LoginEmployeeDetail) authentication.getPrincipal();
loginEmployeeDetail.setLoginPassword(null);
return ResponseDTO.ok(loginEmployeeDetail);*/
return ResponseDTO.ok();
LoginEmployeeDetail loginEmployeeDetail = loginService.getLoginUserDetailCache(SmartRequestUtil.getUserId());
return ResponseDTO.ok(loginEmployeeDetail);
}
@ApiOperation("退出登录 @author 卓大")

View File

@@ -93,7 +93,7 @@ public class LoginService {
// 校验 图形验证码
ResponseDTO<String> checkCaptcha = captchaService.checkCaptcha(loginForm);
if (!checkCaptcha.getOk()) {
// TODO listen 待放开 return ResponseDTO.error(checkCaptcha);
// TODO listen 待放开 return ResponseDTO.error(checkCaptcha);
}
/**
@@ -160,7 +160,7 @@ public class LoginService {
List<MenuVO> menuAndPointsList = employeePermissionService.getEmployeeMenuAndPointsList(employeeEntity.getEmployeeId(), employeeEntity.getAdministratorFlag());
//前端菜单
loginEmployeeDetail.setMenuList(menuAndPointsList);
//后端权限
// TODO listen 原先为什么返回权限
// loginEmployeeDetail.setAuthorities(employeePermissionService.buildAuthorities(menuAndPointsList));
//上次登录信息
@@ -195,6 +195,15 @@ public class LoginService {
loginLogService.log(loginEntity);
}
/**
* 查询用户信息缓存
*
* @param requestUserId
*/
public LoginEmployeeDetail getLoginUserDetailCache(Long requestUserId) {
return loginUserDetailCache.get(requestUserId);
}
/**
* 移除用户信息缓存

View File

@@ -20,7 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -39,6 +38,10 @@ public class MenuService {
@Autowired
private MenuDao menuDao;
@Autowired
private List<RequestUrlVO> authUrl;
/**
* 添加菜单
*
@@ -281,8 +284,7 @@ public class MenuService {
* @return
*/
public ResponseDTO<List<RequestUrlVO>> getAuthUrl() {
// TODO listen
return ResponseDTO.ok(Collections.emptyList());
return ResponseDTO.ok(authUrl);
}
}

View File

@@ -77,9 +77,9 @@ public class RoleMenuService {
* @param roleIdList
* @return
*/
public List<MenuVO> getMenuList(List<Long> roleIdList, Boolean administratorFlag) {
//管理员返回所有菜单
if(administratorFlag){
public List<MenuVO> getMenuList(List<Long> roleIdList, Boolean adminFlag) {
//管返回所有菜单
if(adminFlag){
List<MenuEntity> menuEntityList = roleMenuDao.selectMenuListByRoleIdList(Lists.newArrayList(), false);
return SmartBeanUtil.copyList(menuEntityList, MenuVO.class);
}