@@ -32,9 +32,6 @@ public class FileDownloadController { | |||||
@Autowired | @Autowired | ||||
private FilePathGenerator filePathGenerator; | private FilePathGenerator filePathGenerator; | ||||
@Autowired | |||||
private RedisManager redisManager; | |||||
/** | /** | ||||
* path方式下载文件 | * path方式下载文件 | ||||
*/ | */ | ||||
@@ -56,8 +53,6 @@ public class FileDownloadController { | |||||
@RequestParam(value = "noCache", required = false) Boolean noCache, | @RequestParam(value = "noCache", required = false) Boolean noCache, | ||||
HttpServletRequest request | HttpServletRequest request | ||||
) throws FileNotFoundException { | ) throws FileNotFoundException { | ||||
SessionUtil.checkSession(request, redisManager); | |||||
File file = new File(filePathGenerator.getFileDiskPath(path)); | File file = new File(filePathGenerator.getFileDiskPath(path)); | ||||
if (!file.exists()) { | if (!file.exists()) { | ||||
@@ -30,9 +30,6 @@ public class FileUploadController { | |||||
private DbEngine dbEngine; | private DbEngine dbEngine; | ||||
@Autowired | @Autowired | ||||
private RedisManager redisManager; | |||||
@Autowired | |||||
private ImageAttachDao imageAttachDao; | private ImageAttachDao imageAttachDao; | ||||
// TODO: 权限处理,临时文件处理 | // TODO: 权限处理,临时文件处理 | ||||
@@ -44,7 +41,6 @@ public class FileUploadController { | |||||
@RequestParam(value = "keepName", required = false) Boolean keepName, | @RequestParam(value = "keepName", required = false) Boolean keepName, | ||||
HttpServletRequest request | HttpServletRequest request | ||||
) { | ) { | ||||
SessionUtil.checkSession(request, redisManager); | |||||
return uploadFile(path, file, ThumbImage.type(thumb), thumbHeight, insert, keepName); | return uploadFile(path, file, ThumbImage.type(thumb), thumbHeight, insert, keepName); | ||||
} | } | ||||
@@ -54,8 +50,6 @@ public class FileUploadController { | |||||
@RequestParam(value = "thumbHeight", required = false) Integer thumbHeight, | @RequestParam(value = "thumbHeight", required = false) Integer thumbHeight, | ||||
@RequestParam(value = "commit", required = false) Boolean insert, | @RequestParam(value = "commit", required = false) Boolean insert, | ||||
HttpServletRequest request) { | HttpServletRequest request) { | ||||
SessionUtil.checkSession(request, redisManager); | |||||
MultipartFile file = MemMultipartFile.build(data.getData()); | MultipartFile file = MemMultipartFile.build(data.getData()); | ||||
if (file == null) { | if (file == null) { | ||||
return R.error("数据内容格式有错"); | return R.error("数据内容格式有错"); | ||||
@@ -70,7 +64,6 @@ public class FileUploadController { | |||||
@RequestParam(value = "commit", required = false) Boolean insert, | @RequestParam(value = "commit", required = false) Boolean insert, | ||||
@RequestParam(value = "keepName", required = false) Boolean keepName, | @RequestParam(value = "keepName", required = false) Boolean keepName, | ||||
HttpServletRequest request) { | HttpServletRequest request) { | ||||
SessionUtil.checkSession(request, redisManager); | |||||
return uploadFile(path, file, ThumbImage.TYPE_AVATAR, size, insert, keepName); | return uploadFile(path, file, ThumbImage.TYPE_AVATAR, size, insert, keepName); | ||||
} | } | ||||
@@ -81,7 +74,6 @@ public class FileUploadController { | |||||
@RequestParam(value = "thumbHeight", required = false) Integer thumbHeight, | @RequestParam(value = "thumbHeight", required = false) Integer thumbHeight, | ||||
@RequestParam(value = "keepName", required = false) Boolean keepName, | @RequestParam(value = "keepName", required = false) Boolean keepName, | ||||
HttpServletRequest request) { | HttpServletRequest request) { | ||||
SessionUtil.checkSession(request, redisManager); | |||||
return uploadFile(path, file, ThumbImage.type(thumb), thumbHeight, true, keepName); | return uploadFile(path, file, ThumbImage.type(thumb), thumbHeight, true, keepName); | ||||
} | } | ||||
@@ -143,8 +135,6 @@ public class FileUploadController { | |||||
// TODO: 修改为安全的后台删除方式 | // TODO: 修改为安全的后台删除方式 | ||||
@PostMapping("/fs/remove") | @PostMapping("/fs/remove") | ||||
public R remove(@RequestParam(value = "filePath") String filePath, HttpServletRequest request) { | public R remove(@RequestParam(value = "filePath") String filePath, HttpServletRequest request) { | ||||
SessionUtil.checkSession(request, redisManager); | |||||
File file = new File(filePathGenerator.getFileDiskPath(filePath)); | File file = new File(filePathGenerator.getFileDiskPath(filePath)); | ||||
if (file.exists() && file.isFile()) { | if (file.exists() && file.isFile()) { | ||||
if (file.delete()) { | if (file.delete()) { | ||||
@@ -97,7 +97,7 @@ public final class AttachHelper { | |||||
//批量删除 | //批量删除 | ||||
public boolean deleteAttachList(List<String> attachIdList) throws Exception { | public boolean deleteAttachList(List<String> attachIdList) throws Exception { | ||||
if (CommUtil.isEmpty(attachIdList)) return false; | if (CommUtil.isEmpty(attachIdList)) return false; | ||||
dbEngine.update("delete from " + AttachInfo.ENTITY_NAME + " where attach_id in(" + CommUtil.getSqlInStr(attachIdList) + ")"); | |||||
dbEngine.update("delete from " + AttachInfo.ENTITY_NAME + " where attach_id in(" + StringUtil.join(attachIdList,",") + ")"); | |||||
return true; | return true; | ||||
} | } | ||||
@@ -21,6 +21,7 @@ import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlan; | |||||
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanCache; | import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanCache; | ||||
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanContent; | import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanContent; | ||||
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanItem; | import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanItem; | ||||
import cc.smtweb.system.bpm.web.sys.user.role.RoleHelper; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import java.util.*; | import java.util.*; | ||||
@@ -33,9 +34,16 @@ public class PreviewMenuTreeService { | |||||
public R treeAll(@SwParam("module") String module, UserSession us) { | public R treeAll(@SwParam("module") String module, UserSession us) { | ||||
String prj_id = ModelProjectCache.getInstance().getIdByModule(module); | String prj_id = ModelProjectCache.getInstance().getIdByModule(module); | ||||
if (!SwConsts.SysParam.SYS_DEBUG) { | if (!SwConsts.SysParam.SYS_DEBUG) { | ||||
List<MenuVO> menuVOList = buildMenu(prj_id, module, us); | |||||
MenuVO home = new MenuVO(); | |||||
List<MenuVO> menuVOList = buildMenu(prj_id, module, us,home); | |||||
if (!CommUtil.isEmpty(menuVOList)) { | if (!CommUtil.isEmpty(menuVOList)) { | ||||
return R.success(menuVOList); | |||||
R r = R.success(menuVOList); | |||||
if(home.getId()!=null&&home.getId()>0){ | |||||
r.put("home",home); | |||||
} | |||||
return r; | |||||
}else { | |||||
return R.success(new ArrayList<>()); | |||||
} | } | ||||
} | } | ||||
List<ModelForm> listForm; | List<ModelForm> listForm; | ||||
@@ -73,20 +81,30 @@ public class PreviewMenuTreeService { | |||||
root.setPath(module); | root.setPath(module); | ||||
root.setId(-1L); | root.setId(-1L); | ||||
List<MenuVO> data = TreeDataUtil.buildTree(root, list, MenuVO.createTreeHandler()); | List<MenuVO> data = TreeDataUtil.buildTree(root, list, MenuVO.createTreeHandler()); | ||||
return R.success(data); | |||||
return R.success(data); | |||||
} | } | ||||
public List<MenuVO> buildMenu(String prj_id, String module, UserSession us) { | |||||
Set<MenuPlan> set = MenuPlanCache.getInstance().getByP(prj_id); | |||||
public List<MenuVO> buildMenu(String prj_id, String module, UserSession us,MenuVO home) { | |||||
Set<MenuPlan> set = RoleHelper.isAdmin(us.getUserId()) ? MenuPlanCache.getInstance().getByP(prj_id):RoleHelper.getMenuPlans(us.getUserId(),us.getPartyId()); | |||||
if (CommUtil.isEmpty(set)) { | if (CommUtil.isEmpty(set)) { | ||||
return new ArrayList<>(); | return new ArrayList<>(); | ||||
} | } | ||||
MenuPlan menuPlan = set.iterator().next(); | MenuPlan menuPlan = set.iterator().next(); | ||||
MenuPlanContent mpc = new MenuPlanContent(menuPlan.getContent()); | MenuPlanContent mpc = new MenuPlanContent(menuPlan.getContent()); | ||||
List<MenuVO> list = new ArrayList<>(); | List<MenuVO> list = new ArrayList<>(); | ||||
Set<Long> rightMenuIds = RoleHelper.getRoleMenuIds(us.getUserId() , us.getPartyId(),menuPlan.getId(), true); | |||||
for(MenuPlanItem item : mpc.getHomeList()){ | |||||
long id = item.getId(); | |||||
if(rightMenuIds.contains(id)){ | |||||
home.setId(item.getId()); | |||||
home.setName(item.getLabel()); | |||||
home.setIcon(item.getIcon()); | |||||
home.setPath(MenuCache.getInstance().getPath(item.getMenu())); | |||||
break; | |||||
} | |||||
} | |||||
for (MenuPlanItem mp : mpc.getChildren(0)) { | for (MenuPlanItem mp : mpc.getChildren(0)) { | ||||
List<MenuVO> menuVOList = buildMenu(-1, mp); | |||||
List<MenuVO> menuVOList = RoleHelper.isAdmin(us.getUserId())? buildMenu(-1,mp):buildMenuWithRight(-1, mp,rightMenuIds); | |||||
if (menuVOList == null) continue; | if (menuVOList == null) continue; | ||||
list.addAll(menuVOList); | list.addAll(menuVOList); | ||||
} | } | ||||
@@ -118,7 +136,27 @@ public class PreviewMenuTreeService { | |||||
list.add(menu); | list.add(menu); | ||||
return list; | return list; | ||||
} | } | ||||
private List<MenuVO> buildMenuWithRight(long parent_id, MenuPlanItem mp,Set<Long> rightMenuIds) { | |||||
if (mp == null || !rightMenuIds.contains(mp.getId())) return null; | |||||
List<MenuVO> list = new ArrayList<>(); | |||||
MenuVO menu = new MenuVO(); | |||||
menu.setId(mp.getId()); | |||||
menu.setName(mp.getLabel()); | |||||
menu.setIcon(mp.getIcon()); | |||||
menu.setPath(MenuCache.getInstance().getPath(mp.getMenu())); | |||||
menu.setParentId(parent_id); | |||||
List<MenuVO> child = new ArrayList<>(); | |||||
if (!CommUtil.isEmpty(mp.getChildren())) { | |||||
for (MenuPlanItem cmp : mp.getChildren()) { | |||||
List<MenuVO> m = buildMenuWithRight(menu.getId(), cmp,rightMenuIds); | |||||
if (m == null) continue; | |||||
child.addAll(m); | |||||
} | |||||
} | |||||
menu.setChildren(child); | |||||
list.add(menu); | |||||
return list; | |||||
} | |||||
private MenuVO setMenuParent(Long parent_id, Map<Long, MenuVO> map, List<MenuVO> list) { | private MenuVO setMenuParent(Long parent_id, Map<Long, MenuVO> map, List<MenuVO> list) { | ||||
MenuVO parent = map.get(parent_id); | MenuVO parent = map.get(parent_id); | ||||
if (parent != null) return parent; | if (parent != null) return parent; | ||||
@@ -14,6 +14,7 @@ import cc.smtweb.framework.core.exception.SwException; | |||||
import cc.smtweb.framework.core.mvc.service.SqlNamedPara; | import cc.smtweb.framework.core.mvc.service.SqlNamedPara; | ||||
import cc.smtweb.framework.core.util.MapUtil; | import cc.smtweb.framework.core.util.MapUtil; | ||||
import cc.smtweb.framework.core.util.NumberUtil; | import cc.smtweb.framework.core.util.NumberUtil; | ||||
import cc.smtweb.framework.core.util.SqlUtil; | |||||
import cc.smtweb.framework.core.util.StringUtil; | import cc.smtweb.framework.core.util.StringUtil; | ||||
import cc.smtweb.system.bpm.web.design.form.define.*; | import cc.smtweb.system.bpm.web.design.form.define.*; | ||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
@@ -187,7 +188,7 @@ public class DynPageHelper { | |||||
if (listener != null) listener.buildSelect(dataSet, sql); | if (listener != null) listener.buildSelect(dataSet, sql); | ||||
return sql.toString(); | return sql.toString(); | ||||
} else { | } else { | ||||
return dataSet.sql; | |||||
return SqlUtil.replaceTable(dataSet.sql); | |||||
} | } | ||||
} | } | ||||
@@ -104,7 +104,7 @@ public class DynPageLoadHandler extends AbstractDynPageHandler { | |||||
SwMap filter = params.readMap("filter"); | SwMap filter = params.readMap("filter"); | ||||
//对应的数据集定义 | //对应的数据集定义 | ||||
PageDataset pageDataSet = readParamDs(); | PageDataset pageDataSet = readParamDs(); | ||||
DynRetBean bean = null; | DynRetBean bean = null; | ||||
if (SwEnum.DatasetType.LIST.value.equals(pageDataSet.type)) {//列表类 | if (SwEnum.DatasetType.LIST.value.equals(pageDataSet.type)) {//列表类 | ||||
bean = DynRetBean.createList(getListWorker(filter, pageDataSet).buildListData()); | bean = DynRetBean.createList(getListWorker(filter, pageDataSet).buildListData()); | ||||
@@ -2,7 +2,6 @@ package cc.smtweb.system.bpm.web.login; | |||||
import cc.smtweb.framework.core.annotation.SwBody; | import cc.smtweb.framework.core.annotation.SwBody; | ||||
import cc.smtweb.framework.core.annotation.SwParam; | import cc.smtweb.framework.core.annotation.SwParam; | ||||
import cc.smtweb.framework.core.annotation.SwPerm; | |||||
import cc.smtweb.framework.core.annotation.SwService; | import cc.smtweb.framework.core.annotation.SwService; | ||||
import cc.smtweb.framework.core.common.R; | import cc.smtweb.framework.core.common.R; | ||||
import cc.smtweb.framework.core.common.SwMap; | import cc.smtweb.framework.core.common.SwMap; | ||||
@@ -28,7 +27,6 @@ public class AuthService { | |||||
@SwParam | @SwParam | ||||
private SessionManager sessionManager; | private SessionManager sessionManager; | ||||
@SwPerm() | |||||
public R getParty(@SwParam("username") String username) { | public R getParty(@SwParam("username") String username) { | ||||
Set<Party> partySet = PartyCache.getInstance().getTopSet(); | Set<Party> partySet = PartyCache.getInstance().getTopSet(); | ||||
List<SwMap> list = new ArrayList<>(); | List<SwMap> list = new ArrayList<>(); | ||||
@@ -43,7 +41,6 @@ public class AuthService { | |||||
return R.success(list); | return R.success(list); | ||||
} | } | ||||
@SwPerm(SwPerm.NONE) | |||||
public R login(@SwBody LoginVO loginPO) { | public R login(@SwBody LoginVO loginPO) { | ||||
SwMap data = new SwMap(); | SwMap data = new SwMap(); | ||||
User user = null; | User user = null; | ||||
@@ -55,7 +52,7 @@ public class AuthService { | |||||
// return new UserSession(1); | // return new UserSession(1); | ||||
} else { | } else { | ||||
user = LoginHelper.login(loginPO); | user = LoginHelper.login(loginPO); | ||||
userSession = new UserSession(user.getId()); | |||||
userSession = LoginHelper.createUserSession(user); | |||||
} | } | ||||
} catch (BizException e) { | } catch (BizException e) { | ||||
data.put("isOk", false); | data.put("isOk", false); | ||||
@@ -96,12 +93,10 @@ public class AuthService { | |||||
return R.success(data); | return R.success(data); | ||||
} | } | ||||
@SwPerm() | |||||
public R ping(@SwParam("msg") String msg) { | public R ping(@SwParam("msg") String msg) { | ||||
return R.success(msg); | return R.success(msg); | ||||
} | } | ||||
@SwPerm("user:edit") | |||||
public R config(@SwParam("username") String username) { | public R config(@SwParam("username") String username) { | ||||
return R.success("config: " + username); | return R.success("config: " + username); | ||||
} | } | ||||
@@ -8,10 +8,13 @@ import cc.smtweb.framework.core.exception.BizException; | |||||
import cc.smtweb.framework.core.session.UserSession; | import cc.smtweb.framework.core.session.UserSession; | ||||
import cc.smtweb.system.bpm.web.sys.user.user.User; | import cc.smtweb.system.bpm.web.sys.user.user.User; | ||||
import cc.smtweb.system.bpm.web.sys.user.user.UserCache; | import cc.smtweb.system.bpm.web.sys.user.user.UserCache; | ||||
import cc.smtweb.system.bpm.web.sys.user.user.UserParty; | |||||
import cc.smtweb.system.bpm.web.sys.user.user.UserPartyCache; | |||||
import org.apache.commons.codec.digest.DigestUtils; | import org.apache.commons.codec.digest.DigestUtils; | ||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import java.util.List; | import java.util.List; | ||||
import java.util.Set; | |||||
/** | /** | ||||
* Created with IntelliJ IDEA. | * Created with IntelliJ IDEA. | ||||
@@ -73,7 +76,17 @@ public class LoginHelper { | |||||
if (user == null) { | if (user == null) { | ||||
throw new BizException("账号或者密码出错"); | throw new BizException("账号或者密码出错"); | ||||
} | } | ||||
return new UserSession(user.getId()); | |||||
return createUserSession(user); | |||||
} | |||||
public static UserSession createUserSession(User user){ | |||||
UserSession us = new UserSession(user.getId()); | |||||
Set<UserParty> ups = UserPartyCache.getInstance().getByUser(String.valueOf(us.getUserId())); | |||||
if(ups!=null&&ups.size()>0){ | |||||
UserParty up = ups.iterator().next(); | |||||
us.setPartyId(up.getPartyId()); | |||||
us.setDeptId(up.getDeptId()); | |||||
} | |||||
return us; | |||||
} | } | ||||
// public static UserSession login(LoginVO loginPO) { | // public static UserSession login(LoginVO loginPO) { | ||||
@@ -56,9 +56,9 @@ public class MenuHandler { | |||||
} | } | ||||
if (StringUtil.isNotEmpty(menu.getRight())) { | if (StringUtil.isNotEmpty(menu.getRight())) { | ||||
Map<String, List<String>> rightMap = JsonUtil.parseMap(menu.getRight()); | Map<String, List<String>> rightMap = JsonUtil.parseMap(menu.getRight()); | ||||
bean.put("sm_right_data", CommUtil.getSqlInStr(rightMap.get("data"))); | |||||
bean.put("sm_right_func", CommUtil.getSqlInStr(rightMap.get("func"))); | |||||
bean.put("sm_right_field", CommUtil.getSqlInStr(rightMap.get("field"))); | |||||
bean.put("sm_right_data", StringUtil.join(rightMap.get("data"), ",")); | |||||
bean.put("sm_right_func", StringUtil.join(rightMap.get("func"), ",")); | |||||
bean.put("sm_right_field", StringUtil.join(rightMap.get("field"), ",")); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -75,6 +75,7 @@ public class MenuPlanContent { | |||||
oldItem.setMenu(item.getMenu()); | oldItem.setMenu(item.getMenu()); | ||||
oldItem.setIcon(item.getIcon()); | oldItem.setIcon(item.getIcon()); | ||||
oldItem.setSeq(item.getSeq()); | oldItem.setSeq(item.getSeq()); | ||||
oldItem.setHome(item.getHome()); | |||||
} | } | ||||
} | } | ||||
@@ -122,7 +123,21 @@ public class MenuPlanContent { | |||||
MenuPlanItem parent = findById(parentId); | MenuPlanItem parent = findById(parentId); | ||||
return parent == null ? new ArrayList<>(): parent.getChildren(); | return parent == null ? new ArrayList<>(): parent.getChildren(); | ||||
} | } | ||||
// 获取主页 | |||||
public List<MenuPlanItem> getHomeList(){ | |||||
List<MenuPlanItem> homeList = new ArrayList<>(); | |||||
findHome(list,homeList); | |||||
return homeList; | |||||
} | |||||
private void findHome(List<MenuPlanItem> source,List<MenuPlanItem> homeList){ | |||||
if(CommUtil.isEmpty(source))return; | |||||
for(MenuPlanItem item: source){ | |||||
if(item.getHome() == 1){ | |||||
homeList.add(item); | |||||
} | |||||
findHome(item.getChildren(),homeList); | |||||
} | |||||
} | |||||
private MenuPlanItem findById(long id, List<MenuPlanItem> itemList) { | private MenuPlanItem findById(long id, List<MenuPlanItem> itemList) { | ||||
if (CommUtil.isEmpty(itemList)) { | if (CommUtil.isEmpty(itemList)) { | ||||
return null; | return null; | ||||
@@ -80,7 +80,19 @@ public class MenuPlanItem extends BaseBean { | |||||
public void setSeq(int seq) { | public void setSeq(int seq) { | ||||
put("seq", seq); | put("seq", seq); | ||||
} | } | ||||
/** | |||||
* 排序码 | |||||
*/ | |||||
public int getHome() { | |||||
return getInt("home"); | |||||
} | |||||
/** | |||||
* 排序码 | |||||
*/ | |||||
public void setHome(int home) { | |||||
put("home", home); | |||||
} | |||||
public void setChildren(List<MenuPlanItem> children) { | public void setChildren(List<MenuPlanItem> children) { | ||||
put("children", children); | put("children", children); | ||||
} | } | ||||
@@ -4,6 +4,7 @@ import cc.smtweb.framework.core.annotation.SwCache; | |||||
import cc.smtweb.framework.core.cache.AbstractEntityCache; | import cc.smtweb.framework.core.cache.AbstractEntityCache; | ||||
import cc.smtweb.framework.core.cache.CacheManager; | import cc.smtweb.framework.core.cache.CacheManager; | ||||
import cc.smtweb.framework.core.util.CommUtil; | import cc.smtweb.framework.core.util.CommUtil; | ||||
import cc.smtweb.framework.core.util.StringUtil; | |||||
import java.util.HashSet; | import java.util.HashSet; | ||||
import java.util.Set; | import java.util.Set; | ||||
@@ -36,6 +37,6 @@ public class RoleCache extends AbstractEntityCache<Role> { | |||||
key.forEach(k -> { | key.forEach(k -> { | ||||
names.add(get(k).getName()); | names.add(get(k).getName()); | ||||
}); | }); | ||||
return CommUtil.getSqlInStr(names); | |||||
return StringUtil.join(names,","); | |||||
} | } | ||||
} | } |
@@ -59,6 +59,7 @@ public class RoleHandler { | |||||
//if (CommUtil.isEmpty(set)) return R.error("未选择菜单!"); | //if (CommUtil.isEmpty(set)) return R.error("未选择菜单!"); | ||||
Role role = RoleCache.getInstance().get(role_id); | Role role = RoleCache.getInstance().get(role_id); | ||||
RoleRightContent rc = new RoleRightContent(role.getPrivilege()); | RoleRightContent rc = new RoleRightContent(role.getPrivilege()); | ||||
rc.resetItem(); | |||||
for (Long menu_id : set) { | for (Long menu_id : set) { | ||||
MenuPlanItem item = MenuPlanCache.getInstance().getById(role.getSmpId(), menu_id); | MenuPlanItem item = MenuPlanCache.getInstance().getById(role.getSmpId(), menu_id); | ||||
RoleRight right = rc.getRoleRight(item.getId()); | RoleRight right = rc.getRoleRight(item.getId()); | ||||
@@ -77,8 +78,8 @@ public class RoleHandler { | |||||
right = new RoleRight(); | right = new RoleRight(); | ||||
right.setId(item.getId()); | right.setId(item.getId()); | ||||
right.setMenuId(item.getMenu()); | right.setMenuId(item.getMenu()); | ||||
right.setMenu(1); | |||||
} | } | ||||
right.setMenu(1); | |||||
right.setFunc(formData.readStringSet("func")); | right.setFunc(formData.readStringSet("func")); | ||||
right.setData(formData.readListMap("data")); | right.setData(formData.readListMap("data")); | ||||
if (item.getMenu() > 0L) | if (item.getMenu() > 0L) | ||||
@@ -0,0 +1,130 @@ | |||||
package cc.smtweb.system.bpm.web.sys.user.role; | |||||
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlan; | |||||
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanCache; | |||||
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanContent; | |||||
import cc.smtweb.system.bpm.web.sys.user.menuPlan.MenuPlanItem; | |||||
import cc.smtweb.system.bpm.web.sys.user.user.User; | |||||
import cc.smtweb.system.bpm.web.sys.user.user.UserCache; | |||||
import cc.smtweb.system.bpm.web.sys.user.user.UserRoleCache; | |||||
import java.util.HashSet; | |||||
import java.util.List; | |||||
import java.util.Set; | |||||
/** | |||||
* @Author: tanghp | |||||
* @Date: 2022-09-06 18:35 | |||||
* @Desc: 角色辅助类 | |||||
*/ | |||||
public final class RoleHelper { | |||||
public static boolean isAdmin(long userId){ | |||||
if(userId ==1L) return true; | |||||
User user = UserCache.getInstance().get(userId); | |||||
if(user!=null){ | |||||
return "admin".equalsIgnoreCase(user.getNicky()); | |||||
} | |||||
return false; | |||||
} | |||||
/** | |||||
* 获取角色权限 | |||||
* @param userId 用户ID | |||||
* @param menuPlanId 菜单方案ID | |||||
* @return | |||||
*/ | |||||
public static List<RoleRight> getRoleRightList(long userId, long menuPlanId){ | |||||
return null; | |||||
} | |||||
/** | |||||
* 获取用户的菜单权限 | |||||
* @param userId 用户ID | |||||
* @param partyId 机构ID | |||||
* @param incParent 是否包含父节点,(子节点配置了,把父节点也查出来) | |||||
* @return | |||||
*/ | |||||
public static Set<Long> getRoleMenuIds(long userId,long partyId,boolean incParent){ | |||||
Set<Long> mpIds = getMenuPlanIds(userId,partyId); | |||||
if(mpIds.size()==0){ | |||||
return new HashSet<>(); | |||||
} | |||||
return getRoleMenuIds(userId,partyId,mpIds.iterator().next(),incParent); | |||||
} | |||||
/** | |||||
* 获取用户的菜单权限 | |||||
* @param userId 用户ID | |||||
* @param partyId 机构ID | |||||
* @param menuPlanId 菜单方案ID | |||||
* @param incParent 是否包含父节点,(子节点配置了,把父节点也查出来) | |||||
* @return | |||||
*/ | |||||
public static Set<Long> getRoleMenuIds(long userId,long partyId, long menuPlanId,boolean incParent){ | |||||
Set<Long> menuIds = new HashSet<>(); | |||||
MenuPlan menuPlan = MenuPlanCache.getInstance().get(menuPlanId); | |||||
if(menuPlan==null)return menuIds; | |||||
Set<Long> roleIds = UserRoleCache.getInstance().getRoleIdByUP(userId,partyId); | |||||
RoleCache roleCache = RoleCache.getInstance(); | |||||
roleIds.forEach((roleId)-> { | |||||
Role role = roleCache.get(roleId); | |||||
if(role==null || role.getSmpId()!=menuPlanId){ | |||||
return; | |||||
} | |||||
RoleRightContent roleRightContent = new RoleRightContent(role.getPrivilege()); | |||||
menuIds.addAll(roleRightContent.getMenuRightIds()); | |||||
}); | |||||
MenuPlanContent menuPlanContent = new MenuPlanContent(menuPlan.getContent()); | |||||
if(incParent){ | |||||
Set<Long> tempIds = new HashSet<>(menuIds); | |||||
tempIds.forEach(id->{ | |||||
fillMenuId(id,menuPlanContent,menuIds); | |||||
}); | |||||
} | |||||
return menuIds; | |||||
} | |||||
private static void fillMenuId(Long id,MenuPlanContent menuPlanContent, Set<Long> menuIds){ | |||||
menuIds.add(id); | |||||
MenuPlanItem parent = menuPlanContent.findParentById(id); | |||||
if(parent!=null){ | |||||
if(!menuIds.contains(parent.getId())){ | |||||
fillMenuId(parent.getId(),menuPlanContent,menuIds); | |||||
} | |||||
} | |||||
} | |||||
/** | |||||
* 获取用户的菜单方案 | |||||
* @param userId 用户ID | |||||
* @param partyId 机构ID | |||||
* @return 菜单方案ID | |||||
*/ | |||||
public static Set<Long> getMenuPlanIds(long userId,long partyId){ | |||||
Set<Long> mpList = new HashSet<>(); | |||||
Set<Long> roleIds = UserRoleCache.getInstance().getRoleIdByUP(userId,partyId); | |||||
RoleCache roleCache = RoleCache.getInstance(); | |||||
roleIds.forEach((roleId)-> { | |||||
Role role = roleCache.get(roleId); | |||||
if(role!=null&&role.getSmpId()>0L){ | |||||
mpList.add(role.getSmpId()); | |||||
} | |||||
}); | |||||
return mpList; | |||||
} | |||||
/** | |||||
* 获取用户的菜单方案 | |||||
* @param userId 用户ID | |||||
* @param partyId 机构ID | |||||
* @return 菜单方案 | |||||
*/ | |||||
public static Set<MenuPlan> getMenuPlans(long userId,long partyId){ | |||||
Set<MenuPlan> mpList = new HashSet<>(); | |||||
Set<Long> roleIds = UserRoleCache.getInstance().getRoleIdByUP(userId,partyId); | |||||
roleIds.forEach((roleId)-> { | |||||
Role role = RoleCache.getInstance().get(roleId); | |||||
if(role!=null&&role.getSmpId()>0L){ | |||||
MenuPlan mp = MenuPlanCache.getInstance().get(role.getSmpId()); | |||||
if(mp!=null){ | |||||
mpList.add(mp); | |||||
} | |||||
} | |||||
}); | |||||
return mpList; | |||||
} | |||||
} |
@@ -61,7 +61,7 @@ public class RoleRightContent { | |||||
public String getFuncRight(long key) { | public String getFuncRight(long key) { | ||||
RoleRight right = getRoleRight(key); | RoleRight right = getRoleRight(key); | ||||
if (right == null) return ""; | if (right == null) return ""; | ||||
return CommUtil.getSqlInStr(right.getRightFunc()); | |||||
return StringUtil.join(right.getRightFunc(),","); | |||||
} | } | ||||
public String getMenuRight() { | public String getMenuRight() { | ||||
@@ -70,9 +70,16 @@ public class RoleRightContent { | |||||
map.values().forEach(roleRight -> { | map.values().forEach(roleRight -> { | ||||
if (roleRight.getMenu() == 1) menus.add(roleRight.getId() + ""); | if (roleRight.getMenu() == 1) menus.add(roleRight.getId() + ""); | ||||
}); | }); | ||||
return CommUtil.getSqlInStr(menus); | |||||
return StringUtil.join(menus,","); | |||||
} | |||||
public Set<Long> getMenuRightIds() { | |||||
Set<Long> ids = new HashSet<>(); | |||||
if (map.size() == 0) return ids; | |||||
map.values().forEach(roleRight -> { | |||||
if (roleRight.getMenu() == 1) ids.add(roleRight.getId()); | |||||
}); | |||||
return ids; | |||||
} | } | ||||
public String getFieldRight(long key, int type) { | public String getFieldRight(long key, int type) { | ||||
RoleRight right = getRoleRight(key); | RoleRight right = getRoleRight(key); | ||||
if (right == null) return ""; | if (right == null) return ""; | ||||
@@ -82,7 +89,7 @@ public class RoleRightContent { | |||||
cache.forEach((k, v) -> { | cache.forEach((k, v) -> { | ||||
if (v == type) fields.add(k); | if (v == type) fields.add(k); | ||||
}); | }); | ||||
return CommUtil.getSqlInStr(fields); | |||||
return StringUtil.join(fields,","); | |||||
} | } | ||||
public Map<String, Object> getDataRight(long key) { | public Map<String, Object> getDataRight(long key) { | ||||
@@ -1,7 +1,15 @@ | |||||
package cc.smtweb.framework.core; | package cc.smtweb.framework.core; | ||||
import cc.smtweb.framework.core.cache.redis.RedisManager; | |||||
import cc.smtweb.framework.core.common.SwConsts; | import cc.smtweb.framework.core.common.SwConsts; | ||||
import cc.smtweb.framework.core.exception.BizException; | import cc.smtweb.framework.core.exception.BizException; | ||||
import cc.smtweb.framework.core.mvc.controller.IEditor; | |||||
import cc.smtweb.framework.core.mvc.realm.exception.ForbiddenException; | |||||
import cc.smtweb.framework.core.mvc.realm.exception.UnauthenticatedException; | |||||
import cc.smtweb.framework.core.session.SessionUtil; | |||||
import cc.smtweb.framework.core.session.UserSession; | |||||
import cc.smtweb.framework.core.util.NumberUtil; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
import org.springframework.web.servlet.HandlerInterceptor; | import org.springframework.web.servlet.HandlerInterceptor; | ||||
import org.springframework.web.servlet.ModelAndView; | import org.springframework.web.servlet.ModelAndView; | ||||
@@ -15,6 +23,7 @@ public class CoreInterceptor implements HandlerInterceptor { | |||||
@Override | @Override | ||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | ||||
if (!SwConsts.SysParam.SYS_STARTED) throw new BizException("系统启动中,请稍候..."); | if (!SwConsts.SysParam.SYS_STARTED) throw new BizException("系统启动中,请稍候..."); | ||||
SessionUtil.checkSession(request); | |||||
return HandlerInterceptor.super.preHandle(request, response, handler); | return HandlerInterceptor.super.preHandle(request, response, handler); | ||||
} | } | ||||
@@ -1,26 +0,0 @@ | |||||
package cc.smtweb.framework.core.annotation; | |||||
import java.lang.annotation.ElementType; | |||||
import java.lang.annotation.Retention; | |||||
import java.lang.annotation.RetentionPolicy; | |||||
import java.lang.annotation.Target; | |||||
/** | |||||
* 被该注释修饰的方法都会经过切面拦截校验权限,默认是需要已登录权限 | |||||
* | |||||
* @author kevin | |||||
*/ | |||||
@Retention(RetentionPolicy.RUNTIME) | |||||
@Target({ElementType.METHOD, ElementType.TYPE}) | |||||
public @interface SwPerm { | |||||
/** | |||||
* 无权限控制的值,在函数上注解@SwPerm(SwPerm.NONE) | |||||
*/ | |||||
static final String NONE = "*"; | |||||
static final String SESSION = ""; | |||||
/** | |||||
* 权限定义值 | |||||
*/ | |||||
String value() default SESSION; | |||||
} |
@@ -8,7 +8,7 @@ import java.util.concurrent.ConcurrentHashMap; | |||||
*/ | */ | ||||
public class SessionCacheFactory { | public class SessionCacheFactory { | ||||
private static SessionCacheFactory INSTANCE = null; | private static SessionCacheFactory INSTANCE = null; | ||||
private Map<Long, SessionCache> buffer = new ConcurrentHashMap<>(); | |||||
private Map<String, SessionCache> buffer = new ConcurrentHashMap<>(); | |||||
private SessionCacheFactory() { | private SessionCacheFactory() { | ||||
} | } | ||||
@@ -27,10 +27,10 @@ public class SessionCacheFactory { | |||||
//得到用户缓存对象 | //得到用户缓存对象 | ||||
public SessionCache getUserCache(long userId) { | public SessionCache getUserCache(long userId) { | ||||
return getUserCache(userId, 0L); | |||||
return getCache(String.valueOf(userId), 0L); | |||||
} | } | ||||
public SessionCache getUserCache(long userId, long timeout) { | |||||
public SessionCache getCache(String userId, long timeout) { | |||||
SessionCache cache; | SessionCache cache; | ||||
cache = buffer.get(userId); | cache = buffer.get(userId); | ||||
@@ -41,11 +41,12 @@ public class SessionCacheFactory { | |||||
buffer.put(userId, cache); | buffer.put(userId, cache); | ||||
} | } | ||||
return cache; | return cache; | ||||
} | } | ||||
//删除用户缓存 | //删除用户缓存 | ||||
public void remove(String userId) { | public void remove(String userId) { | ||||
buffer.remove(userId); | buffer.remove(userId); | ||||
} | } | ||||
} | } |
@@ -38,8 +38,6 @@ public class RedisSysTask implements ISysTask { | |||||
RedisBroadcastEvent redisBroadcast = JsonUtil.parse(message, RedisBroadcastEvent.class); | RedisBroadcastEvent redisBroadcast = JsonUtil.parse(message, RedisBroadcastEvent.class); | ||||
applicationContext.publishEvent(redisBroadcast); | applicationContext.publishEvent(redisBroadcast); | ||||
// log.debug("Redis [" + channel + "] message:" + redisBroadcast); | |||||
} | } | ||||
}); | }); | ||||
@@ -13,8 +13,17 @@ public interface SwConsts { | |||||
public static String RUN_PROJECTS = ""; | public static String RUN_PROJECTS = ""; | ||||
} | } | ||||
//错误码 | |||||
interface ErrorCode { | |||||
//未登录 | |||||
int NO_LOGIN = 101; | |||||
//指纹错误 | |||||
int TOKEN_INVALID = 102; | |||||
} | |||||
//启动顺序默认值 | |||||
int DEFAULT_ORDER = 1; | int DEFAULT_ORDER = 1; | ||||
//缓存中:树节点按parent的key | //缓存中:树节点按parent的key | ||||
String KEY_PARENT_ID = "pr"; | String KEY_PARENT_ID = "pr"; | ||||
//级次码、字符串连接符 | //级次码、字符串连接符 | ||||
@@ -9,6 +9,7 @@ import cc.smtweb.framework.core.db.EntityDao; | |||||
import cc.smtweb.framework.core.db.vo.ModelDatabase; | import cc.smtweb.framework.core.db.vo.ModelDatabase; | ||||
import cc.smtweb.framework.core.db.vo.ModelProject; | import cc.smtweb.framework.core.db.vo.ModelProject; | ||||
import cc.smtweb.framework.core.util.CommUtil; | import cc.smtweb.framework.core.util.CommUtil; | ||||
import cc.smtweb.framework.core.util.StringUtil; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import java.util.List; | import java.util.List; | ||||
@@ -47,6 +48,6 @@ public class ModelDatabaseCache extends AbstractCache<ModelDatabase> { | |||||
public final String getName(long id) { | public final String getName(long id) { | ||||
ModelDatabase db = get(id); | ModelDatabase db = get(id); | ||||
return db != null ? db.getName(): ""; | |||||
return db != null ? db.getName() : ""; | |||||
} | } | ||||
} | } |
@@ -10,6 +10,8 @@ public class BizException extends RuntimeException { | |||||
* | * | ||||
*/ | */ | ||||
private static final long serialVersionUID = 1L; | private static final long serialVersionUID = 1L; | ||||
//错误编号 | |||||
private int code = 0; | |||||
public BizException() { | public BizException() { | ||||
super(); | super(); | ||||
@@ -23,7 +25,20 @@ public class BizException extends RuntimeException { | |||||
super(message); | super(message); | ||||
} | } | ||||
public BizException(int code) { | |||||
super(); | |||||
this.code = code; | |||||
} | |||||
public BizException(int code, String message) { | |||||
super(message); | |||||
this.code = code; | |||||
} | |||||
public BizException(Throwable cause) { | public BizException(Throwable cause) { | ||||
super(cause.getMessage(), cause); | super(cause.getMessage(), cause); | ||||
} | } | ||||
public int getCode() { | |||||
return code; | |||||
} | |||||
} | } |
@@ -0,0 +1,29 @@ | |||||
package cc.smtweb.framework.core.exception; | |||||
import cc.smtweb.framework.core.common.R; | |||||
import org.springframework.web.bind.annotation.ControllerAdvice; | |||||
import org.springframework.web.bind.annotation.ExceptionHandler; | |||||
import org.springframework.web.bind.annotation.ResponseBody; | |||||
/** | |||||
* Created by Akmm at 2022-09-07 20:13 | |||||
* 异常统一处理 | |||||
*/ | |||||
@ControllerAdvice | |||||
public class SwExceptionHandler { | |||||
/** | |||||
* 系统异常处理 | |||||
* | |||||
* @param e | |||||
* @return | |||||
*/ | |||||
@ExceptionHandler(Exception.class) | |||||
@ResponseBody | |||||
public R error(Exception e) { | |||||
if (e instanceof BizException) { | |||||
return R.error(((BizException) e).getCode(), e.getMessage()); | |||||
} | |||||
e.printStackTrace(); | |||||
return R.error("系统异常,请联系技术支持人员!"); | |||||
} | |||||
} |
@@ -64,7 +64,7 @@ public class WebMvcConfig implements WebMvcConfigurer { | |||||
@Bean | @Bean | ||||
public MethodAccessManager methodAccessManager(CacheManager cacheManager) { | public MethodAccessManager methodAccessManager(CacheManager cacheManager) { | ||||
return new MethodAccessManager(redisManager, cacheManager); | |||||
return new MethodAccessManager(cacheManager); | |||||
} | } | ||||
@Override | @Override | ||||
@@ -8,7 +8,6 @@ import cc.smtweb.framework.core.mvc.SchedulerManager; | |||||
import cc.smtweb.framework.core.mvc.controller.access.IMethodAccess; | import cc.smtweb.framework.core.mvc.controller.access.IMethodAccess; | ||||
import cc.smtweb.framework.core.mvc.controller.access.MethodAccess; | import cc.smtweb.framework.core.mvc.controller.access.MethodAccess; | ||||
import cc.smtweb.framework.core.mvc.controller.scan.BeanManager; | import cc.smtweb.framework.core.mvc.controller.scan.BeanManager; | ||||
import cc.smtweb.framework.core.mvc.realm.interceptor.PermInterceptor; | |||||
import cc.smtweb.framework.core.mvc.realm.service.PermChecker; | import cc.smtweb.framework.core.mvc.realm.service.PermChecker; | ||||
import cc.smtweb.framework.core.mvc.scheduler.SchedulerTaskManager; | import cc.smtweb.framework.core.mvc.scheduler.SchedulerTaskManager; | ||||
import lombok.Getter; | import lombok.Getter; | ||||
@@ -27,15 +26,13 @@ import java.util.Map; | |||||
public class MethodAccessManager { | public class MethodAccessManager { | ||||
private Map<String, IMethodAccess> controllers; | private Map<String, IMethodAccess> controllers; | ||||
private IBeanContext beanContext; | private IBeanContext beanContext; | ||||
private PermInterceptor permInterceptor; | |||||
private SchedulerTaskManager schedulerTaskManager; | private SchedulerTaskManager schedulerTaskManager; | ||||
private MethodAccess[] destroyMethods; | private MethodAccess[] destroyMethods; | ||||
@Getter | @Getter | ||||
private CacheManager cacheManager; | private CacheManager cacheManager; | ||||
public MethodAccessManager(RedisManager redisManager, CacheManager cacheManager) { | |||||
permInterceptor = new PermInterceptor(redisManager); | |||||
public MethodAccessManager(CacheManager cacheManager) { | |||||
this.cacheManager = cacheManager; | this.cacheManager = cacheManager; | ||||
} | } | ||||
@@ -47,8 +44,6 @@ public class MethodAccessManager { | |||||
IMethodAccess methodAccess = controllers.get(url); | IMethodAccess methodAccess = controllers.get(url); | ||||
if (methodAccess != null) { | if (methodAccess != null) { | ||||
permInterceptor.preHandle(request, methodAccess.getPerm()); | |||||
return methodAccess.invoke(beanContext, params, body, request); | return methodAccess.invoke(beanContext, params, body, request); | ||||
} | } | ||||
@@ -71,7 +66,6 @@ public class MethodAccessManager { | |||||
this.beanContext = beanManager.getBeanContext(); | this.beanContext = beanManager.getBeanContext(); | ||||
this.controllers = beanManager.getControllers(); | this.controllers = beanManager.getControllers(); | ||||
this.destroyMethods = beanManager.loadDestroyMethods(); | this.destroyMethods = beanManager.loadDestroyMethods(); | ||||
this.permInterceptor.setCache(cache); | |||||
// 启动定时任务 | // 启动定时任务 | ||||
this.schedulerTaskManager = SchedulerTaskManager.build(beanContext, beanManager.getTasks()); | this.schedulerTaskManager = SchedulerTaskManager.build(beanContext, beanManager.getTasks()); | ||||
@@ -0,0 +1,34 @@ | |||||
package cc.smtweb.framework.core.mvc.controller; | |||||
import cc.smtweb.framework.core.common.R; | |||||
import cc.smtweb.framework.core.session.SessionUtil; | |||||
import org.springframework.core.MethodParameter; | |||||
import org.springframework.http.MediaType; | |||||
import org.springframework.http.server.ServerHttpRequest; | |||||
import org.springframework.http.server.ServerHttpResponse; | |||||
import org.springframework.web.bind.annotation.ControllerAdvice; | |||||
import org.springframework.web.context.request.RequestContextHolder; | |||||
import org.springframework.web.context.request.ServletRequestAttributes; | |||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | |||||
/** | |||||
* Created by Akmm at 2022-09-08 16:03 | |||||
* 统一添加指纹 | |||||
*/ | |||||
@ControllerAdvice | |||||
public class SwResponseBodyAdvice implements ResponseBodyAdvice { | |||||
@Override | |||||
public boolean supports(MethodParameter returnType, Class converterType) { | |||||
Class targetClass = returnType.getMethod().getDeclaringClass(); | |||||
return true; | |||||
} | |||||
@Override | |||||
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { | |||||
if (body instanceof R) { | |||||
SessionUtil.setFingerValue(((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(), (R)body); | |||||
} | |||||
return body; | |||||
} | |||||
} |
@@ -46,10 +46,4 @@ public interface IMethodAccess { | |||||
*/ | */ | ||||
String fullName(); | String fullName(); | ||||
/** | |||||
* API权限 | |||||
* | |||||
* @return 权限串 | |||||
*/ | |||||
String getPerm(); | |||||
} | } |
@@ -22,14 +22,12 @@ import java.util.Map; | |||||
public class MethodAccess implements IMethodAccess { | public class MethodAccess implements IMethodAccess { | ||||
private final ControllerAccess controllerAccess; | private final ControllerAccess controllerAccess; | ||||
private final Method method; | private final Method method; | ||||
@Getter | |||||
private final String perm; | |||||
private final MethodParamAccess[] paramBinds; | private final MethodParamAccess[] paramBinds; | ||||
public MethodAccess(ControllerAccess controllerAccess, Method method, String perm, MethodParamAccess[] paramBinds) { | |||||
public MethodAccess(ControllerAccess controllerAccess, Method method, MethodParamAccess[] paramBinds) { | |||||
this.controllerAccess = controllerAccess; | this.controllerAccess = controllerAccess; | ||||
this.method = method; | this.method = method; | ||||
this.perm = perm; | |||||
this.paramBinds = paramBinds; | this.paramBinds = paramBinds; | ||||
} | } | ||||
@@ -35,20 +35,13 @@ public class MethodParser { | |||||
public void parse(Class<?> clazz, ControllerAccess controllerAccess, boolean isApi) throws ParseException { | public void parse(Class<?> clazz, ControllerAccess controllerAccess, boolean isApi) throws ParseException { | ||||
// this.controllerAccess = controllerAccess; | // this.controllerAccess = controllerAccess; | ||||
// 服务的默认权限 | |||||
String classPerm = ""; | |||||
SwPerm swPerm = clazz.getAnnotation(SwPerm.class); | |||||
if (swPerm != null) { | |||||
classPerm = swPerm.value(); | |||||
} | |||||
// 扫描方法注解 | // 扫描方法注解 | ||||
for (Method m : clazz.getMethods()) { | for (Method m : clazz.getMethods()) { | ||||
int modifier = m.getModifiers(); | int modifier = m.getModifiers(); | ||||
// && (R.class.isAssignableFrom(m.getReturnType())) | // && (R.class.isAssignableFrom(m.getReturnType())) | ||||
if (Modifier.isPublic(modifier) && !Modifier.isStatic(modifier) && !Object.class.equals(m.getDeclaringClass())) { | if (Modifier.isPublic(modifier) && !Modifier.isStatic(modifier) && !Object.class.equals(m.getDeclaringClass())) { | ||||
MethodAccess methodAccess = parseMethod(controllerAccess, m, classPerm); | |||||
MethodAccess methodAccess = parseMethod(controllerAccess, m); | |||||
if (scanContext.dealMethod(m, methodAccess)) { | if (scanContext.dealMethod(m, methodAccess)) { | ||||
if (isApi) { | if (isApi) { | ||||
@@ -81,7 +74,7 @@ public class MethodParser { | |||||
} | } | ||||
} | } | ||||
private MethodAccess parseMethod(ControllerAccess controllerAccess, Method method, String defaultPerm) { | |||||
private MethodAccess parseMethod(ControllerAccess controllerAccess, Method method) { | |||||
Class<?>[] paramTypes = method.getParameterTypes(); | Class<?>[] paramTypes = method.getParameterTypes(); | ||||
Annotation[][] paramAnnotations = method.getParameterAnnotations(); | Annotation[][] paramAnnotations = method.getParameterAnnotations(); | ||||
@@ -146,14 +139,7 @@ public class MethodParser { | |||||
} | } | ||||
} | } | ||||
// 得到权限注解 | |||||
String perm = defaultPerm; | |||||
SwPerm swPerm = method.getAnnotation(SwPerm.class); | |||||
if (swPerm != null) { | |||||
perm = swPerm.value(); | |||||
} | |||||
return new MethodAccess(controllerAccess, method, perm, paramBinds); | |||||
return new MethodAccess(controllerAccess, method, paramBinds); | |||||
} | } | ||||
} | } |
@@ -1,110 +0,0 @@ | |||||
package cc.smtweb.framework.core.mvc.realm.interceptor; | |||||
import cc.smtweb.framework.core.annotation.SwPerm; | |||||
import cc.smtweb.framework.core.cache.ISwCache; | |||||
import cc.smtweb.framework.core.cache.redis.RedisManager; | |||||
import cc.smtweb.framework.core.mvc.controller.IEditor; | |||||
import cc.smtweb.framework.core.mvc.realm.exception.ForbiddenException; | |||||
import cc.smtweb.framework.core.mvc.realm.exception.UnauthenticatedException; | |||||
import cc.smtweb.framework.core.mvc.realm.service.PermCheckItem; | |||||
import cc.smtweb.framework.core.mvc.realm.service.PermChecker; | |||||
import cc.smtweb.framework.core.session.SessionUtil; | |||||
import cc.smtweb.framework.core.session.UserSession; | |||||
import org.apache.commons.lang3.StringUtils; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
public class AbstractPermInterceptor { | |||||
private final RedisManager redisManager; | |||||
private ISwCache<Long, PermChecker> cacheService; | |||||
public AbstractPermInterceptor(RedisManager redisManager) { | |||||
this.redisManager = redisManager; | |||||
} | |||||
public void setCache(ISwCache<Long, PermChecker> cacheService) { | |||||
this.cacheService = cacheService; | |||||
} | |||||
protected boolean handle(HttpServletRequest request, String permissionValue) { | |||||
// 如果注解为null, 说明不需要拦截, 直接放过 | |||||
if (SwPerm.NONE.equals(permissionValue)) { | |||||
return true; | |||||
} | |||||
// redis读取session,判断是否登录 | |||||
String token = SessionUtil.readToken(request); | |||||
if ("design".equals(token)) return true; | |||||
if (StringUtils.isBlank(token)) { | |||||
throw new UnauthenticatedException("not find Auth-Token in header"); | |||||
} | |||||
UserSession us = redisManager.get(token, UserSession.class); | |||||
if (us == null) { | |||||
throw new UnauthenticatedException("not find UserSession by token: " + token); | |||||
} | |||||
request.setAttribute(IEditor.USER_TOKEN, token); | |||||
request.setAttribute(IEditor.USER_SESSION, us); | |||||
// 如果标记了权限注解,则判断权限 | |||||
if (checkPermission(permissionValue, us)) { | |||||
// 更新Token redis TTL | |||||
redisManager.expire(token, RedisManager.SESSION_EXPIRE_SEC); | |||||
return true; | |||||
} else { | |||||
throw new ForbiddenException("user not permission: " + permissionValue); | |||||
} | |||||
} | |||||
/** | |||||
* 权限检查 | |||||
*/ | |||||
private boolean checkPermission(String permissionValue, UserSession us) { | |||||
if (StringUtils.isBlank(permissionValue)) { | |||||
return true; | |||||
} | |||||
// 从本地缓存或数据库中获取该用户的权限信息 | |||||
PermChecker permissionSet = cacheService.get(us.getUserId()); | |||||
// if (MapUtils.isEmpty(permissionSet)) { | |||||
// throw new ForbiddenException("empty permission"); | |||||
// } | |||||
PermCheckItem permChecker = permissionSet.get(permissionValue); | |||||
if (permChecker != null) { | |||||
return true; | |||||
} | |||||
while (true) { | |||||
permissionValue = getParentPermValue(permissionValue); | |||||
if (permissionValue != null) { | |||||
permChecker = permissionSet.get(permissionValue); | |||||
if (permChecker != null && permChecker.isPerfixMath()) { | |||||
return true; | |||||
} | |||||
} else { | |||||
break; | |||||
} | |||||
} | |||||
return false; | |||||
} | |||||
private static String getParentPermValue(String permissionValue) { | |||||
if (permissionValue.length() > 0) { | |||||
int pos = permissionValue.lastIndexOf(':'); | |||||
if (pos > 0) { | |||||
return permissionValue.substring(0, pos); | |||||
} | |||||
return ""; | |||||
} | |||||
return null; | |||||
} | |||||
} |
@@ -1,42 +0,0 @@ | |||||
package cc.smtweb.framework.core.mvc.realm.interceptor; | |||||
import cc.smtweb.framework.core.annotation.SwPerm; | |||||
import cc.smtweb.framework.core.cache.redis.RedisManager; | |||||
import org.springframework.web.method.HandlerMethod; | |||||
import org.springframework.web.servlet.HandlerInterceptor; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
import javax.servlet.http.HttpServletResponse; | |||||
/** | |||||
* 暂时未使用,spring的拦截器方式判断权限 | |||||
*/ | |||||
public class AuthorizationInterceptor extends AbstractPermInterceptor implements HandlerInterceptor { | |||||
public AuthorizationInterceptor(RedisManager redisManager) { | |||||
super(redisManager); | |||||
} | |||||
@Override | |||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | |||||
throws Exception { | |||||
if (handler instanceof HandlerMethod) { | |||||
HandlerMethod handlerMethod = (HandlerMethod) handler; | |||||
// 获取方法上的注解 | |||||
SwPerm requiredSwPerm = handlerMethod.getMethod().getAnnotation(SwPerm.class); | |||||
// 如果方法上的注解为空 则获取类的注解 | |||||
if (requiredSwPerm == null) { | |||||
requiredSwPerm = handlerMethod.getMethod().getDeclaringClass().getAnnotation(SwPerm.class); | |||||
} | |||||
String requiredValue = null; | |||||
if (requiredSwPerm != null) { | |||||
requiredValue = requiredSwPerm.value(); | |||||
} | |||||
return super.handle(request, requiredValue); | |||||
} | |||||
return true; | |||||
} | |||||
} |
@@ -1,27 +0,0 @@ | |||||
package cc.smtweb.framework.core.mvc.realm.interceptor; | |||||
import cc.smtweb.framework.core.cache.redis.RedisManager; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
/** | |||||
* 权限拦截器,在API请求处理中一起完成 | |||||
* | |||||
* @author xkliu | |||||
*/ | |||||
public class PermInterceptor extends AbstractPermInterceptor { | |||||
public PermInterceptor(RedisManager redisManager) { | |||||
super(redisManager); | |||||
} | |||||
/** | |||||
* 校验用户是否有API权限 | |||||
* | |||||
* @param request http请求 | |||||
* @param permissionValue 权限值 | |||||
* @return 是否有权限 | |||||
*/ | |||||
public boolean preHandle(HttpServletRequest request, String permissionValue) { | |||||
return super.handle(request, permissionValue); | |||||
} | |||||
} |
@@ -1,12 +1,22 @@ | |||||
package cc.smtweb.framework.core.session; | package cc.smtweb.framework.core.session; | ||||
import cc.smtweb.framework.core.cache.SessionCache; | |||||
import cc.smtweb.framework.core.cache.SessionCacheFactory; | |||||
import cc.smtweb.framework.core.cache.redis.RedisManager; | import cc.smtweb.framework.core.cache.redis.RedisManager; | ||||
import cc.smtweb.framework.core.common.R; | |||||
import cc.smtweb.framework.core.common.SwConsts; | |||||
import cc.smtweb.framework.core.exception.BizException; | |||||
import cc.smtweb.framework.core.mvc.controller.IEditor; | import cc.smtweb.framework.core.mvc.controller.IEditor; | ||||
import cc.smtweb.framework.core.mvc.realm.exception.UnauthenticatedException; | import cc.smtweb.framework.core.mvc.realm.exception.UnauthenticatedException; | ||||
import cc.smtweb.framework.core.util.NumberUtil; | |||||
import cc.smtweb.framework.core.util.StringUtil; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import org.springframework.web.context.request.RequestContextHolder; | import org.springframework.web.context.request.RequestContextHolder; | ||||
import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
import java.util.ArrayList; | |||||
import java.util.List; | |||||
import java.util.concurrent.atomic.AtomicInteger; | |||||
/** | /** | ||||
* 〈session工具类〉 | * 〈session工具类〉 | ||||
@@ -15,6 +25,96 @@ import javax.servlet.http.HttpServletRequest; | |||||
* @since 1.0.0 | * @since 1.0.0 | ||||
*/ | */ | ||||
public class SessionUtil { | public class SessionUtil { | ||||
//登录令牌header名 | |||||
private final static String KEY_HEADER_SESSION = "Auth-Token"; | |||||
//url参数名 | |||||
private final static String KEY_PARAM_SESSION = "auth_token"; | |||||
//防止重复提交指纹-header名 | |||||
private final static String KEY_HEADER_FP_KEY = "Fpk_Token"; | |||||
private final static String KEY_HEADER_FP_VAL = "Fpv_Token"; | |||||
//url参数名 | |||||
private final static String KEY_PARAM_FP_KEY = "fpk"; | |||||
private final static String KEY_PARAM_FP_VAL = "fpv"; | |||||
//不需要校验登录的url | |||||
public static List<String> notLoginUrl = new ArrayList<>(); | |||||
/*//不需要切换数据源的url,强制用主库 | |||||
public static List<String> notSetDbUrl = new ArrayList<>();*/ | |||||
//不需要验证合法性的url | |||||
public static List<String> notCheckUrlValid = new ArrayList<>(); | |||||
//不算用户交互操作的url,用于管理session失效 | |||||
public static List<String> notInterActiveUrl = new ArrayList<>(); | |||||
static { | |||||
notLoginUrl.add("/api/getVerifyCode"); | |||||
notLoginUrl.add("/api/bpm/auth/login"); | |||||
notLoginUrl.add("/fs/static/*"); | |||||
notLoginUrl.add("/custdb/*.do"); | |||||
notLoginUrl.add("/attach/upload.do"); | |||||
notLoginUrl.add("/attach/download.do"); | |||||
notLoginUrl.add("/attach/uploadBase64.do"); | |||||
notCheckUrlValid.add("/db/*.do"); | |||||
notCheckUrlValid.add("/custdb/*.do"); | |||||
notCheckUrlValid.add("/cust/common/httpService/service.do"); | |||||
notCheckUrlValid.add("/attach/upload.do"); | |||||
notCheckUrlValid.add("/attach/uploadBase64.do"); | |||||
} | |||||
private static void addUrl(String url, List<String> list) { | |||||
list.add(url); | |||||
} | |||||
//是否不需要url校验 | |||||
private static boolean isMatchUrl(String lasturi, List<String> list) { | |||||
if (list.contains(lasturi)) return true; | |||||
for (String s: list){ | |||||
if (StringUtil.match(s, lasturi)) return true; | |||||
} | |||||
return false; | |||||
} | |||||
//是否不需要登录验证 | |||||
public static boolean isNoLogin(String lasturi) { | |||||
return isMatchUrl(lasturi, notLoginUrl); | |||||
} | |||||
//增加不需要登录的Uri | |||||
public static void addNoLoginUri(String uri) { | |||||
addUrl(uri, notLoginUrl); | |||||
} | |||||
/*//是否不需要切换库 | |||||
public static boolean isNoSetDb(String lasturi) { | |||||
return isMatchUrl(lasturi, notSetDbUrl); | |||||
} | |||||
//增加不需要登录的Uri | |||||
public static void addNoSetDbUrl(String uri) { | |||||
addUrl(uri, notSetDbUrl); | |||||
}*/ | |||||
//是否不需要url校验 | |||||
public static boolean isNoCheckValid(String lasturi) { | |||||
return isMatchUrl(lasturi, notCheckUrlValid); | |||||
} | |||||
//增加不需要校验合法性的Uri | |||||
public static void addNoCheckValidUri(String uri) { | |||||
addUrl(uri, notCheckUrlValid); | |||||
} | |||||
//不算用户交互操作 | |||||
public static boolean isNoInterActive(String lasturi) { | |||||
return isMatchUrl(lasturi, notInterActiveUrl); | |||||
} | |||||
//不算用户交互操作 | |||||
public static void addNoInterActiveUri(String uri) { | |||||
addUrl(uri, notInterActiveUrl); | |||||
} | |||||
private SessionUtil() { | private SessionUtil() { | ||||
} | } | ||||
@@ -23,41 +123,95 @@ public class SessionUtil { | |||||
} | } | ||||
public static String readToken(HttpServletRequest request) { | public static String readToken(HttpServletRequest request) { | ||||
String token = request.getHeader("Auth-Token"); | |||||
return readToken(request, KEY_HEADER_SESSION, KEY_PARAM_SESSION); | |||||
} | |||||
public static String readFingerKey(HttpServletRequest request) { | |||||
return readToken(request, KEY_HEADER_FP_KEY, KEY_PARAM_FP_KEY); | |||||
} | |||||
public static int readFingerVal(HttpServletRequest request) { | |||||
return NumberUtil.getIntIgnoreErr(readToken(request, KEY_HEADER_FP_VAL, KEY_PARAM_FP_VAL)); | |||||
} | |||||
private static String readToken(HttpServletRequest request, String headerName, String paramName) { | |||||
String token = request.getHeader(headerName); | |||||
if (token == null) { | if (token == null) { | ||||
token = request.getParameter("auth_token"); | |||||
token = request.getParameter(paramName); | |||||
} | } | ||||
return token; | return token; | ||||
} | } | ||||
public static UserSession checkSession(HttpServletRequest request, RedisManager redisManager) { | |||||
String token = readToken(request); | |||||
//无效登录异常 | |||||
public static void throwNoLogin() { | |||||
throw new BizException(SwConsts.ErrorCode.NO_LOGIN); | |||||
} | |||||
if (StringUtils.isBlank(token)) { | |||||
throw new UnauthenticatedException("not find Auth-Token in header"); | |||||
public static void checkSession(HttpServletRequest request) { | |||||
//校验登录 | |||||
String lasturi = getUriLast(request.getContextPath(), request.getServletPath()); | |||||
String token = readToken(request); | |||||
UserSession us = null; | |||||
if (StringUtils.isNotBlank(token)) { | |||||
us = RedisManager.getInstance().get(token, UserSession.class); | |||||
} | } | ||||
//校验登录 | |||||
final boolean isNologin ="design".equals(token) || isNoLogin(lasturi); | |||||
if (!isNologin) { | |||||
if (us == null) { | |||||
throwNoLogin(); | |||||
} | |||||
UserSession us = redisManager.get(token, UserSession.class); | |||||
if (us == null) { | |||||
throw new UnauthenticatedException("not find UserSession by token: " + token); | |||||
request.setAttribute(IEditor.USER_TOKEN, token); | |||||
request.setAttribute(IEditor.USER_SESSION, us); | |||||
} | } | ||||
return us; | |||||
String fpk = readFingerKey(request); | |||||
if (!StringUtil.isEmpty(fpk)) { | |||||
//校验指纹 | |||||
String fpKey = us != null ? String.valueOf(us.getUserId()): request.getSession(true).getId(); | |||||
int fpv = readFingerVal(request); | |||||
if (fpv > 0) { | |||||
SessionCache cache = SessionCacheFactory.getInstance().getCache(KEY_PARAM_FP_KEY, 1200L); | |||||
AtomicInteger ai = cache.get(fpKey + "_" + fpk); | |||||
int bv = ai != null ? ai.get(): 0; | |||||
if (fpv != bv) { | |||||
throw new BizException(SwConsts.ErrorCode.TOKEN_INVALID, "指纹错误,请勿重复提交!"); | |||||
} | |||||
} | |||||
} | |||||
} | } | ||||
//设置指纹 | |||||
public static void setFingerValue(HttpServletRequest request, R r) { | |||||
String fpk = readFingerKey(request); | |||||
if (!StringUtil.isEmpty(fpk)) { | |||||
UserSession us = (UserSession) request.getAttribute(IEditor.USER_SESSION); | |||||
String fpKey = us != null ? String.valueOf(us.getUserId()): request.getSession(true).getId(); | |||||
SessionCache cache = SessionCacheFactory.getInstance().getCache(KEY_PARAM_FP_KEY, 1200L); | |||||
public static UserSession checkSession(String accessToken, RedisManager redisManager) { | |||||
if (StringUtils.isBlank(accessToken)) { | |||||
throw new UnauthenticatedException("not find Auth-Token in header"); | |||||
} | |||||
UserSession us = redisManager.get(accessToken, UserSession.class); | |||||
if (us == null) { | |||||
throw new UnauthenticatedException("not find UserSession by token: " + accessToken); | |||||
AtomicInteger ai = cache.get(fpKey + "_" + fpk); | |||||
if (ai == null) { | |||||
ai = new AtomicInteger(0); | |||||
cache.put(fpKey + "_" + fpk, ai); | |||||
} | |||||
r.put(KEY_PARAM_FP_KEY, fpk); | |||||
r.put(KEY_PARAM_FP_VAL, ai.incrementAndGet()); | |||||
} | } | ||||
} | |||||
return us; | |||||
/** | |||||
* 截取请求名称,如重 /${contextPath}/framework/loginpage.do中截出loginpage.do; | |||||
* | |||||
* @param contextPath /dfp | |||||
* @param uri /dfp/framework/loginpage.do | |||||
*/ | |||||
private static String getUriLast(String contextPath, String uri) { | |||||
int index = uri.indexOf(contextPath+"/"); | |||||
if (index < 0) return uri; | |||||
if (index > 1) return uri; | |||||
return uri.substring(index + contextPath.length()); | |||||
} | } | ||||
// private static Session getShiroSession() { | // private static Session getShiroSession() { | ||||
@@ -3,6 +3,7 @@ package cc.smtweb.framework.core.util; | |||||
import cc.smtweb.framework.core.common.AbstractEnum; | import cc.smtweb.framework.core.common.AbstractEnum; | ||||
import cc.smtweb.framework.core.common.IntEnum; | import cc.smtweb.framework.core.common.IntEnum; | ||||
import cc.smtweb.framework.core.common.StrEnum; | import cc.smtweb.framework.core.common.StrEnum; | ||||
import cc.smtweb.framework.core.db.EntityHelper; | |||||
import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||
import java.util.List; | import java.util.List; | ||||
@@ -22,7 +23,14 @@ public abstract class SqlUtil { | |||||
* @return | * @return | ||||
*/ | */ | ||||
public static String replaceTable(String sql) { | public static String replaceTable(String sql) { | ||||
return sql; | |||||
return StringUtil.myReplaceStrEx(sql, "[#", "#]", new StringUtil.IStrHanlder() { | |||||
@Override | |||||
public String work(String src) { | |||||
src = src.toUpperCase(); | |||||
return EntityHelper.getSchemaTableName(src); | |||||
} | |||||
}); | |||||
} | } | ||||
/** | /** | ||||
@@ -240,7 +240,7 @@ public class StringUtil { | |||||
* @param delim the delimiter character(s) to use. (null value will join with no delimiter) | * @param delim the delimiter character(s) to use. (null value will join with no delimiter) | ||||
* @return a String of all values in the list seperated by the delimiter | * @return a String of all values in the list seperated by the delimiter | ||||
*/ | */ | ||||
public static String join(List<String> list, String delim) { | |||||
public static String join(Collection<String> list, String delim) { | |||||
if (list == null || list.size() < 1) | if (list == null || list.size() < 1) | ||||
return null; | return null; | ||||
StringBuffer buf = new StringBuffer(); | StringBuffer buf = new StringBuffer(); | ||||
@@ -254,6 +254,11 @@ public class StringUtil { | |||||
return buf.toString(); | return buf.toString(); | ||||
} | } | ||||
public static String join(String[] list, String delim) { | |||||
return join(Arrays.asList(list), delim); | |||||
} | |||||
/** | /** | ||||
* Splits a String on a delimiter into a List of Strings. | * Splits a String on a delimiter into a List of Strings. | ||||
* | * | ||||
@@ -490,7 +495,7 @@ public class StringUtil { | |||||
* @return str | * @return str | ||||
*/ | */ | ||||
public static String checkNull(String strValue, String defaultValue) { | public static String checkNull(String strValue, String defaultValue) { | ||||
return strValue == null ? defaultValue: strValue; | |||||
return strValue == null ? defaultValue : strValue; | |||||
} | } | ||||
/** | /** | ||||
@@ -27,5 +27,10 @@ | |||||
<modules> | <modules> | ||||
<module>core</module> | <module>core</module> | ||||
<module>bpm</module> | <module>bpm</module> | ||||
<!--<module>../biz/qhcs-parent/qhcs</module> | |||||
<module>../biz/pgzx-parent/wx-api</module> | |||||
<module>../biz/pgzx-parent/pgzx</module>--> | |||||
</modules> | </modules> | ||||
</project> | </project> |