Võ Văn Hải's blog

Chỉ có một điều tuyệt đối đó là mọi thứ đều tương đối…

Programatic security in serlet

Vấn đề đặt ra là ta có trang JSP hay bất cứ loại tài nguyên web nào nào cần được bảo vệ khỏi sự truy xuất không hợp lệ. Yêu cầu ở đây là chúng ta lập trình để cho phép hoặc không đối với 1 người sử dụng.

Trong eclipse, ta tạo 1 project tên Servlet_Security_Programmatic, chọn target run time là Tomcat.

Ta có 1 trang có tên Pay.jsp với nội dung sau

<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;
<HTML>
<HEAD>
<TITLE>Compensation Plans</TITLE>
</HEAD>
<BODY>
<TABLE BORDER=5 ALIGN=”CENTER”>
<TR><TH CLASS=”TITLE”>Compensation Plans</TABLE>
<P>
This is the salary structure for employees. This is shortly incremented.
<H3>Regular Employees</H3>
Pay for medium level employee (Master’s degree, eight year’s
experience):
<UL>
<LI><B>2002:</B> $50,000.
<LI><B>2003:</B> $30,000.
<LI><B>2004:</B> $25,000.
<LI><B>2005:</B> $20,000.
</UL>
<%
out.println(“User dã đăng nhập: “+request.getUserPrincipal().getName());
out.newLine();
//Nếu user đã đăng nhập là thành viên của role manager thì hiển thị
if (request.isUserInRole(“manager”)) {%>
<H3>Executives</H3>
Pay for corporate executives:
<UL>
<LI><B>2002:</B> $500,000.
<LI><B>2003:</B> $600,000.
<LI><B>2004:</B> $700,000.
<LI><B>2005:</B> $800,000.
</UL>
<% }%>
</body>
</html>

Như vậy với bất kỳ người dùng nào đăng nhập thì phần không tô màu đỏ sẽ được thực thi và chỉ những thành viên nào trong role manager hoặc employer truy xuất thì phần này mới được hiển thị.

Ta cấu hình cho web.xml như sau:

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns=”http://java.sun.com/xml/ns/javaee&#8221; xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;
id=”WebApp_ID” version=”2.5″>
<display-name>Servlet_Security_Programmatic</display-name>

<!– Protect compensation plan. Employees or executives. –>
<security-constraint>
<web-resource-collection>
<web-resource-name>Compensation Plan</web-resource-name>
<url-pattern>/Pay.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>manager</role-name>
<role-name>employee</role-name>

</auth-constraint>
</security-constraint>
<!– Server sử dụng BASIC authentication. –>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>default</realm-name>
</login-config>

</web-app>

Trong file tomcat-users.xml của %TOMCAT_HOME%\conf ta cấu hình người dùng như sau:

<?xml version=’1.0′ encoding=’utf-8′?>
<tomcat-users>
<role rolename=”manager”/>
<role rolename=”standard”/>
<role rolename=”admin”/>
<role rolename=”employee”/>
<user username=”ty” password=”ty” roles=”employee”/>
<user username=”teo” password=”teo” roles=”employee,manager”/>
<user username=”admin” password=”admin” roles=”standard,manager,admin”/>
</tomcat-users>

Thự thi ứng dụng. Khi trang popup lên cửa sổ đăng nhập ta nhập username admin, psw admin, kết quả sẽ như sau:
programatic_security_servlet_01.png

Chúc thành công!

3 Responses to “Programatic security in serlet”

  1. Application said

    Chào thầy!
    Em làm theo các bước như trên, nhưng khi chạy không có “cửa sổ đăng nhập” hiện lên. Mà dòng “out.println(”User dã đăng nhập: “+request.getUserPrincipal().getName());” em viết như thế này:
    try{
    out.println(“User da dang nhap: ” +request.getUserPrincipal().getName());
    }
    catch(Exception ex)
    {
    out.println(ex.getMessage());
    }

  2. Application said

    Kết quà là null
    Thầy giúp em!

  3. Application said

    Em đã làm đc rồi
    Cảm ơn thầy!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.