<nbean:right/> 标签实现访问控制 newxy(新坐标) 标签运用 胡立新 说明:newxy是web快速开发框架,下文说明了如果利用标签实现访问控制。第一、二、三项中的用户管理、业务管理、业务分配可下载《部门及用户权限管理(第三版)》来实现。第四、五项介绍了用户登录过程获取业务操作权限信息,及如何实现访问控制。第六项的登录页面login.jsp可作参考。 下载: <nbean:right/> 标签实现访问控制 http://www.newxy.net/zh_cn/download/index.jsp 在web开发中,难免要对一些页面进行访问控制,newxy的<nbean:right/>可以帮助建立两种访问控制方法,首先建立三个表。 一、建立用户、业务、用户业务关系表 /*用户表*/ create table t_user( user_id int primary key, user_name varchar(50) not null, user_pass varchar(50) not null ) ; /*用户名的唯一索引*/ create unique index idx_user_name on t_user(user_name) ; /*业务*/ create table t_business( business_id int primary key, title varchar(255) not null, intro text null, type int null /*分类*/ ) ; create unique index idx_business on t_business(title) ; /*用户-业务*/ create table r_user_business( user_business_id int primary key, user_id int not null, business_id int not null, find char(1) default '1', remove char(1) default '0', upinsert char(1) default '0', scope char(1) default '0',/*操作范围,0:顶级单位范围内,1:本部门内*/ type int null /*分类*/ ) ; create unique index idx_user_business on r_user_business(user_id,business_id) ; 表结构可自定,此处的定义只作参考。 二、设定业务 假设有三个jsp页面,分别是/jsp1.jsp,/jsp2.jsp,/jsp3.jsp。上有三项业务,暂以jsp文件名作为业务名,插入三条记录。 BaseDAO dao=new BaseDAO(); DynaDto dto=new DynaDto(); dto.set_table("t_business"); dto.set(“title”,”jsp1”); dao.update(dto); dto= new DynaDto(); dto.set_table("t_business"); dto.set(“title”,”jsp2”); dao.update(dto); dto= new DynaDto(); dto.set_table("t_business"); dto.set(“title”,”jsp3”); dao.update(dto); 三、分配权限 建一权限管理模块,维护"用户-业务"表。假设有一用户的user_id=1,有一业务的business_id=1,可分两种情况: 1. 业务操作只分有权和无权 赋给该用户操作权限,只需插入一条记录 BaseDAO dao=new BaseDAO(); DynaDto dto=new DynaDto(); dto.set_table("r_user_business"); dto.set(“user_id”,”1”); dto.set(“business_id”,”1”); dao.update(dto); 删除该用户权限,只需删除这条记录 String sql=”delete from r_user_right where user_id=1 and business_id=1;” dao.prepareCall(sql); 2. 业务操作分多种情况,如查询、删除、插入更新等 设定该用户查询权 BaseDAO dao=new BaseDAO(); DynaDto dto=new DynaDto(); dto.set_table("r_user_business"); dto.set(“user_id”,”1”); dto.set(“business_id”,”1”); dto.set(“find”,”0”); // 或 dto.set(“find”,”1”); 0: 无权,1: 有权 dao.update(dto); 设定该用户删除权 BaseDAO dao=new BaseDAO(); DynaDto dto=new DynaDto(); dto.set_table("r_user_business"); dto.set(“user_id”,”1”); dto.set(“business_id”,”1”); dto.set(“remove”,”0”);// dto.set(“remove”,”1”); 0: 无权,1: 有权 dao.update(dto); 设定该用户插入更新权 BaseDAO dao=new BaseDAO(); DynaDto dto=new DynaDto(); dto.set_table("r_user_business"); dto.set(“user_id”,”1”); dto.set(“business_id”,”1”); dto.set(“upinsert”,”0”);//dto.set(“upinsert”,”1”); 0: 无权,1: 有权 dao.update(dto); 四、用户登录时在会话中建立有关权限bean 在用户登录时对用户名及口令验证,如果成功,在会话中加入权限bean。 假设这过程是在struts的action中完成的,struts配置如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <global-forwards> <forward name="index" path="/index.jsp" /> </global-forwards> <action-mappings> <action input="/login.jsp" parameter="method" path="/actionLogin" type="test.ActionLogin" /> </action-mappings> </struts-config> 有一个action,登录用,这个action没有formBean。 这个用于登录的action类如下: package action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.Action; import org.apache.commons.beanutils.DynaBean; import net.newxy.dbm.BaseDAO; public class ActionLogin extends Action { public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest servletRequest, HttpServletResponse servletResponse) { BaseDAO dao=new BaseDAO(); String user=servletRequest.getParameter("user_name"); String pass=servletRequest.getParameter("user_pass"); String sql="select * from t_user where user_name='" +user+"' and user_pass='"+pass+"'"; // 验证过程是判断数据库中是否保存了此用户 List list=null; try { list = dao.list(sql); if(list.size()==0){ // 没查到此用户,返回到登录页面 servletRequest.setAttribute("message"," 用户名或口令错误! "); return actionMapping.getInputForward(); } } catch (Exception ex) { // 发生错误,返回到登录页面 servletRequest.setAttribute("message"," 用户登录时发生错误! "+ex.getMessage()); return actionMapping.getInputForward(); } // 执行到此处表明 list.size>0 ,用户存在于数据库中, DynaBean bean=(DynaBean)list.get(0); servletRequest.getSession(false).setAttribute("user",bean); // 将用户的权限信息提取出,保存到会话中。 sql="select c.*, b.find,b.remove,b.upinsert from t_user a,r_user_business b,t_business c where a.user_name='" +user+"' and a.user_id=b.user_id and b.business_id=c.business_id"; try { list = dao.list(sql); servletRequest.getSession(false).setAttribute("right",list); } catch (Exception ex1) { } return actionMapping.findForward("index"); } } 用户登录成功后会话中的user保存了户用户基本信息,right保存了用户的权限信息。 right的_coll属性是集合类型,结构如下:
Business_id title find remove upinsert 1 Jsp1 0 0 0 2 Jsp2 1 1 1 五、使用<nbean:right/> 假设对jsp1.jsp 的访问只分有权和无权,可在jsp1.jsp 上放上<nbean:right/> 标签: <%@ page contentType="text/html; charset=GBK" %> <%@ taglib uri="/WEB-INF/newxy-bean.tld" prefix="nbean"%> <nbean:right name="right" fieldName="business_id" fieldValue="1"/> <html> <head> <title> jsp1 </title> </head> <body bgcolor="#ffffff"> …… </body> </html> 标签在会话中找到名为“right”的bean后,在bean的_coll中以 business_id为字段名,以”1”为字段值查找,如果找到,表明有权,后面代码继续,如果找不到,表明无权,打印出“没有权限”或自定义的提示信息,后面代码被忽略。 如果会话已过期,标签会提示,并且后面内容不再显示,以免出错。 假设对jsp2.jsp 的访问分查询、删除、插入更新。可在jsp2.jsp 页上加入<nbean:right/> 如下: <%@ page contentType="text/html; charset=GBK" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/newxy-html.tld" prefix="nhtml"%> <%@ taglib uri="/WEB-INF/newxy-logic.tld" prefix="nlogic"%> <%@ taglib uri="/WEB-INF/newxy-bean.tld" prefix="nbean"%> <nbean:right name="right" fieldName="business_id" fieldValue="2"/> <html> <head> <title> jsp2 </title> </head> <body bgcolor="#ffffff"> <logic:equal value="1" name="var1"> <font size="3" color="blue"> 有只读权 </font> </logic:equal> <logic:equal value="2" name="var1"> <font size="3" color="blue"> 有读写权 </font> </logic:equal> <h3> 本页文件: jsp2.jsp <logic:present name="user" scope="session"> ,用户: <bean:write name="user" property="user_name"/> </logic:present> </h3> <hr /> 可在“有权”的地方插入相关代码 <p></p> <nbean:right name="right" fieldName="business_id" fieldValue="2" lookupField="find" var="varFind"/> <logic:equal value="0" name="varFind"> 无查询权 <br /> </logic:equal> <logic:equal value="1" name="varFind"> 有查询权 <br /> </logic:equal> <nbean:right name="right" fieldName="business_id" fieldValue="2" lookupField="remove" var="varRemove"/> <logic:equal value="0" name="varRemove"> 无删除权 <br /> </logic:equal> <logic:equal value="1" name="varRemove"> 有删除权 <br /> </logic:equal> <nbean:right name="right" fieldName="business_id" fieldValue="2" lookupField="upinsert" var="varUpInsert"/> <logic:equal value="0" name="varUpInsert"> 无插入更新权 <br /> </logic:equal> <logic:equal value="1" name="varUpInsert"> 有插入更新权 <br /> </logic:equal> </body> </html> 标签在会话中找到名为“right”的bean后,在bean的_coll中以”business_id”为字段名,以”1”为字段值查找,如果找不到,表明无权,打印出“没有权限”或自定义的提示信息,后面代码被忽略。如果找到这条记录,以属性var的属性值(如”varFind”)为变量,将lookupField属性值(如“find”)为字段名的值赋给此变量,保存到toScope中。 标签得到名为“varFind”的bean后,根据varFind的值是0还是1,判断用户是否有查询权。 六、参考页面 登录页面login.jsp 代码: <%@ page contentType="text/html; charset=GBK" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %> <html> <head> <title> login </title> </head> <body bgcolor="#ffffff"> <h1> 登录 </h1> <!-- 显示用户登录失败时的提示信息 --> <logic:notEmpty name="message" scope="request"> <bean:write name="message"/> </logic:notEmpty> <hr /> <form method="post" action="/MyWeb/actionLogin.do"> <input type="text" name="user_name"/> <input type="text" name="user_pass"/><br /> <input type="submit" name="Submit" value=" 登录 "> <input type="reset" value=" 取消 "> </form> </body> </html> 主页面index.jsp 代码: <%@ page contentType="text/html; charset=GBK" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <%@ taglib uri="/WEB-INF/newxy-html.tld" prefix="nhtml"%> <%@ taglib uri="/WEB-INF/newxy-logic.tld" prefix="nlogic"%> <%@ taglib uri="/WEB-INF/newxy-bean.tld" prefix="nbean"%> <html> <head> <title> index </title> </head> <body bgcolor="#ffffff"> <h1> 主页面 <logic:present name="user" scope="session"> ,用户: <bean:write name="user" property="user_name"/> </logic:present> </h1> <logic:notEmpty name="message" scope="request"> <bean:write name="message"/> </logic:notEmpty> <hr /> <logic:present name="right" scope="session"> <ul> <li> <html:link page="/jsp1.jsp">jsp1.jsp </html:link> </li> <li> <html:link page="/jsp2.jsp">jsp2.jsp </html:link> </li> <li> <html:link page="/jsp3.jsp">jsp3.jsp </html:link> </li> </ul> </logic:present> <logic:notPresent name="right" scope="session"> <html:link page="/login.jsp"> 登录 </html:link> </logic:notPresent> </body> </html> newxy 新坐标网站: http:/www.newxy.net 相关文章:newxy标签实现部门、用户权限管http://blog.csdn.net/nlhlx/archive/2006/10/09/1327904.aspx
