Recientemente, estoy trabajando en un proyecto de tipo sitio web que requiere controlar el módulo de acceso del usuario (permisos), por lo que se diseña e implementa un conjunto simple de funciones de control de permisos.
1. Diseño de base de datos
Usuario: usuarios
Módulos: módulos
Código SQL:
/*Tipo de servidor de destino: MySQLTarget Server Versión: 50628 File Coding: 65001Date: 2016-08-26 10: 35: 28*/set extranjero_key_checks = 0;----------------------------------- La estructura de la tabla para `módulos'-- -----------------------------------------------------------------------------------------------------------------------------------------------------Tabla de caída Si existe` Modules `Crea la Tabla` Modules `` ‘` `` `10, 10). AUTO_INCREMENT, `module` varchar(30) DEFAULT NULL COMMENT '模块', `pid` int(10) DEFAULT NULL COMMENT '上一级id', `level` int(4) DEFAULT NULL COMMENT '级别', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records of modules-- ------------------------------ ------------------------------ Table Estructura para `usuarios`-- -------------------------------- Tabla de caída Si existe` Usuarios`; Crear tabla `Usuarios` (` User_code` varchar (10) no comentario nulo '用户代码', `user_name` varchar (40) predeterminado NULL comment '用户名',` user_password` varHar (100) comentario predeterminado '密码 Qq` var (15) comentario predeterminado Qq', NECHAR NELUL `msn` varchar (50) comentario nulo predeterminado 'msn',` damo` varchar (100) comentario nulo predeterminado '备注', `auth_code` comentario de texto '权限码', clave primaria (` user_code`)) Engine = innoDB Default Charset = Utf8; --------------------------------------- Records de usuarios----------------------------------
1. Implementación de fondo <Br /> El marco de marcas freems+se usa en el proyecto para encapsular los permisos en la estructura de datos del árbol de permiso, y luego convertirlo en el formato JSON.
1) La capa de visualización adopta Ztree Tree (setUserauthontree.html)
< href = "$ {base.ctx} /js/layer-v2.1/laypage/skin/layPage.css" rel = "stylesheet" type = "text/css"/> <script src = "$ {base.ctx} /js/layer-v2.1/layer/layer.js"> </script> <! href = "$ {base.ctx} /component/ztree/css/ztreestyle/ztreestyle.css" rel = "stylesheet" type = "text/css"/> <script type = "text/javaScript" src = "$ {base.ctx} /Component/ztree/js/jquery.ztree.core-3.5.js"> </script> <script type = "text/javaScript" src = "$ {base.ctx} /Component/ztree/js/jquery.ztree.excheck-3.5.jsy type = "text/css">. Blue-Madison {Border: 1px Solid #7ca7cc; border-top: 0;}. subtítulos {Color de fondo: #578ebe; Border-Bottom: 0; relleno: 0 10px; Botón de margen: 0; color: #fff;} </ystye> </head> <body> <div style = "Overflow-y: Auto; Width: 400Px; Height: 550px;"> <Div Id = "Ztree"> <ul Id = "Treedemo"> </ul> </divs>/divs> <div> <div> <div> <divirign = "styly" centro = "margin-top: 5px" onClick = "editModle ()"> ok </boton> <button type = "button" id = "cancel"> Close </boton> </div> </div> </div> <script> $ ("documento"). Ready (function () {$ .JAJAX ({Tipo: "Post", url: "$ {base.ctx}/setup/getup/getuSrightmaskByid", ",", ",", ",", ",", ",", ",", ",", ",", Datos: {"id": "$ {userId}"}, DataType: "JSON", Success: Funcion (resultado) {ZtreeObj = $ .fn.ztree.init ($ ("#Treedemo"), setting, resultado.datas.data); // Cargue el árbol var ztreeobj; // Para uso en profundidad, consulte la documentación de la API (explicación detallada de la configuración de configuración) Var setting = {View: {// dBlClICKExPand: false, showline: true, // ¿La conexión entre nodos se muestra}, comprobar: {enable: true, // nocheckInHerit: false, falso, "chechBoxBox", chkBoxtype: {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" "," "," "", "" "," "", "" "," "", "" "," "" "PS"}, // AUTOCHECKTRIGGER: true}, devolución de llamada: {onCheck: ztreeoncheck,}}; // casilla de verificación Haga clic en la función de devolución de llamada Ztreeoncheck (evento, treeid, treeNode) { /* var ztree = $ .fn.ztree.getztreeObj ("Treedemo"); var cambianodes = ztree.getChangeCheckedNodes (); for (var i = 0; i <cambiadonodes.length; i ++) {var treeNode = cambieDNodes [i]; } */}; función editModle () {var rootId = null; var midid = null; var minid = nulo; var treeobj = $ .fn.ztree.getztreeobj ("Treedemo"); var nodos = treeobj.getCheckedNodes (); for (var i = 0; i <nodo.length; i ++) {if (nodos [i] .level == 0) {rootId = rootId+","+nodos [i] .id; } if (nodos [i] .level == 1) {midid = midid+","+nodos [i] .id; } if (nodos [i] .level == 2) {minid = minid+","+nodos [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/updateUserrightMaskByAdaJax", dataType: "json", data: {"rootId": rootID, "midId": midid, "minid": minid, "userID": "$ {userID"}, exitoso: function (resultado (resultado) (resultado) Layer.msg ("Empoderada con éxito!"); } // cerrar $ ("#cancelar"). Click (function () {top.dialog.get ("set-dialog"). Close (). Remove ();}); </script> </body> </html> El efecto de visualización es el siguiente:
2) La capa de control del controlador usa SpringMVC
En la capa de control, convierta los datos en formato JSON y envíelo a la capa de visualización.
/** * @fun Get Branch Permisos de usuario * @author pi feng * @date 2016/8/25 * @param session * @param id * @param sustoreid * @return */@requestmapping ("getUserrightMaskByid") @ResponseBody Public Object getUserrightMaskByid (Httpsession Session, String id, String Sustoreid) {{ sustoreId = StringUtils.isEmpty (sustoreId)? String.ValueOf (session.getAttribute ("sustoreid")): sustoreid; // juzga si es un hotel o una lista de posadas <map <string, object >> versionsList = this.setupservice.gethotelversions (sustoreid); Versiones de objetos = versionsList.get (0) .get ("versiones"); MAP <String, Object> HotelMap = New Hashmap <String, Object> (); if ((null! = versionsList) && (versionsList.size ()! = 0)) {// La lista no está vacía if ("completa" .equals (versions)) {// hotel // quiry hotel permiso árbol de hotelmap = this.rightmasskskservice.getusErrightmaskontree (érminto, id, "completo");; } else if ("simple" .equals (versiones)) {// inn // Query Inn Permission Tree HotelMap = this.RightMasskService.getuserrightMaskOntree (sustore, id, "simple"); }} Map <string, object> resultMap = new HashMap <String, Object> (); resultMap.put ("DataS", HotelMap); return jsonObject.ToJSonstring (ResultMap, SerializerFeature.WriteMapnullValue); } 3) La capa de servicio de servicio encapsula los permisos en una estructura de datos de árbol que satisface el formato ZTree
/** * @fun Get Rands User Permisos * @author pi feng * @date 2016/8/25 * @param sustoreid * @param id * @param versions * @return map <string, object>/@Override public map <string, object> getUsErrightMaskOntree (String SustoreID, String Id, String Versions) {Map <String, Object> userrightmask = this.irightmaskdao.getuserrightmaskbysubandid (sustoreid, 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 >>> listTheRe = new ArrayList <map <string, object >> (); Lista <map <string, object >>> resultList = new ArrayList <map <string, object >> (); if (versions.equals ("completo")) {// hotel listone = this.irightmaskdao.getRightMaskonHotelone (); listTwo = this.irightmaskdao.getRightMaskonHoteltWo (); // listThree = this.irightmaskdao.getRightMaskonHotelThree (); PackagingTotwotree (ResultList, listOne, listwo, userrightmask); } else if (versions.equals ("simple")) {// inn listone = this.irightmaskdao.getRightMaskonTavernone (); listTwo = this.irightmaskdao.getRightMaskontaverntwo (); // listthree = this.irightmaskdao.getRightMaskontavernThree (); PackagingTotwotree (ResultList, listOne, listwo, userrightmask); } Map <string, object> map = new HashMap <String, Object> (); map.put ("data", resultList); mapa de retorno; }/** * @function encapsula un árbol de primer nivel * @author pi feng * @date 2016/8/26 * @param resultlist * @param listone * @param authcode * @return void */private void paquete de paquete (list <map <string, object >> resultList, list <string <string >> listone, map <string, string, objecteTree (list <string <string, object >> resultList, list <map <string >> listone, map <string, map, objeceTeTree (list <string <string, object >> resultList, list <string <string >> listone, map <string, string, objeto>/auth (list <string, object >> result. 0; rootmap.put ("id", listone.get (i) .get ("id")); rootmap.put ("name", listone.get (i) .get ("módulo")); if (validaterightMask (listOne, authcode, i)! = -1) {rootmap.put ("verificado", true); } else {rootmap.put ("verificado", falso); } resultList.Add (rootmap); } } /** * @function Encapsulate a secondary tree* @author Pi Feng* @date 2016/8/26 * @param resultList * @param listOne * @param listTwo * @param authCode * @return void */ private void packagingToTwoTree(List<Map<String, Object>> resultList, List<Map<String, Object>> listOne, List<Map<String, Objeto >> listwo, map <string, object >> authcode) {for (int i = 0; i <listone.size (); i ++) {list <map <string, object >> midlist = new ArrayList <map <string, object >> (); for (int j = 0; j <listwo.size (); j ++) {if (listTwo.get (j) .get ("pid"). toString () .equals (listOne.get (i) .get ("id"). toString ()) {list <map <string, objeto >>> minlist = new arrayLLInlInslist <map <string, string, object ();); Map <string, object> midmap = new HashMap <String, Object> (); midmap.put ("id", listwo.get (j) .get ("id")); midmap.put ("name", listwo.get (j) .get ("módulo")); midmap.put ("niños", minlist); if (validaterightMask (listTwo, authcode, j)! = -1) {midmap.put ("verificado", true); } else {midmap.put ("comprobado", falso); } 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 ("módulo")); rootmap.put ("niños", lista media); if (validaterightMask (listOne, authcode, i)! = -1) {rootmap.put ("verificado", true); } else {rootmap.put ("verificado", falso); } resultList.Add (rootmap); }}/** * @Function encapsula un árbol de tercer nivel * @author pi feng * @date 2016/8/26 * @param dultelist * @param listone * @param listwo * @param listtwo * @param authcode * @return */private void paquete de paquete de listwo (list <mapmy>> @param authcode * @return */private void paquete de paquete listone, list <map <string, object >> listTwo, list <map <string, object >> listthree, map <string, object> authcode) {for (int i = 0; i <listone.size (); i ++) {list <map <string, object >> midlist = new ArrayList <map <string, object >> ();; for (int j = 0; j <listwo.size (); j ++) {if (listTwo.get (j) .get ("pid"). toString () .equals (listOne.get (i) .get ("id"). toString ()) {list <map <string, object >> minlist = new arrayLLIllInlInlInsm. 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 ("id", listThree.get (k) .get ("id")); minmap.put ("name", listthree.get (k) .get ("módulo")); if (validaterightMask (listThree, authcode, k)! = -1) {minmap.put ("verificado", true); } else {minmap.put ("verificado", falso); } minlist.add (minmap); }} Map <string, object> midmap = new HashMap <String, Object> (); midmap.put ("id", listwo.get (j) .get ("id")); midmap.put ("name", listwo.get (j) .get ("módulo")); midmap.put ("niños", minlist); if (validaterightMask (listTwo, authcode, j)! = -1) {midmap.put ("verificado", true); } else {midmap.put ("comprobado", falso); } 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 ("módulo")); rootmap.put ("niños", lista media); if (validaterightMask (listOne, authcode, i)! = -1) {rootmap.put ("verificado", true); } else {rootmap.put ("verificado", falso); } resultList.Add (rootmap); }}/** * @Function Verifique si hay un código de permiso en Authcode en la lista * @author pi feng * @date 2016/8/26 * @param list * @param authcode * @param i * @return int */private int validaterightmask (lista <map <string, objeto >> list, map <string> authcode, int i) {string rightmask = 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) Consulte la base de datos para obtener permisos de usuario
a. Saque diferentes niveles de permisos de la tabla de módulos de acuerdo con el nivel de permiso en la capa de datos.
Seleccione ID, módulo, PID, nivel de módulos donde nivele = '0' Seleccione ID, módulo, PID, nivel de módulos donde nivele = '1' Seleccione ID, Módulo, PID, Nivel de módulos Where nivel = '2'
b. Saque todos los permisos de un usuario (código de permiso) en la tabla de usuarios
Seleccione Auth_code de usuarios donde user_code = 'Pifeng'
do. Al ahorrar permisos, los códigos de permiso entre los diferentes niveles están separados por la coma británica ";", y los códigos de permiso entre el mismo nivel están separados por la coma británica ",". Por ejemplo: 1,2,3,4,5,6,7,8,9,10,11,12; 13 13,14,15,17,17,17,18,192,20,21,21,22,23,24,24,25,27,26,37,37,27,27,28,29,30,33,33,32,33,34,34,35,37,397,40,412,43,4441. , 45,46,47,48,49,50,51,52,53SECHOS 26277,128,129,130,130,13131,76,7777,777,7979,80,8182,833,847,85,87,87,87,987,999124,134,134,135,136,140140,1140,111355,1357,1371111 .1 .140,994,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,945,94,94,94,94,94,9413. 6,97,97,137,138,139,139,100,101,102,1033,106,107,107,108,109,110,110,111112,113,114,115,116,125,117,117,117,111919111111 .110,120,1212122.
5) Use la etiqueta Freemarker para controlar si el módulo de función de página se muestra de acuerdo con el código de permiso del usuario
a. Configuración de Marker en el archivo XML
<bean id = "freeMarkerConfig"> <!-Template Carging Rath-> <Property name = "TemplateloaderPath"> <alvalor>/web-INF/ftl/</valor> </propiedad> <propiedad name = "freeMarkerVariables"> <MAP> <POTY KEY = "Xml_escape" Value-ref = "FMXMLES Cape"/</> </MAP> MAP name = "freemarkerSettings"> <props> <prot key = "tag_syntax"> Auto_detect </prop> <prot key = "TEMPLATE_UPDATE_DELAY"> 0 </prop> <Prop Key = "Default_Encoding"> Utf-8 </prop. <prop key = "date_format"> yyyyy-mm-dd </pr> <prop key = "time_format"> hh: mm: ss </pr> <prop syp key = "number_format"> 0. ####### </prop> <proping key = "datetime_format"> yyyyyyyyyyyy-mm-dd hh: mm: ss </prop> <!-nlelulein Key = "Classic_Compatible"> True </prop> <!-Importe automáticamente la plantilla FTL y use el alias "Base" como el espacio de nombres-> <proping key = "auto_import"> inc/spring.ftl como base </prop> </props> </property </bean> <bean id = "fmxmlescape"/> <bean id = "free-freeViewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewer" name = "Suffix" Value = ". Html"/> <Property Name = "Cache" Value = "False"/> <Property Name = "ViewClass" Value = "org.springframework.web.servlet.view.freemarker.freemarkerView"/> <nombre de propiedad = "ContentType" Valor = "Text/html; Excepción: No se puede exponer el atributo de sesión 'Sustoreid' debido a un modelo existente-> <Property Name = "LEGEASIONSIONSoverRide" Value = "True"/> <Property Name = "ExposeRequestAtTributes" value = "true"/> <Property name = "exposeSessionAttributes" value = "true"/> <Property name = "exposespringmacrohelpers" value = "verdadero"/> <n. PageContext.Request, Método de uso de la página: request.ContextPath-> <Property name = "requestContextAttribute" value = "request"/> <Property name = "AttributesMap"> <s map> <!-Definir el nombre del método freeMarker-> <Key de entrada = "Menucall"> <!-Asociado a la clase de herramientas que definimos antes-> <> <> </> <Incition </bean>
b. Escriba una clase para heredar la clase TemplateMethodModel e implementar el método personalizado de Freemarker para controlar si el módulo de página muestra inicio de sesión y almacenar el código de permiso del usuario en la sesión, y luego obtener permiso de la sesión. Aquí hay un ejemplo:
El MenuFunction de la clase pública implementa TemplateMethodModel {@Override Public Object Exec (List Arg0) lanza TemplateModelException {int nivel = Integer.valueOf (arg0.get (0) .ToString ()); // nivel de módulo int modelID = integer.valueOf (arg0.get (1) .ToString ()); // módulo id int count = 0; // Registre la sesión si existe un código de permiso para este módulo httpservletRequest solicitud = ((ServLetRequestatTributes) requestContexTholder.getRequestatTributes ()). GetRequest (); Httpsession session = request.getSession (); Objeto o = session.getAttribute ("info"); if (o == null) return false; Info info = (info) o; Cadena authcode = info.getuser (). GetAuthCode (); // Código de permiso if (authCode.Contains (";")) {String [] Masks = AuthCode.Split (";"); Cadena [] m = máscaras [nivel] .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; }}} do. Use la etiqueta de marca freem en la página y el control de la visualización del módulo está oculta
Dos parámetros en Menucall, el primero es el nivel del módulo y el segundo es la identificación del módulo
Por ejemplo:
<#if MENUCALL (1,122)> <li style = "Line-Height: 250%"> <a href = "#" id = "booknew"> <i> </i> reserva </a> </li> </#if>
Lo anterior es la implementación general del control del módulo de acceso del usuario (permisos).
Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.