Project One: 13th Day 1, menu data Management 2, rights data management 3, role data management 4, user Data Management 5, dynamic query user rights in realm, role 6, Shiro consolidate Ehcache cache permissions Data

Source: Internet
Author: User

1 Course Plan

Menu Data Management

Rights Data Management

Role Data Management

User Data Management

in the Realm in the dynamic query user rights, Roles

S Hiro integrated in Ehcache Cache Permission Data

2 Menu Data Additions2.1 using combotree parent menu item Data

1. page:menu_add.jsp

2, Modify the component style:easyui-combotree, modify the URL tree table treegrid follow down Combotree requires the data format to be basically consistent. Combotree The text by using the text property.

3 . How to use Treegrid components

4. problem : No text content is displayed in the returned data: Resolved

2.2 Submit Form

2.3 server-side finish saving

S Ervice to make a judgment :

3 Permissions Data Module Add

1 , page submission Form

2 , create a three-tier object, add a Save method to the server

Permissions Paging Query

Note: page DataGrid adds page bar properties, adaptive properties

4 Role Data Module4.1 Role Additions

page:pages/system/role_add.jsp

Implementation steps:

1. Page End

A) step one: Use check boxes to show permission data

b) Step two: Use ztree to present menu data in a simple data format

c) step three: Submit the form

2. Service Side

A) first step: Save role data

b) step two: role-related permissions

c) step Three: Role context menu

4.1.1 use check boxes to show permission data

1. Send Request

2. add a query to all permission data in the permissions action

3, in the page Ajax callback parsing data

4.1.2 displaying menu data using ztree

Review Ztree two data types:

To add a method before using it directly: Listjax the returned data JSON standard data format.

--Modified to return data is a simple data format.

In order to return The JSON contains the pId attribute. Therefore, add the corresponding get method in the entity:

Action To Add a new method:

S Ervice:

4.1.3 Submit a role add a form

1. bind event to save button

2. problem: Check data in Ztree component is not committed: Reason: node data is not a checkbox, essentially span Just changing the style dynamically

3, solve: Query ztree API Get tick Check method: Note The method that calls Ztree must first get to ztree Object

Submit a form: You need to set a hidden field in the form form: Hold the selected menu ID

Add hidden fields

4.1.4 server-side Save role

1. create three layers of objects, inject

2, Service

/**

* @Description: 1, save role 2, role context menu 123,345 3, role Association permissions

* @return

*/

public void Save (Role model, String menuids, integer[] permissionids) {

Save the Role object: Persistent State

Roledao.save (model);

Role Context Menu

if (stringutils. Isnotblank(menuids)) {

String[] strings = Menuids.split (",");

for (String menuid:strings) {

Menu menu = Menudao.findone (Integer. parseint(menuId));

persistent state associated persistent State

Model.getmenus (). Add (menu); Add a record to an intermediate table t_role_menu

}

}

Role Association Permissions

if (permissionids!=null && permissionids.length>0) {

for (Integer permissionid:permissionids) {

Permission Permission = new Permission ();

Permission.setid (PermissionID);// managed TODO can also invoke DAO based on ID Query to persistent permission object

persistent state associated with a managed state

Model.getpermissions (). Add (permission);// Adding records to the intermediate table t_role_permission

}

}

}

4.2 Role List Query

5 User Data Module5.1 User Add5.1.1 use check boxes to show role data

page:pages/system/user_add.jsp

After the page loads are complete:

The server invokes the role list to display the request;

5.1.2 Submit Form

5.1.3 The server completes the save user; associate role

A ction:

S Ervice:

5.2 User Paged Query

6 dynamically query the user's permissions in realm, role

when a permission check is required: Four ways to block URLs, annotations, page labels, code levels , the authorization method in realm is called when the authentication permission is required :

/**

* @Description: authorization to the current user

* use Shiro to provide URL Blocking method, call this method to query user rights, role

* use Shiro to provide annotations, call this method to query user permissions, role

* use Shiro page label, call this method to query user rights, role

* @return

*/

protected Authorizationinfo Dogetauthorizationinfo (principalcollection principals) {

list<role> rolelist = null;

list<permission> permissionlist = null;

System. out. println (" Verify that the user has permission --- authorize the user");

Create an Authorization information object

Simpleauthorizationinfo info = new simpleauthorizationinfo ();

Dynamically query permissions roles based on user ID .

if the built-in Administrator account has all roles, all permissions

User user = (user) securityutils. Getsubject (). Getprincipal ();

if (User.getusername (). Equals ("admin")) {

Rolelist = Roledao.findall ();

Permissionlist = Permissiondao.findall ();

}Else{

if the other ordinary user based on the ID Dynamic Query

Rolelist = Roledao.findbyuserid (User.getid ());

Permissionlist = Permissiondao.findbyuserid (User.getid ());

}

if (permissionlist!=null && permissionlist.size () >0) {

for (Permission permission:permissionlist) {

Info.addstringpermission (Permission.getkeyword ()); Addstringpermission cannot be null or an empty string

}

}

if (rolelist!=null && rolelist.size () >0) {

for (Role role:rolelist) {

Info.addrole (Role.getkeyword ());

}

}

return info;

}

D ao:

7 S Hiro Integrated ehCache Cache (Next lesson)

S Hiro Framework Internal integration of the Ehcache environment, only need to configure.

1. provide ehcache cache configuration file

<ehcache xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation= ". /config/ehcache.xsd ">

<!-- disk Data temp directory --

<diskstore path= "Java.io.tmpdir"/>

<!--maxelementsinmemory: maximum number of data stored in memory

Eternal: Whether permanently valid

Timetoidleseconds: In-memory object idle time, per second

Maxelementsondisk: Maximum number of storage on disk

Timetoliveseconds: In-memory object survival time, per second

Diskexpirythreadintervalseconds: Specify clear memory data thread execution time period

Memorystoreevictionpolicy: Clear data policy: LRU: Least Recently used FIFO: First in, out

-

<defaultcache

Maxelementsinmemory= "10000"

Eternal= "false"

Timetoidleseconds= "120"

Timetoliveseconds= "120"

maxelementsondisk= "10000000"

Diskexpirythreadintervalseconds= "120"

memorystoreevictionpolicy= "LRU" >

<persistence strategy= "Localtempswap"/>

</defaultCache>

</ehcache>

2. Configuring the Cache manager

3. inject the cache manager into the security manager

Project One: 13th Day 1, menu data Management 2, rights data management 3, role data management 4, user Data Management 5, dynamic query user rights in realm, role 6, Shiro consolidate Ehcache cache permissions Data

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.