Java-different users can see different menus after Logon (User menus can be managed in the background) 1 (55), java User Logon

Source: Internet
Author: User

Java-different users can see different menus after Logon (User menus can be managed in the background) 1 (55), java User Logon
You can view different menus after logging on to different users. (User menus can be managed in the background .) Step 1: analyze the data structure

1: User table

Table Name:Users

Column name

Type

Description

Id

Varchar (32)

Primary Key

Name

Varchar (30)

 

2: menu table

Insert into users values ('u001', 'jack', '123 ');

Insert into users values ('u002 ', 'zhang san', '123 ');

Insert into users values ('u003 ', 'Tom', '123 ');

 

Insert into roles values ('r001', 'postmaster ','');

Insert into roles values ('r002 ', 'instructor ','');

 

Insert into roleuser values ('u001', 'r001 ');

Insert into roleuser values ('u002', 'r002 ');

 

Insert into menus values ('m001', 'System management', '/sys. jsp ');

Insert into menus values ('m002', 'user management', '/user. jsp ');

Insert into menus values ('m003 ', 'Role management','/role. jsp ');

 

Insert into rolemenu values ('m001', 'r001 ');

Insert into rolemenu values ('m002', 'r001 ');

Insert into rolemenu values ('m003 ', 'r001 ');

Insert into rolemenu values ('m003 ', 'r002 ');

 

/* Query a role */

Select u. name, r. name

From users u inner join roleuser ru on u. id = ru. uid

Inner join roles r on ru. rid = r. id;

/* A role has a menu */

Select r. name, m. name

From roles r inner join rolemenu rm on r. id = rm. rid

Inner join menus m on rm. mid = m. id;

 

/* Query a menu owned by someone */

Select u. name, m. name

From users u inner join roleuser ru on u. id = ru. uid

Inner join roles r on ru. rid = r. id

Inner join rolemenu rm on r. id = rm. rid

Inner join menus m on rm. mid = m. id;

Step 3: Create a JavaBean (domain model) and a tool class

Public ClassUser {

PrivateString name;

PrivateString id;

PrivateString pwd;

Tools used to connect to the database:

<Form name ="Name"Action ="<C: url value ='/UserServlet? Cmd = login'/>"Method ="Post">

Name: <input type ="Text"Name ="Name"/> <Br/>

Pwd: <input type ="Text"Name ="Pwd"/> <Br/>

<Input type ="Submit"/>

</Form>

Step 6: Develop the entire usre package

Cn. hx. user.

UserServlet

Service

UserService

Dao

UserDao

Step 7: query the real menu

A menu is a function package of a menu.

Main function, with its ownServvlet-MainServvlet

 

Step 8: Use the pass handler for verification/jsp /*

This is only the first step. It only verifies whether the user has access creden. Authentication.

Filter verification class:

PackageCn. hx. filter;

ImportJava. io. IOException;

ImportJavax. servlet. Filter;

ImportJavax. servlet. FilterChain;

ImportJavax. servlet. FilterConfig;

ImportJavax. servlet. ServletException;

ImportJavax. servlet. ServletRequest;

ImportJavax. servlet. ServletResponse;

ImportJavax. servlet. http. HttpServletRequest;

ImportOrg. apache. commons. dbutils. QueryRunner;

ImportOrg. apache. commons. dbutils. handlers. ScalarHandler;

ImportCn. hx. domain. User;

ImportCn. hx. utils. performanceutils;

 

Public ClassAuthFilterImplementsFilter {

Public VoidInit (FilterConfig filterConfig)ThrowsServletException {

//TODOAuto-generated method stub

}

Public VoidDoFilter (ServletRequest request, ServletResponse response,

FilterChain chain)ThrowsIOException, ServletException {

// Obtain the uri

HttpServletRequest req = (HttpServletRequest) request;

String uri = req. getRequestURI (); // Http: // localhost: 8080/day20/jsps/role. jsp-> day20/jsps/role. jsp

Uri = uri. replace (req. getContextPath (),"");

// SQL

String SQL = "SELECT COUNT (1)" +

"FROM menus m inner join rolemenu rm ON m. id = rm. mid" +

"Inner join roles r ON r. id = rm. rid" +

"Inner join roleuser ru ON r. id = ru. rid" +

"WHERE ru. uid =? AND url =? ";

// Obtain the user id

User user = (User) req. getSession (). getAttribute ("user ");

Try{

QueryRunner run =NewQueryRunner (performanceutils.GetDatasSource());

Object o = run. query (SQL,NewScalarHandler (), user. getId (), uri );

IntSize = Integer.ParseInt(O. toString ());

If(Size = 0 ){

System.Err. Println ("You do not have permission ....");

}Else{

Chain. doFilter (req, response );

}

}Catch(Exception e ){

}

}

Public VoidDestroy (){

//TODOAuto-generated method stub

}

}

Flowchart

Authentication process

Logon Process:

Display menu process:

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.