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太多,但结构简单,思路清晰,适用范围广。