2019独角兽企业重金招聘Python工程师标准>>>
关于权限这一块,一直没去深入的研究过,只知道大概的过程,这一次借着新项目,重新把权限这一块了解了一下,以下是关于如何使用struts2中的拦截器实现用户权限的管理。
处理过程:
1. 初始化权限,在服务器启动的时候,将系统中所有的角色和功能查询出来,放入HashMap中,将来所有功能都在此进行判断。
2. 用户登录之后,将用户Id放入Session。
3. 拦截器拦截用户的每一次请求,先充Session中取出userId,如果没有UserId,则提示登录信息,有userId,则使用userId查询用户的角色,根据角色名称和当前请求的功能名称和list中的角色、功能信息进行对比,如果有当前功能,则有权限,没有则提示没有权限。
初始化权限: (在拦截器的init()方法中初始化)
public void init() {
// TODO Auto-generated method stub
System.out.println("AuthorityInterceptor----------------- init()");
// 初始化权限
String selectRoleIdOK = db.selectRoleId().toString();
System.out.println(selectRoleIdOK);
if(responseutil.getResultData(selectRoleIdOK).equals("OK")){
String roleId = responseutil.JSONResolveArray(responseutil.getRowsData(selectRoleIdOK), ",");
System.out.println("roleId:"+roleId);
String[] roleIdArr = roleId.split(",");
for (int i = 0; i < roleIdArr.length; i++) {
System.out.println(roleIdArr[i]);
String selectFunctionNameOK = db.selectFunctionName(roleIdArr[i]).toString();
System.out.println(selectFunctionNameOK);
String roleName = responseutil.JSONResolveArray(responseutil.getRowsData(selectFunctionNameOK), ",");
Object[] obj = roleName.split(",");
authorityMap.put(roleIdArr[i], obj);
}
}else {
System.out.println(selectRoleIdOK);
}
}
登录成功后,将userId存入Session中:
// 将用户id保存在Session中
ActionContext.getContext().getSession().put("userId", userId);
拦截用户的每一次请求,对用户的权限进行判断:
public String intercept(ActionInvocation invocation) throws Exception {
/**
* 从Session中检查是否有userId,没有则提示登录,有userId,则使用UserId查询用户的角色,根据角色名称和当前请求的功能名称和list中的
* 角色、功能信息进行对比,如果有当前功能,则有权限,没有则提示没有权限
*/
// 返回的信息
JSONObject result = new JSONObject();
String intercepter = null;
String userId = (String) ActionContext.getContext().getSession().get("userId");
if ("".equals(userId) || null == userId) {
System.out.println("没有登录");
result.put("result", "请登录!");
responseutil.authResponseData(result.toString());
}else {
System.out.println("已经登录");
// 已经登录,使用userId查询用户角色
String getRoleOK = db.getRole(userId).toString();
if(responseutil.getResultData(getRoleOK).equals("OK")){
String roleId = responseutil.JSONResolveArray(responseutil.getRowsData(getRoleOK), "");
System.out.println("roleId:"+roleId);
String functionName = invocation.getProxy().getActionName();
// 使用角色id和功能名称检查用户是否有次权限
boolean res = authorityJudge(authorityMap, functionName, roleId);
if (res) {
System.out.println("有权限了--------------");
intercepter = invocation.invoke();
}else {
System.out.println("没有权限****************");
result.put("result", "无此权限!");
responseutil.authResponseData(result.toString());
}
}else {
System.out.println(getRoleOK);
result.put("result", "获取角色失败!");
responseutil.authResponseData(result.toString());
}
}
return intercepter;
}
// 使用角色id和功能名称检查用户是否有次权限
public static boolean authorityJudge(Map<String,Object> authorityMap, String functionName, String roleId){
boolean authorityMake = false;
Object[] obj=(Object[]) authorityMap.get(roleId);
// 遍历对比是否有权限
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i]);
if (functionName.equals(obj[i])) {
System.out.println("有权限");
authorityMake = true;
break;
}else {
System.out.println("没有权限");
}
}
return authorityMake;
}