Java Web Implementation User Rights Management _java

Source: Internet
Author: User

Recently doing a site type project, mainly responsible for the background, UI framework selection for the jquery Easy UI, Project architecture for Spring MVC + Spring jdbc, simple to use well! After building the framework, the first task is designed and implemented with a simple set of rights management functions.

A set of most basic rights management includes users, roles, and resources .

Implementation effect:

Database Design , designed as follows:

Users: User

Roles: Role

User-Role: user_role

Resources: Resource (including superior menu, submenu, button and other resources)

Role-Resources: Role_resource

The standard authority management system is designed for the above 5 sheets.

Note: user, user-role I do not explain, these two are very simple two pieces, the user's crud, as well as for users to assign roles (Many-to-many relationship) a little bit of thinking about it, the following are the implementation of assigning permissions to the role

Background implementation
The display layer adopts Ztree tree

role.jsp

<%@ page contenttype= "Text/html;charset=utf-8"%> <%@ include file= "/views/back/include/taglib.jsp"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

action Layer
Roleaction.java

@RequestMapping (value = "/treedata.jhtml") 
 @ResponseBody public 
 String treedata (httpservletrequest request), Model model) { 
  Dynamicparams params = new Dynamicparams (request); 
  list<map<string, object>> maplist = Lists.newarraylist (); 
 
  Params.put ("Allmenu", "Allmenu"); 
  list<menu> list = authmanager.findmenulist (params); 
 
  list<rolemenu> Rolemenus = authmanager.findrolemenulist (params); 
 
  for (int i = 0; i < list.size (); i++) { 
   Menu e = List.get (i); 
   map<string, object> map = Maps.newhashmap (); 
   Map.put ("id", E.getid ()); 
   Map.put ("PId", E.getparentid ()!= null? E.getparentid (): 0); 
   Map.put ("Name", E.getname ()); 
   for (Rolemenu rolemenu:rolemenus) { 
    if (rolemenu.getmenuid () = = E.getid ()) { 
     map.put ("checked", true); 
    } 
   } 
   Maplist.add (map); 
 
  Return Tojson (maplist); 
 } 

Service Layer

Authmanager.java

Menu Management public list<menu> findmenulist (dynamicparams params) {list<menu> menus = new Arraylist<men 
 
  U> (); 
  if ("Allmenu". Equals (Params.getstring ("Allmenu"))) {menus = menudao.findlist (params); 
   else {//through the user query role list<userrole> userroles = Userroledao.findlist (params); 
   Through the Role query menu list<rolemenu> rolemenus = new arraylist<rolemenu> (); if (userroles!= null && userroles.size () > 0) {for (userrole userrole:userroles) {params = new 
     Dynamicparams (); 
      if (userrole!= null) {if (Userrole.getroleid (). Equals (Params.getstring ("rid")) {break; 
      } params.put ("Roleid", Userrole.getroleid (). toString ()); 
      list<rolemenu> RMS = rolemenudao.findlist (params); 
      for (Rolemenu rolemenu:rms) {rolemenus.add (rolemenu); }}///Query menu information for (Rolemenu Rolemenu:rolemenus) {if (Rolemenu!= null) {menu m ENu = Menudao.find (Rolemenu.getmenuid ()); 
     if (menu!= null) {Menus.add (menu); 
   }} menus = removeduplicate (menus); 
  Collections.sort (menus); 
 return menus; 
 }
/** 
  * Remove Duplicates in menu * 
  * 
  @param list 
  * @return 
 /private list<menu> removeduplicate (list <Menu> list) { 
  list<menu> result = new arraylist<menu> (); 
  set<long> menuids = new hashset<long> (); 
  for (int i = 0; i < list.size (); i++) { 
   Menu m = list.get (i); 
   if (M!= null && menuids.add (M.getid ())) { 
    result.add (m); 
   } 
  } 
  return result; 
 
Public list<rolemenu> findrolemenulist (dynamicparams params) { 
  list<rolemenu> Rolemenus = Rolemenudao.findlist (params); 
  return rolemenus; 
 } 

DAO layer
Menudao

@Override 
 protected void CreateQuery (dynamicparams params, stringbuffer sql, list<object> args) { 
  Sql.append ("Select s.* from Sys_menu s where 1=1"); 
 
  String parentid = params.getstring ("ParentID"); 
  if (Stringutils.isnotblank (ParentID)) { 
   sql.append ("and parent_id =?"); 
   Args.add (ParentID); 
  } 
 
  String sort = params.getstring ("sort"); 
  String order = params.getstring (' order '); 
 
  if (Stringutils.isnotblank (sort)) { 
   sql.append ("Order BY"). Append (Hump2underline (sort)); 
   if (Stringutils.isnotblank (order)) { 
    Sql.append ("" + order); 
   } else { 
    sql.append ("desc"); 
   } 
  } else { 
   Sql.append ("Order by sort Asc,id desc"); 
  } 
  

Userroledao

@Override 
 protected void CreateQuery (dynamicparams params, stringbuffer sql, list<object> args) { 
  Sql.append ("Select s.* from Sys_user_role s where 1=1"); 
  Long Adminid = Params.getlong ("Adminid"); 
  if (Adminid!= null) { 
   sql.append ("and s.user_id =?"); 
   Args.add (Adminid); 
  } 
 

Rolemenudao

@Override 
 protected void CreateQuery (dynamicparams params, stringbuffer sql, list<object> args) { 
  Sql.append ("Select s.* from"). Append ("Sys_role_menu"). Append ("s where 1=1"); 
  Long Adminid = Params.getlong ("Roleid"); 
  if (Adminid!= null) { 
   sql.append ("and s.role_id =?"); 
   Args.add (Adminid); 
  } 
 

In the Web-inf directory to create a folder TLDs Create a custom tag file shiros.tld, we through the custom label to implement Page button control.

<span style= "color: #333333;" ><?xml version= "1.0" encoding= "UTF-8"?> <taglib xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee http://java.sun.com/ Xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "version=" 2.0 "> <description>p2p permission taglib</description > <display-name>permission taglib</display-name> <tlib-version>1.0</tlib-version> < short-name>p2p_back</short-name> <uri>http://vanfon.p2p.cn/</uri> <tag> <descriptio n> permission check tag, have permission to display the contents of the label body, otherwise do not show </description> <name>permission</name> <tag-class> Com.vanfon.p2p.back.tag.permissiontag</tag-class> <body-content>JSP</body-content> < attribute> <description></description> <name>module</name> <required>true< /required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <description></description> <n Ame>code</name> <required>true</required> <rtexprvalue>false</rtexprvalue> 
 ;/attribute> </tag> </taglib></span>

Custom Label class

Package Com.vanfon.p2p.back.tag; 
 
Import java.util.List; 
Import Javax.servlet.http.HttpServletRequest; 
Import javax.servlet.jsp.JspException; 
 
Import Javax.servlet.jsp.tagext.TagSupport; 
Import Com.vanfon.p2p.entity.system.Admin; 
Import Com.vanfon.p2p.entity.system.Menu; 
Import Com.vanfon.p2p.manager.system.AuthManager; 
Import Com.vanfon.p2p.utils.DynamicParams; 
 
Import Com.vanfon.p2p.utils.SpringContextHolder; 
  /** * Permission Control label * * @author ZHANGWX * @date 2015-2-5/public class Permissiontag extends TagSupport {/** 
 
 * */private static final long serialversionuid = 4592227792811389132L; 
 
 Private string module;//property name must be as private string code as the property name of the JSP custom label; 
 Public String GetModule () {return module; 
 public void Setmodule (String module) {this.module = module; 
 Public String GetCode () {return code; 
 public void Setcode (String code) {this.code = code; @Override public int doStartTag () throws Jspexception {Boolean result = false; 
  HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest ()//Get HttpServletRequest object from member variable Admin admin = (admin) request.getsession (). getattribute ("admin")//Get the user who is logged on to the system (Admin!= null) {if ("1". Equals ( 
   String.valueof (Admin.getifsuper ())) {//Super admin result = true; 
    else {dynamicparams params = new Dynamicparams (); 
    Params.put ("id", string.valueof (Admin.getid ())); 
    Params.put ("module", This.module); 
    Params.put ("code", This.code); 
    AuthManager AuthManager = Springcontextholder.getbean (Authmanager.class); 
    list<menu> userroleauths = authmanager.finduserroleauthlist (params); 
    if (userroleauths!= null && userroleauths.size () > 0) {result = true; }} return result? 
 Eval_body_include:skip_body; 

 } 
}

The above is the permissions of the rights management of the general implementation of the tree, completed the implementation of the Java Web User Rights management functions, I hope that the learning of everyone help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.