发表于: 2018-01-15 18:48:35
1 591
今日完成:
应前端要求修改了登陆返回的数据,吧权限认证重写了。
明日计划:
看书该接口。
成果展示:
登陆时先清空再把权限写入。
Enumeration sessionNames=request.getSession().getAttributeNames();
while (sessionNames.hasMoreElements()){
request.getSession().removeAttribute(sessionNames.nextElement().toString());
}
//把登陆的用户的权限存入session
for (Rolewithmodule power:powerList) {
for (Module module : moduleList) {
if (module.getId().equals(power.getModuleId())) {
request.getSession().setAttribute(module.getModuleName(), power);
}
}
}
控制器中调用权限判断,只能做到这样,还是需要传入一些特定的字段,无法实现更全面的复用性。
//权限判断
if (!PowerJudgement.powerJudge(request,"账号管理","delete")){
log.info("this user don't have power to delete user");
model.addAttribute("code",-7005);
return "pagescarrots-youneedboy-home-service/data/json";
}
判断器:
public static boolean powerJudge(HttpServletRequest request, String moduleName, String powerType) throws ServiceException, ServiceDaoException, IllegalAccessException, NoSuchFieldException {
Enumeration sessionNames=request.getSession().getAttributeNames();
while (sessionNames.hasMoreElements()){
String sessionName=sessionNames.nextElement().toString();
if (sessionName.contains(moduleName)){
Rolewithmodule power=(Rolewithmodule) request.getSession().getAttribute(sessionName);
Field field=Rolewithmodule.class.getDeclaredField(powerType+"Power");
field.setAccessible(true);
if (field.get(power).equals(1l)){
return true;
}
}
}
return false;
}
判断时间五毫秒之内,没有循环就是快。
存在的问题:
突然想到session还应该存放一些用户信息,比如在设置updateBy和creatBy的时候不应该使用前端发的数据,这样不严谨,明天修改,也就几行代码的事,就是之前没想到这一茬,让前端也把这些数据传过来了。
禅道:
http://task.ptteng.com/zentao/my-task.html
评论