最近、ユーザーのアクセスモジュール(許可)を制御する必要があるWebサイトタイプのプロジェクトに取り組んでいるため、許可制御機能の単純なセットが設計および実装されています。
1。データベース設計
ユーザー:ユーザー
モジュール:モジュール
SQLコード:
/*ターゲットサーバータイプ:mysqltargetサーバーバージョン:50628fileエンコード:65001date:2016-08-26 10:35:28*/set foreign_key_checks = 0; null auto_increment、 `module` varchar(30)デフォルトのnullコメント '模块'、` pid` int(10)デフォルトnullコメント 'id'、 `level` int(4)デフォルトnullコメント ''、プライマリキー(` id`))エンジン= innodbデフォルトcharset = utf8; ------------------------------ Table structure for `users`-- ----------------------------DROP TABLE IF EXISTS `users`;CREATE TABLE `users` ( `user_code` varchar(10) NOT NULL COMMENT '用户代码', `user_name` varchar(40) DEFAULT NULL COMMENT '用户名', `user_password` varchar(100) DEFAULT NULL COMMENT '密码', `qq` varchar(15) DEFAULT nullコメント 'qq'、 `msn` varchar(50)デフォルトのヌルコメント 'msn'、` demo` varchar(100)デフォルトヌルコメント '' '、 `auth_code`テキストコメント'、` user_code`))エンジン= innodbデフォルトcharset = utf8;
1.バックエンド実装<BR /> SSM+FreeMarkerフレームワークは、プロジェクトで許可ツリーのデータ構造にアクセスできるようにプロジェクトで使用され、JSON形式に変換します。
1)ディスプレイレイヤーはZtreeツリー(Setuserauthontree.html)を採用しています
< href = "$ {base.ctx} /js/layer-v2.1/laypage/skin/laypage.css" rel = "styleSheet" type = "text/css"/> <scrip = "$ {base.ctx} /js/layer-v2.1/layer/layer.js">スタイル - > <link href = "$ {base.ctx}/component/ztree/css/ztreestyle/ztreestyle.css" rel = "styleSheet" type = "text/css"/> <script = "text/javascript" " src = "$ {base.ctx} /component/ztree/js/jquery.ztree.core-3.5.js"> </script> <script = "text/javascript" src = "src = {base.ctx}/component/ztree/js/js/jquery.zexcheck-3.5.5.js"> type = "text/css">。blue-madison {border:1px solid#7ca7cc; border-top:0;}。キャプション{background-color:#578ebe;国境圏:0;パディング:0 10px;マージンボトム:0;色:#fff;} </style> </head> <body> <div style = "overflow-y:auto; width:400px; height:550px;"> <div id = "ztree"> <ul id = "treedemo"> </ul> </div> </div> <div> <div aLign = "MARGIN onclick = "editmodle()"> ok </button> <button type = "button" id "=" cancel "> close </button> </div> </div> </div> <script> $(" document ")データ:{"$ {userid}"}、 "json"、function(result){ztreeobj = $ .fn.ztree.init( "#treedemo")、result.datas.data); //ツリーvar ztreeobjをロードします。 //詳細な使用については、APIドキュメント(設定設定の詳細な説明)を参照してくださいvar設定= {view:{// dblclickexpand:false、showline:true、//ノード間の接続は表示されますか}、{enable:true、// nocheckinherit:che kbox: " "n": "ps"}、// autochecktrigger:true}、callback:{oncheck:ztreeoncheck、}}; // Callbackイベント関数のクリックボックスZtreeonCheck(Event、TreeID、Treenode){ /* var ztree = $ .fn.ztree.getztreeobj( "treedemo"); var chandernodes = ztree.getChangeCheckedNodes(); for(var i = 0; i <chandernodes.length; i ++){var treenode = chandernodes [i]; } */}; function editmodle(){var rootid = null; var midid = null; var minid = null; var treeobj = $ .fn.ztree.getztreeobj( "treedemo"); var nodes = treeobj.getCheckedNodes(); for(var i = 0; i <nodes.length; i ++){if(nodes [i] .level == 0){rootid = rootid+"、"+nodes [i] .id; } if(nodes [i] .level == 1){midid = midid+"、"+nodes [i] .id; } if(nodes [i] .level == 2){minid = minid+"、"+nodes [i] .id; }} if(rootid!= null){rootid = rootid.substring(5、rootid.length); } if(midid!= null){midid = midid.substring(5、midid.length); } if(minid!= null){minid = minid.substring(5、minid.length); } $ .ajax({type: "post"、url: "$ {base.ctx}/setup/updateSerrightmaskbyajax"、datatype: "json"、data:{"rootid": "rootid、" midid ":midid、" minid ":minid、" userid ":" $ {userid} {userid}、 " if(result == "1"){layer.msg(suctionfully! "); } // close $( "#cancel")。 </script> </body> </html>ディスプレイ効果は次のとおりです。
2)コントローラー制御層はSpringMVCを使用します
制御レイヤーで、データをJSON形式に変換し、ディスプレイレイヤーに送信します。
/** * @fun get branchユーザー許可subsoreid = stringutils.isempty(substoreid)?string.valueof(session.getattribute( "substoreid")):subsoreid; //それがホテルであるか、宿屋であるかを判断<マップ<文字列、オブジェクト>>バージョンリスト= this.setupservice.gethotelversions(substoreid);オブジェクトバージョン= versionSlist.get(0).get( "バージョン"); map <string、object> hotelmap = new hashmap <string、object>(); if((null!= versionSlist)&&(versionSlist.size()!= 0)){//リストは空ではありません。 } else if( "simple" .equals(version)){// inn // query inn permerition tree hotelmap = this.rightmaskservice.getuserrightmaskontree(subroreid、id、 "simple"); }} map <string、object> resultmap = new hashmap <string、object>(); resultMap.put( "Datas"、HotelMap); jsonobject.tojsonstring(resultmap、serializerfeature.writemapnullvalue); } 3)サービスサービスレイヤーは、Ztree形式を満たすツリーデータ構造へのアクセス許可をカプセル化します
/** * @fun get branchユーザー許可 * @author pi feng * @date 2016/8/25 * @param substoreid * @param id * @return map <string、object> */@override public <string、object> getuserrightmaskontree Userrightmask = this.irightmaskdao.getuserrightmaskbysubandid(substoreid、id); List <Map <String、object >> listone = new ArrayList <Map <String、object >>(); list <map <string、object >> listtwo = new ArrayList <Map <String、object >>(); // list <map <string、object >>> listthree = new arraylist <map <string、object >>(); List <Map <String、Object >>> resultList = new ArrayList <Map <String、Object >>(); if(versions.equals( "complete")){// hotel listone = this.irightmaskdao.getrightmaskonhotelone(); listtwo = this.irightmaskdao.getrightmaskonhoteltwo(); // listthree = this.irightmaskdao.getrightmaskonhotelthree(); PackagingTotWotree(resultlist、listone、listtwo、userrightmask); } else if(versions.equals( "simple")){// inn listone = this.irightmaskdao.getrightmaskontavernone(); listtwo = this.irightmaskdao.getrightmaskontaverntwo(); // listthree = this.irightmaskdao.getrightmaskontavernthree(); PackagingTotWotree(resultlist、listone、listtwo、userrightmask); } map <string、object> map = new hashmap <string、object>(); map.put( "data"、resultList);マップを返します。 }/** * @functionの最初のレベルのツリーをカプセル化 * @author pi feng * @date 2016/8/26 * @param resultlist * @param listone * @param authcode * @return void */private backagingtoonetree(list <map <map <map <string >> result>> auptrist、for <<<<<<<<<<<<<<<<<<<<<<<<<string> i = 0; listone.size(); rootmap.put( "id"、listone.get(i).get( "id")); rootmap.put( "name"、listone.get(i).get( "module")); if(validaterightmask(listone、authcode、i)!= -1){rootmap.put( "checked"、true); } else {rootmap.put( "checked"、false); } resultlist.add(rootmap); }}/** * @function cendulate Secondary Tree * @author pi feng * @date 2016/8/26 * @param resultlist * @param listone * @param listtwo * @return void */private void packagingtotwotree(list map <map <<<<<<<<<<<<<<<<<< object >> listtwo、map <string、object >> authcode){for(int i = 0; i <listone.size(); i ++){list <string、object >> midlist = new arraylist <map <string、object >>(); for(int j = 0; j <listtwo.size(); j ++){if(listtwo.get(j).get( "pid")。toString().equals(listone.get(i).get( "id")。toString( "id")。 map <string、object> midmap = new hashmap <string、object>(); midmap.put( "id"、listtwo.get(j).get( "id")); midmap.put( "name"、listtwo.get(j).get( "module")); midmap.put( "children"、minlist); if(validaterightmask(listtwo、authcode、j)!= -1){midmap.put( "checked"、true); } else {midmap.put( "checked"、false); } midlist.add(midmap); }} map <string、object> rootmap = new hashmap <string、object>(); rootmap.put( "id"、listone.get(i).get( "id")); rootmap.put( "name"、listone.get(i).get( "module")); rootmap.put( "children"、midlist); if(validaterightmask(listone、authcode、i)!= -1){rootmap.put( "checked"、true); } else {rootmap.put( "checked"、false); } resultlist.add(rootmap); }}/** * @function cenculate incapsulte culsulte revel tree * @author pi feng * @date 2016/8/26 * @param resultlist * @param listone * @param listtwo * @param listTwo listone、list <map <map <string、object >> listtwo、list <map <map <string、object >> list three、map <string、object> authcode){for(int i = 0; i <listone.size(); i ++){list <string、object >> midlist = new Arraylist <map <string、object >>(); for(int j = 0; j <listtwo.size(); j ++){if(listtwo.get(j).get( "pid")。toString().equals(listone.get(i).get( "id")。toString( "id")。 for(int k = 0; k <listthree.size(); k ++){map <string、object> minmap = new hashmap <string、object>(); if(listthree.get(k).get( "pid")。toString().equals(listtwo.get(j).get( "id")。toString()))))))))))))) minmap.put( "name"、listthree.get(k).get( "module")); if(validaterightmask(listthree、authcode、k)!= -1){minmap.put( "checked"、true); } else {minmap.put( "checked"、false); } minlist.add(minmap); }} map <string、object> midmap = new hashmap <string、object>(); midmap.put( "id"、listtwo.get(j).get( "id")); midmap.put( "name"、listtwo.get(j).get( "module")); midmap.put( "children"、minlist); if(validaterightmask(listtwo、authcode、j)!= -1){midmap.put( "checked"、true); } else {midmap.put( "checked"、false); } midlist.add(midmap); }} map <string、object> rootmap = new hashmap <string、object>(); rootmap.put( "id"、listone.get(i).get( "id")); rootmap.put( "name"、listone.get(i).get( "module")); rootmap.put( "children"、midlist); if(validaterightmask(listone、authcode、i)!= -1){rootmap.put( "checked"、true); } else {rootmap.put( "checked"、false); } resultlist.add(rootmap); }}/** * @functionリストにauthcodeに許可コードがあるかどうかを確認 * @author pi feng * @date 2016/8/26 * @param list * @param authcode * @return Int */private int valitaterightmask(リスト<マップ<マップ<文字列、オブジェクト> authcode.get( "auth_code")!= null? authcode.get( "auth_code")。toString(): ""; if(!stringutils.isempty(rightmask)){rightmask = rightmask.replace( ";"、 "、"); string [] array = rightmask.split( "、"); for(int j = 0; j <arry.length; j ++){string arrayrightmask = arry [j]; string listrightmask = list.get(i).get( "id")。toString(); if(arryrightmask.equals(listrightmask)){return 1; }}} else {return -1; } return -1; } 4)データベースを照会して、ユーザー許可を取得します
a。データレイヤーの許可レベルに従って、モジュールテーブルからさまざまなレベルのアクセス許可を取り出します。
id、モジュール、pid、レベルからのレベル= '0' 'id、module、pid、levels from module =' 1 'revel =' 2 'からid、モジュール、pid、レベルを選択します。
b。ユーザーテーブルのユーザー(許可コード)のすべての権限を取り出します
user_code = 'pifeng'でusersからauth_codeを選択します
c。許可を保存すると、異なるレベル間の許可コードはイギリスのセミコロンによって分離されます。」、および同じレベル間の許可コードはイギリスのコンマ "、"によって区切られます。例:1,2,3,4,5,6,7,8,9,10,11、12; 13,14,15,17,18,19,20,21,22,23,24,25,26,36,37,27,28,29,30,31,32,33,34,35,38,39,40,41,42,43,44,35,33,30,31,32,33,344. 、45,46,47,48,49,50,51,52,53,54,56,57,58,59,60,61,62,63,64,133,65,66,67,68,69,70,71,72,73,74,74,75,747,68,68,68,69,70,71,72,73,74. 26,127,128,130,131,76,77,78,79,80,81,82,83,84,85,86,87,88,99,124,134,135,136,140,141,89,90,91,92,93,94,95,95,95,95,95,95,134, 6,97,98,137,138,139,100,101,102,103,106,107,132,108,109,110,111,112,113,114,115,116,125,117,118,119,120,121,12222
5)FreeMarkerタグを使用して、ユーザーの許可コードに従ってページ機能モジュールが表示されるかどうかを制御します
a。 XMLファイルのFreeMarker構成
<bean id = "freemarkerconfig"> <! - テンプレートの読み込みパス - > <プロパティ名= "Templateloaderpath"> <value>/ftl/</value> </property> <Property name = "freemarkervariables"> <map> <entry key = "xml_escape" falue-ref = </</> </> </> </> </</> < name = "freemarkersettings"> <props> <prop key = "tag_syntax"> auto_detect </prop> <prop key = "template_update_delay"> 0 </prop> <prop key = "default_encoding"> utf-8 </prop> <prop key = "出力" key = "locale"> zh_cn </prop> <prop key = "date_format"> yyyy-mm-dd </prop> <prop key = "time_format"> hh:mm:ss </prop> <prop key = "number_format"> 0。 <! - null value Processing-> <prop key = "Classic_compatible"> true </prop> <! - ftlテンプレートを自動的にインポートし、「ベース」エイリアスを名前空間として使用します - > <prop key = "auto_import"> inc/spring.ftl id = "freemarkerviewiedresolver"> <プロパティ名= "suffix" value = "。html"/> <プロパティ名= "cache" value = "false"/> <プロパティ= "viewclass" value = "org.springframework.web.servlet.view.freemarker.freemarker.freemarker value = "text/html; charset = utf-8"> </property> <! - 固定例外:既存のモデルのためにセッション属性 'suptoreid'を公開できません - > <プロパティname = "balows" value = "true"/> <プロパティ名= " name = "exposeSpringmacrohelpers" value = "true"/> <! - この変数値はpagecontext.request.request.request.contextpath-> <プロパティ名= "requestcontextattribute" value = "request"/> <プロパティ名= "属性マップ">先ほど定義したツールクラスのアソシエイト - > <bean/> </entry> </map> </property> </bean>
b。 TemplateMethodModelクラスを継承するクラスを作成し、FreeMarkerカスタムメソッドを実装して、ページモジュールがログインを表示し、ユーザー許可コードをセッションに保存し、セッションから許可を取得するかどうかを制御します。これが例です:
パブリッククラスメニューフンションを実装したTemplateMethodModel {@Override public objece exec(list arg0)throws templatemodelexception {int level = integer.valueof(arg0.get(0).tostring()); //モジュールレベルint modelid = integer.valueof(arg0.get(1).toString()); //モジュールID int count = 0; //セッションこのモジュールの許可コードhttpservletrequest request =((servletRequestattributes)requestContextholder.getRequestattributes())。getRequest(); httpsession session = request.getSession();オブジェクトo = session.getAttribute( "Info"); if(o == null)falseを返します。情報情報=(情報)o; string authcode = info.getuser()。getauthcode(); //許可コードif(authcode.contains( ";")){string [] masks = authcode.split( ";"); string [] m = masks [level] .split( "、"); for(int i = 0; i <m.length; i ++){if(modelid == integer.parseint(m [i])){++ count; } else {count+= 0; }}}} if(count == 0){return false; } else {return true; }}} c。ページでFreeMarkerタグを使用し、モジュールの表示を制御すると非表示
Menucallの2つのパラメーター、1つ目はモジュールレベル、2つ目はモジュールIDです
例えば:
<#if menucall(1,122)> <li style = "line-height:250%"> <a href = "#" id = "booknew"> <i> </i>予約</a> </li> </#if>
上記は、ユーザーのアクセスモジュール(許可)を制御する一般的な実装です。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。