看了网上一些关于权限控制的帖子,越看越迷糊,什么用AOP(Aspect Oriented Programming,面向方面编程),用容器,RBAC(基于角色的访问控制方法),SSO,Jive的Proxy模式等等等等,且又是role又是group,真是头都大了,先写个简单的实现方法,以后再研究高深的。
此方法不依赖容器 框架,适用于小系统(主要JSP页面要少于100,因为是硬编码到JSP),适用于要精确控制页面field的情况较多的系统。
(插句话:要分清权限控制与业务逻辑,业务逻辑就是情况由系统运行时的某些条件决定,如学生管理系统中,某一学生进入系统,只能看自己的记录,因为可看的记录是由学号来决定的,所以这是业务逻辑,而又如学生不能看老师的记录,这是有学生的身份来决定的,所以这是权限控制。)
好了,进入正题!
建表:
user(user信息: userID userPassword 等)
role(role描述:roleID roleDesc)
permission(permission描述:permissionID permissionDesc)
user-role(user role对应关系表:userID roleID)
role-permission(role permission对应关系表:roleID permissionID)
user-permission(user permission对应关系表:userID permissionID)
重要申明:
1 此处role没有继承关系,只是permission的集合
2 user-permission表只是为了方便,其数据是根据user-role role-permission两表得来,只有在user-role role-permission两表有更新的时候更新此表,并不能单独赋予user某个permission,只能赋予user一个或多个role。
3 permission的分配,这是一个难点,很多比较复杂的权限控制系统也是因为这个才发展出来,此处把它尽量想简单,不考虑业务逻辑,以页面为视角,分两层,首先是需要控制的jsp页面,然后是需要控制的页面field(包括link,text,textbox,button等等),field这一层还有privilege之分(R和W,即可读和可写)
基本思路:进入JSP页面时,检查用户信息,查到用户有此permission就包含此代码,如果没有此permission就不包含此代码,此功能由Tag来完成(不会写Tag?不要紧,抄!)。看代码吧!
1 建表(如上)
2 建两个class(bean) (UserProfile是用户基本信息 UserPermission是permission )
UserProfile.java:
package com.××.××.××;
import java.util.Collection;
public class UserProfile {
private String userId;
private String userType;
private String companyNo;
private String companyName;
private String companyType;
private Collection userPermissions;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getCompanyNo() {
return companyNo;
}
public void setCompanyNo(String companyNo) {
this.companyNo = companyNo;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCompanyType() {
return companyType;
}
public void setCompanyType(String companyType) {
this.companyType = companyType;
}
public Collection getUserPermissions() {
return userPermissions;
}
public void setUserPermissions(Collection userPermissions) {
this.userPermissions = userPermissions;
}
}
UserPermission.java:
package com.××.××.××;
public class UserPermission {
private int permissionId;
private String privilege;
public int getPermissionId() {
return permissionId;
}
public void setPermissionId(int permissionId) {
this.permissionId = permissionId;
}
public String getPrivilege() {
return privilege;
}
public void setPrivilege(String privilege) {
this.privilege = privilege;
}
}
3 加两个Tag(Page与Field):
SecurityTagForPage.java:
package com.**.**.taglib;
import java.util.*;
public class SecurityTagForPage extends TagSupport
{
private int permissionID;
public int doEndTag() throws JspException
{
HttpSession session = pageContext.getSession();
//登陆时把该user的userProfile放到session里
UserProfile userProfile = (UserProfile)session.getAttribute("userProfile");
Collection collection= userProfile.getUserPermissions();
Iterator it = collection.iterator() ;
while(it.hasNext())
{
UserPermission userPermission = (UserPermission)it.next();
if ((permissionID == userPermission.getPermissionId()))
{
return EVAL_PAGE;
}
}
return SKIP_PAGE;
}
public int getPermissionID()
{
return permissionID;
}
public void setPermissionID(int permissionID)
{
this.permissionID = permissionID;
}
}
SecurityTagForField:
public class SecurityTagForField extends TagSupport
{
private int permissionID;
private String privilege;
public int doStartTag() throws JspException
{
HttpSession session = pageContext.getSession();
UserProfile userProfile = (UserProfile)session.getAttribute("userProfile");
Collection collection= userProfile.getUserPermissions();
Iterator it = collection.iterator() ;
while(it.hasNext())
{
UserPermission userPermission = (UserPermission)it.next();
if (privilege ==null)
{
if ( (permissionID == userPermission.getPermissionId()))
{
return EVAL_BODY_INCLUDE;
}
}
else
{
if ((permissionID == userPermission.getPermissionId())
&&(privilege.equals(userPermission.getPrivilege())))
{
return EVAL_BODY_INCLUDE;
}
}
}
return SKIP_BODY;
}
public int getPermissionID()
{
return permissionID;
}
public void setPermissionID(int permissionID)
{
this.permissionID = permissionID;
}
public String getPrivilege()
{
return privilege;
}
public void setPrivilege(String privilege)
{
this.privilege = privilege;
}
}
4 在web-inf目录下建个securityTag.tld文件,内容如下:(改一下class的目录)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<!-- a tag library descriptor -->
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>security</shortname>
<uri/>
<info>
access control!
</info>
<tag>
<name>securityForField</name>
<tagclass>com.companyname.prjname.taglib.SecurityTagForField</tagclass>
<attribute>
<name>permissionID</name>
<required>true</required>
</attribute>
<attribute>
<name>privilege</name>
</attribute>
</tag>
<tag>
<name>securityForPage</name>
<tagclass>com.companyname.prjname.taglib.SecurityTagForPage</tagclass>
<attribute>
<name>permissionID</name>
<required>true</required>
</attribute>
</tag>
</taglib>
5 修改需要控制的jsp
在jsp页面这样写:
<%@ taglib uri="/WEB-INF/securityTag.tld" prefix="security" %>
<%@ page import="com.hp.elog2.util.Util" %>
<security:securityForPage permissionID="36" />
.......
<security:securityForField permissionID="46" privilege="R">
<td><html:text name="formBean" property="property1" readonly="true" /></td>
</security:securityForField>
<security:securityForField permissionID="46" privilege="W">
<td><html:text name="formBean" property="property1" /></td>
</security:securityForField>
......
这样就大功告成了,建议在所有jsp页面完成后再做权限控制这一块(添加Tag),主要都是些Copy+C和Copy+V的工作。
此方法最大的问题就是hardcode太多,但结构简单,思路清晰,适用范围广。
复杂的方法希望同道之士于我联系[email protected],共同研究!
本文地址:http://com.8s8s.com/it/it16383.htm