隨筆-39  評論-33  文章-0  trackbacks-0

          說明:newxyweb快速開發框架,下文說明了如果利用標簽實現訪問控制。第一、二、三項中的用戶管理、業務管理、業務分配可下載《部門及用戶權限管理(第三版)來實現。第四、五項介紹了用戶登錄過程獲取業務操作權限信息,及如何實現訪問控制。第六項的登錄頁面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

          ?

          posted on 2007-03-04 09:17 newxy新坐標 閱讀(1653) 評論(1)  編輯  收藏

          評論:
          # hclplfrb 2007-06-14 09:53 | hclplfrb
          fmdmqrsf http://wypryorc.com iehdrbcu xvcsyktz  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 定南县| 锡林郭勒盟| 宝山区| 乡城县| 莱州市| 泌阳县| 怀远县| 同仁县| 韶山市| 靖江市| 谷城县| 江川县| 绥化市| 滨州市| 鄯善县| 宜君县| 含山县| 永和县| 吉水县| 永修县| 固始县| 蓬莱市| 凤阳县| 德阳市| 易门县| 修武县| 二连浩特市| 井研县| 大竹县| 宁海县| 丹阳市| 盐山县| 鄯善县| 苍溪县| 定南县| 神农架林区| 台江县| 罗田县| 郁南县| 育儿| 崇明县|