java中权限

二进制码运算:
操作码与授权码进行与(&)运算,结果为操作码证明有该权限。
操作码与授权码进行或(|)运算,得到新的操作码。
操作码与授权码进行异或(^)运算,得到新的操作码。


po:
User(用户)
Role(角色)---------角色名(rname),
Module(模块)-------模块名(mname),模块url(murl)
AccessControl(访问控制)-----操作码(code)

关系:
用户(User)与角色(Role)之间 (mappedby="users") many---------many
角色(Role)与AccessControl (mappedby="role")one-------many
模块(module)与AccessControl (mappedby="module")one-------many

》》》》用户功能用于用户表的增删改查,用户登录名和密码验证。
分配权限角色授权

》》》角色功能,为用户分配角色,方式:左边移到右边是删除,右边移到左边是添加(移动需要修改数据库)。
判断用户是否被赋予某角色
public List findByUser(Integer userId) throws Exception {

String hql="from Role r where https://www.360docs.net/doc/d916957099.html,ers.id=?";
List list=roleDao.findListForParams(hql, new Object[]{userId});
return list;
}

public List findByUser2(Integer userId) throws Exception {
String hql="from Role r where r.id not in (select r2.id from Role r2 where https://www.360docs.net/doc/d916957099.html,ers.id=?)";

List list=roleDao.findListForParams(hql, new Object[]{userId});

return list;
}

角色的赋予与删除,并在数据库内进行修改
public void addRelation(Integer userId, Integer roleId) throws Exception {
Role role =roleDao.findById(roleId);
User user = userDao.findById(userId);
role.getUsers().add(user);
roleDao.update(role);
}

public void delRelation(Integer userId, Integer roleId) throws Exception {
Role role =roleDao.findById(roleId);
User user = userDao.findById(userId);
role.getUsers().remove(user); //
roleDao.update(role);

}
就是代码:
var userId=${https://www.360docs.net/doc/d916957099.html,erId};
window.onload=function(){
// 1. 初始化 数据。

roleService.findByUser(userId,function(list)
{
var s1= document.getElementById("s1");
for(var i=0; ivar temp = list[i];
s1.add(new Option(temp.rname,temp.id),s1.options.length);
}
});

roleService.findByUser2(userId,function(list)
{
var s2= document.getElementById("s2");
for(var i=0; ivar temp = list[i];
s2.add(new Option(temp.rname,temp.id),s2.options.length);
}
});
}



function right2left(){
var s1= document.getElementById("s1");
var s2= document.getElementById("s2");

var ss=s2.options;
for(var j=ss.length-1; j>=0; j--){
var temp = ss[j];
if(temp.selected){
s2.remove(j);
s1.add(temp,s1.options.length);
roleService.addRelation(userId,temp.value);
}
}
}


function left2right(){
var s1= document.getElementById("s1");
var s2= document.getElementById("s2");

var ss=s1.options;
for(var j=ss.length-1; j>=0; j--){
var temp = ss[j];
if(temp.selected){
s1.remove(j);
s2.add(temp,s2.options.length);

roleService.delRelation(userId,temp.value);
}
}

}




角色权限分配,授权:功能授权

》》》》》模块负责为角色分配操作权限。

通过拿到模块表的信息用于显示。
通过ignoreContextParams="false"
>
获取一个Map,键为模块id,值为操作码。当操作码与8(4、2、1)进行与运算,如果结果仍然是8(4、2、1)的话,表示原先有此权限,应该是被选中的,相反则是未被选中的,这里有一个判断。
权限选择的效方法:批处理,点击确定时判断,当中间表没有该模块的授权记录时创建一条记录,原先有的话只做update(与运算)
批处理:var roleId=${param.roleId};

function grant(ck,moduleId){
if(!dwr.engine._batch)
dwr.engine.beginBatch();

if(ck.checked){
acService.grant(roleId,moduleId,ck.value);
}else{
acService.cancel(roleId,moduleId,ck.value);
}
}

按键:

权限的取消与授予
public void cancel(Integer roleId, Integer moduleId, Integer code) throws Exception {
String hql="from AccessControl ac where ac.role.id=? and ac.module.id=?";
List list=accessControlDao.findListForParams(hql, new Object[]{roleId,moduleId});

AccessControl ac=list.get(0);
ac.setCode(ac.getCode()^code);
accessControlDao.update(ac);

}

@Transactional
public void grant(Integer roleId, Integer moduleId, Integer code) throws Exception {

String hql="from AccessControl ac where ac.role.id=? and ac.module.id=?";

List list=accessControlDao.findListForParams(hql, new Object[]{roleId,moduleId});

if(list==null||list.size()==0){
AccessControl accessControl = new AccessControl();
accessControl.setCode(code);
accessControl.setRole(roleDao.findById(roleId));
accessControl.setModule(moduleDao.findById(moduleId));

accessControlDao.add(accessControl);
}else{

AccessControl ac=list.get(0);
ac.setCode(ac.getCode()|code);

accessControlDao.update(ac);
}
}




》》》登录操作和登录后的权限操作使用拦截器实现,并在拦截器中判断登录用户是否有某操作权限,如果没有跳转至
无权限跳转至无权限提示页面或跳回登陆页面,有权限ActionInvocation通过,ac.invoke()。

1.

actionInvocation是什么
ActionInvocation就是Action的调用者。ActionInvocation在Action的执行过程中,负责Interceptor、Action和Result等一系列元素的调度。


2.ActionInvocation干什么
Interceptor通过ActionInvocation可以完全的改变Action行为:不让它执行、改变返回值、甚至可以细颗粒的操作Action的方法


相关文档
最新文档