Use JSP and Struts to correctly solve the user exit problem (1)

Source: Internet
Author: User

Summary

In a password-protected Web application, it is not only necessary to call the invalidate () method of the HttpSession object to properly handle the user exit process, because most browsers now have a Back) and Forward (Forward) button, allowing the user to move back or Forward to a page.

After a user exits a Web application, if he presses the back button and the browser presents the cached page to the user, the user may be confused and worry about whether their personal data is secure.

In fact, many Web applications will pop up a page, warning the user to close the entire browser when exiting, to prevent the user from clicking the back button. Some others use JavaScript, but this does not necessarily work in some client browsers. Most of these solutions are clumsy and cannot be guaranteed to be 100% effective under any circumstances. At the same time, they also require user experience.

This article describes how to solve the user exit problem with simple program examples. First, it describes an ideal password protection Web application, and then explains how the problem is generated and discusses the solution to the problem in the example program. Although this article discusses JSP, the concepts described by the author are easy to understand and can be used by other Web technologies. Finally, it is more elegant to use Jakarta Struts to solve the user exit problem. This document contains sample programs for JSP and Struts.

Most Web applications do not contain confidential information like bank accounts or credit card data. However, if sensitive data is involved, we need to provide some password protection mechanisms. For example, in a factory, workers must access their schedules, enter their training courses, and view their salaries through Web applications. At this time, the application of SSL (Secure Socket Layer) is a little useful (the SSL page will not be saved in the cache, and the discussion about SSL is beyond the scope of this article ). However, these applications do need some password protection measures. Otherwise, in this case, the user of the Web application can discover the Private confidential information of all employees in the factory.

Similar to the above, computers are also located in public libraries, hospitals, Internet cafes, and other public places. In these places, many users use several computers together, so it is vital to protect users' personal data. At the same time, the good design and implementation of applications require little professional user knowledge and related training.

Let's take a look at how a perfect Web application works in the Real World:

1. Enter a URL in your browser to access a page.

2. The Web application displays a login page, requiring users to enter valid verification information.

3. Enter the user name and password.

4. Assume that the verification information provided by the user is correct. After the verification process, the Web application allows the user to browse the areas he or she has access.

5. When exiting, the user clicks the exit button on the page, and the Web application displays the confirmation page, asking whether the user really needs to exit. Once the user clicks OK, the Session ends and the Web application locates on the logon page again. You can leave without worrying that your information will be leaked.

6. Another user is sitting in front of the same computer. He clicked the back button, and the Web application should not display any page accessed by the previous user.

In fact, the Web application will stay on the login page until the second user provides the correct authentication information before accessing the region where he has permissions.

Through the example program, the article explains how to implement the above functions in a Web application.

I. JSP samples

To more effectively describe this solution, this article will begin with the problems encountered in logoutSampleJSP1, a Web application. This example shows many Web applications that do not properly solve the exit process. LogoutSampleJSP1 contains the following JSP pages: login. jsp, home. jsp, secure1.jsp, secure2.jsp, logout. jsp, loginAction. jsp, and logoutAction. jsp. The page home. jsp, secure1.jsp, secure2.jsp, and logout. jsp is not accessible to unauthenticated users. That is to say, these pages contain important information and should not be displayed in the browser before or after the user logs in or exits. The login. jsp page contains the form for the user to enter the user name and password. The logout. jsp page contains a form that requires the user to confirm whether to exit. LoginAction. jsp and logoutAction. jsp, as controllers, respectively contain the login and exit code.

The second Web example application logoutSampleJSP2 shows how to correct problems in the example logoutSampleJSP1. However, in the second example, logoutSampleJSP2 itself is faulty. Under certain circumstances, the exit problem still exists.

The third Web example application logoutSampleJSP3 has improved logoutSampleJSP2 and properly solved the exit problem.

The last Web example logoutSampleStruts shows how JakartaStruts elegantly solves the exit problem.

2. Login action

The solution is to introduce a custom security implementation mechanism, which provides greater flexibility.

In User-defined authentication methods, the common method is to obtain the authentication information entered by the user from the form submitted by the user, and then to such as LDAP (lightweight directory access protocol) or perform authentication in the security domain of relational database management system (RDBMS. If the authentication information provided by the user is valid, the login action saves an object in the HttpSession object. An object stored in HttpSession indicates that the user has logged on to the Web application. For convenience, only one user name is saved in HttpSession to indicate that the user has logged on. Listing 1 is a piece of code Excerpted from the loginAction. jsp page to explain the login action:

Listing 1

//...
//initialize RequestDispatcher object; set forward to home page by default
RequestDispatcher rd = request.getRequestDispatcher( "home.jsp" );

//Prepare connection and statement
rs = stmt.executeQuery( "select password from USER where userName = '" + userName + "'" );
if (rs.next()) {
//Query only returns 1 record in the result set;
//Only 1 password per userName which is also the primary key
if (rs.getString( "password" ).equals(password)) { //If valid password
session.setAttribute( "User" , userName);

//Saves username string in the session object
}
else { //Password does not match, i.e., invalid user password
request.setAttribute( "Error" , "Invalid password." );

rd = request.getRequestDispatcher( "login.jsp" );
}
} //No record in the result set, i.e., invalid username
else {

request.setAttribute( "Error" , "Invalid user name." );
rd = request.getRequestDispatcher( "login.jsp" );
}
}

//As a controller, loginAction.jsp finally either forwards to "login.jsp" or "home.jsp"
rd.forward(request, response);
//...

The Web application examples attached to this article use relational databases as the security domains, but the content described in this article is also applicable to any other types of security domains.



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.