9/10/2011
1
LẬP TRÌNH WEB HƯỚNG JAVA
Bài 18: Kỹ thuật security cho ứng
dụng Web
Giảng viên: ThS. Trịnh Tuấn Đạt
Bộ môn CNPM
Email: trinhtuandat.bk@gmail.com/dattt@soict.hut.edu.vn
Bộ môn Công nghệ Phần mềm
Viện CNTT & TT
Trường Đại học Bách Khoa Hà Nội
1DatTT-DSE-SOICT-HUST
Nội dung
 1. Các vấn đề chung về Security
 2. Các kỹ thuật và yêu cầu về Security trên tầng
Web
 3. Xác thực
 3.1. Kỹ thuật xác thực “HTTP basic authentication”
 3.2. Kỹ thuật xác thực “Form-based authentication”
 3.3. Quản lý Realm
 3.4. Bảo mật Passwords trên đường truyền cho kỹ thuật
Basic & Form-based Authentications
 3.5. Hướng dẫn cài đặt Security cho tầng Web
 3.6. Xác thực Client sử dụng kỹ thuật xác thực dựa trên
Certificate 2DatTT-DSE-SOICT-HUST
Nội dung
 3.7. Digest Authentication
 3.8. Programmatic authentication
 4. Authorization
 4.1. Điều khiển truy cập Declarative ở tầng Web
 4.2. Điều khiển truy cập Programmatic ở tầng
Web
3DatTT-DSE-SOICT-HUST
1. Các vấn đề chung về
Security
4DatTT-DSE-SOICT-HUST
Các vấn đề chung về Security
 Xác thực (Authentication)
 Đảm bảo 1 user đúng là người mà anh ta tuyên bố
 Phân quyền-Authorization (Điều khiển truy cập -
Access control)
 Đảm bảo chỉ những người có quyền truy cập mới được
truy cập tài nguyên
 Người dùng phải được xác thực trước
 Bảo mật-Confidentiality (Chính sách-Privacy)
 Bảo vệ dữ liệu khỏi những kẻ nghe nén/tòmò khi đang
trên đường truyền
5DatTT-DSE-SOICT-HUST
2. Các kỹ thuật và yêu cầu về
Security trên tầng Web
6DatTT-DSE-SOICT-HUST
9/10/2011
2
Các yêu cầu về Security trên tầng Web
 Ngăn người dùng chưa xác thực truy cập vàp các
tài nguyên được “điều khiển truy cập”
 Nếu 1 người dùng chưa xác thực cố gắng truy cập vào tài
nguyên web có “điều khiển truy cập”, web container sẽ
tự động yêu cầu user xác thực (authenticate) trước
 Một khi user đã xác thực, web container (và/hoặc web
components) tiến hành áp dụng điều khiển truy cập
 Ngăn attackers thay đổi hoặc đọc các dữ liệu nhạy
cảm trên đường truyền
 Dữ liệu có thể được bảo vệ nhờ SSL
7DatTT-DSE-SOICT-HUST
Các kỹ thuật về Security trên tầng Web
cần tập trung vào “Authentication”
 Lấy về thông tin danh tính (identity
information) của người dùng cuối
 Thường qua giao diện trên trình duyệt
 Thông tin danh tính người dùng bao gồm
username và password
 Gọi là “logging in”
 Truyền thông tin danh tính lấy được cho web
server
 unsecurely (HTTP) hoặc securely (HTTP trên
SSL)
8DatTT-DSE-SOICT-HUST
Các kỹ thuật về Security trên tầng Web
cần tập trung vào “Authentication” (2)
 Thực hiện kiểm tra danh tính, so khớp với “security
database”
 Web container kiểm tra xem danh tính người dùng có
trùng với danh tính nào trong “security database” phía
sau không
 “Security database” còn được gọi là Realms
 Realms lưu trữ/bảo trì
 Username, password, roles, ...
 Cách thức tổ chức & quản lý các realms phụ thuộc vào
sản phẩm, môi trường
 LDAP, RDBMS, Flat-file, Solaris PAM, Windows AD
9DatTT-DSE-SOICT-HUST
Các kỹ thuật về Security trên tầng Web
cần tập trung vào “Authentication” (3)
 Web container lưu vết lại các user đã xác
thực cho các thao tác HTTP về sau
 Sử dụng trạng thái session được lưu trữ, web
container biết được rằng 1 người dùng khi gửi
các HTTP requests đã được xác thực chưa
 Web container cũng tạo các đối tượng
HttpServletRequest ứng với các HTTP requests
gửi đến
 Đối tượng HttpServletRequest chứa các thông tin
“security context”
 Principal, Role, Username
10DatTT-DSE-SOICT-HUST
Các kỹ thuật về Security trên tầng Web
cần tập trung vào “Access control”
 Access control: điều khiển truy cập
 LTV và những người triển khai ứng dụng
Web (deployer) chỉ định điều khiển truy cập
cho các tài nguyên web (web resources)
 Sử dụng điều khiển truy cập loại Declarative
và/hoặc loại Programmatic
11DatTT-DSE-SOICT-HUST
Các kỹ thuật về Security trên tầng Web
cần tập trung vào “Data confidentiality”
 Data confidentiality: bảo mật dữ liệu
 Cung cấp cơ chế bảo mật (confidentiality)
cho các dữ liệu nhạy cảm (sensitive) trên
đường truyền
 Giữa trình duyệt và web server
 Ví dụ: số thẻ tín dụng (Credit card number)
 Sử dụng SSL
12DatTT-DSE-SOICT-HUST
9/10/2011
3
Các kỹ thuật xác thực trên tầng Web
 Xác thực HTTP cơ bản (HTTP basic authentication)
 Sử dụng hoặc không sử dụng với SSL
 Xác thực sử dụng Form (Form-based
authentication)
 Sử dụng hoặc không sử dụng với SSL
 Xác thực sử dụng Client-certificate (Client-certificate
authentication)
 Phải sử dụng SSL
 Xác thực biến đổi (Digest authentication)
 Không cần sử dụng SSL
13DatTT-DSE-SOICT-HUST
3.1. Kỹ thuật xác thực “HTTP
basic authentication”
14DatTT-DSE-SOICT-HUST
HTTP Basic Authentication
 Web server thu thập thông tin danh tính
người dùng (user name & password) qua 1
hộp thoại trên browser
 Không an toàn vì user name và password ở
dạng “dễ dàng decodable” trên đường truyền
 Kỹ thuật encoding là Base64
 Một người bất kỳ dễ dàng thực hiện decode
 Không được mật mã (encrypted)
  Cần SSL để mật mã password
15DatTT-DSE-SOICT-HUST
Các bước cài đặt “Basic Authentication”
 Thiết lập username, passwords, và roles
(realms)
 Thông báo với web container kỹ thuật đang
sử dụng là Basic authentication
 Chỉ định URLs nào (web resources) cần được
điều khiển truy cập (password-protected)
 Chỉ định URLs nào được cung cấp chỉ với SSL
(data integrity & confidentiality protected)
16DatTT-DSE-SOICT-HUST
Bước 1: Thiết lập username, passwords,
và roles (Realms)
 Các kỹ thuật (Schemes), APIs, và các công cụ thiết
lập usernames, passwords & roles (realms) tùy theo
web container và môi trường (operational
environment)
 Flat-file based, Database, LDAP server
 Passwords có thể ở dạng mã mật (encrypted form) hoặc
không
 Tomcat 4.0 có thể làm việc với các loại realms sau
 Mặc định: file, dạng không mã mật (unencrypted form)
 Relational database (qua JDBCRealm)
 LDAP server (qua LDAPRealm)
17DatTT-DSE-SOICT-HUST
Ví dụ: mặc định trong Tomcat
 <install-dir>/config/tomcat-users.xml
 Dạng không mã mật: không an toàn nhưng dễ cài
đặt và bảo trì
<?xml version='1.0'?>
<tomcat-users>
<role rolename="manager"/>
<role rolename="employee"/>
<role rolename="admin"/>
<user username="sang" password="sangPassword"
roles="manager,employee"/>
</tomcat-users>
18DatTT-DSE-SOICT-HUST
9/10/2011
4
Bước 2: Chỉ dẫn web container sử dụng
kỹ thuật Basic authentication
 Trong file web.xml của ứng dụng web:
<web-app>
...
<security-constraint>...</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>realm name</realm-name>
</login-config>
...
</web-app>
19DatTT-DSE-SOICT-HUST
Bước 3: Chỉ định các URLs được điều
khiển truy cập
<web-app>
...
<security-constraint>
<web-resource-collection>
<web-resource-name>WRCollection</web-resource-name>
<url-pattern>/loadpricelist</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method> <realm-name></realm-name>
</login-config>
...
</web-app>
20DatTT-DSE-SOICT-HUST
Bước 4: Chỉ định các resources
(URLs) áp dụng SSL
<web-app>
...
<security-constraint>
<web-resource-collection>
<web-resource-name>WRCollection</web-resource-name>
<url-pattern>/loadpricelist</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method> <realm-name></realm-name>
</login-config>
...
</web-app>
21DatTT-DSE-SOICT-HUST
3.2. Kỹ thuật xác thực “Form-
based authentication”
22DatTT-DSE-SOICT-HUST
Form-based Authentication
 Ứng dụng Web application thu thập thông tin
danh tính người dùng (user name, password,
và các thông tin khác) qua 1 trang login
thông thường
 Không an toàn vì user name và password ở
dạng “dễ dàng decodable” trên đường truyền
 Kỹ thuật encoding là Base64
 Một người bất kỳ dễ dàng thực hiện decode
 Không được mật mã (encrypted)
  Cần SSL để mật mã password 23DatTT-DSE-SOICT-HUST
Luồng điều khiển
1. Request made by client
2. Is client authenticated?
3. Unauthenticated client
redirected
4. Login form returned to client
5. Client submits login form
6. Authentication Login succeeded,
redirected to resource
7. Authorization Permission tested,
result returned
8. Login failed, redirect to error page
9. Error page returned to client
1
2
Protected
Resource
Login.jsp j_security_check Error.html
Request Response
Page
Login
Form
Error Page
3
6 8
7 4 5 9
24DatTT-DSE-SOICT-HUST
9/10/2011
5
Các bước cài đặt “Form-based
Authentication”
 Thiết lập các username, passwords, and roles
(realms)
 Thông báo với web container kỹ thuật đang sử
dụng là Form-based authentication
 Tạo trang “Login page”
 Tạo trang “Login failure error page”
 Chỉ định URLs nào (web resources) được điều khiển
truy cập (password-protected)
 Chỉ định URLs nào được cung cấp chỉ với SSL (data
integrity & confidentiality protected)
25DatTT-DSE-SOICT-HUST
Bước 1: Thiết lập username, passwords,
và roles (Realms)
 Như trong Basic-authentication
26DatTT-DSE-SOICT-HUST
Bước 2: Chỉ dẫn web container sử dụng
kỹ thuật “Form-based authentication”
 Trong file web.xml của ứng dụng Web:
<web-app>
...
<security-constraint>...</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>realm name</realm-name>
</login-config>
...
</web-app>
27DatTT-DSE-SOICT-HUST
Bước 3: Tạo trang “Login Page”
 Có thể là trang HTML hoặc JSP
 Chứa form HTML như sau:
<FORM ACTION="j_security_check" METHOD="POST">
…
<INPUT TYPE="TEXT" NAME="j_username">
…
<INPUT TYPE="PASSWORD" NAME="j_password">
…
</FORM>
28DatTT-DSE-SOICT-HUST
Bước 4: Tạo trang login fail
 Có thể là trang HTML hoặc JSP
 Nội dung bất kỳ
29DatTT-DSE-SOICT-HUST
Bước 5: Chỉ định URLs nào được điều
khiển truy cập (như trong Basic Auth)
<web-app>
...
<security-constraint>
<web-resource-collection>
<web-resource-name>WRCollection</web-resource-name>
<url-pattern>/loadpricelist</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
<role-name>executive</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method> <realm-name></realm-name>
</login-config>
...
</web-app>
30DatTT-DSE-SOICT-HUST
9/10/2011
6
Bước 6: Chỉ định các resources (URLs) áp
dụng SSL (như trong Basic Auth)
<web-app>
...
<security-constraint>
<web-resource-collection>
<web-resource-name>WRCollection</web-resource-name>
<url-pattern>/loadpricelist</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method> <realm-name></realm-name>
</login-config>
...
</web-app>
31DatTT-DSE-SOICT-HUST
Form-based
Basic vs. Form-based Authentication
• Uses “browser provided
dialog box” to get
username and password
• Only username and
password can be collected
• Might result in different
look and feel
• HTTP Authentication
header is used to convey
username and password
• No good way to enter a
new user name
• Uses “web application
provided login page” to get
username and password
• Custom data can be
collected
• Can enforce consistent look
and feel
• Form data is used to convey
username and password
• Can enter a new user name
via login page
Basic
32DatTT-DSE-SOICT-HUST
3.3. Quản lý Realm
33DatTT-DSE-SOICT-HUST
Quản lý Realm
 Quản lý các thông tin danh tính của người
dùng
 username, password, roles, ...
 Dạng mã mật hoặc không
 Phụ thuộc vào container và các môi trường
tính toán của nó (operational environment)
 Tomcat
 flat file based, RDBMS, LDAP
 GlassFish App server
34DatTT-DSE-SOICT-HUST
Security Roles
 Sử dụng các security roles để “điểu khiển truy cập”
(với cả loại declarative & programmatic)
 Là các abstract roles, không liên quan đến usernames,
passwords, groups của hệ điều hành
 Khi triển khai ứng dụng, các abstract security roles cần
được map với các usernames, passwords, groups của hệ
điều hành
 Trên thực tế, CSDL security realm ngoài (vd: LDAP)
có thể được sử dụng cho cả ứng dụng Web và hệ
điều hành
35DatTT-DSE-SOICT-HUST
Ví dụ: Mặc định trong Tomcat
 <install-dir>/config/tomcat-users.xml
 Dạng không mã mật: không an toàn nhưng dễ cài
đặt và bảo trì
<?xml version='1.0'?>
<tomcat-users>
<role rolename="manager"/>
<role rolename="employee"/>
<role rolename="admin"/>
<user username="sang" password="sangPassword"
roles="manager,employee"/>
</tomcat-users>
36DatTT-DSE-SOICT-HUST
9/10/2011
7
Mặc định trong Tomcat
 Flat file based realm được lưu trong
 <install-dir>/config/tomcat-users.xml
 Có thể thay đổi theo 2 cách
 Bằng tay - mannually
 Sử dụng công cụ admin - admintool
37DatTT-DSE-SOICT-HUST
Ví dụ-công cụ admin của Tomcat
38DatTT-DSE-SOICT-HUST
GlassFish Admin Console
39DatTT-DSE-SOICT-HUST
3.4. Bảo mật Passwords trên đường
truyền cho kỹ thuật Basic & Form-
based Authentications
40DatTT-DSE-SOICT-HUST
Bảo mật Passwords
 Với loại Basic & Form-based authentication, trừ khi
được chỉ định rõ ràng, password sẽ được truyền ở
dạng không được mã mật (Base64)
 Khai báo bảo mật cho pasword như các loại dữ liệu
khác:
 Nếu chọn giá trị CONFIDENTIAL hoặc INTEGRAL trong
<transport-guarantee> (con của <user-data-constraint>),
ràng buộc này sẽ được áp dụng cho tất cả các requests
khớp với các URL patterns định nghĩa trong <web-
resource-collection> (không chỉ trong login)
 Đã sử dụng SSL
41DatTT-DSE-SOICT-HUST
Chế độ bảo mật SSL áp dụng cho tất cả
dữ liệu truyền dẫn, bao gồm cả password
<web-app>
...
<security-constraint>
<web-resource-collection>
<web-resource-name>WRCollection</web-resource-name>
<url-pattern>/loadpricelist</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method> <realm-name></realm-name>
</login-config>
...
</web-app>
42DatTT-DSE-SOICT-HUST
9/10/2011
8
3.5. Hướng dẫn cài đặt
Security cho tầng Web
43DatTT-DSE-SOICT-HUST
Chuyển đối giữa SSL và non-SSL cho các
tài nguyên Web (Web resources)
 Khi đã chuyển sang chế độ SSL, không chấp
nhận các request non-SSL trong session đó
 Vì session ID không ở dạng mã mật, kẻ giả mạo
có thể thực hiện các transaction liên quan đến
dữ liệu nhạy cảm (vd: số thẻ tín dụng)
 Sử dụng Servlet filter để từ chối (reject) mọi
non-SSL requests
44DatTT-DSE-SOICT-HUST
SSL có chi phí cao
 Chỉ sử dụng SSL cho những tài nguyên Web
cần đến security
45DatTT-DSE-SOICT-HUST
Ví dụ demo:
 Download mã nguồn từ:
 http://archive.moreservlets.com/Chapter7.html
 2 ví dụ, sử dụng Basic Auth và Form-based Auth
 hotdotcom-internal.war
 hotdotcom.war
 Thêm usernames, roles (được sử dụng trong
code) thích hợp vào trong môi trường
Tomcat (tomcat-users.xml)
 Khởi động lại Tomcat
46DatTT-DSE-SOICT-HUST
Basic Authentication Demo
 hotdotcom-internal.war
 Financial plan page: cho tất cả các employees
 Business plan page: cho tất cả các executives
 Employee compensation plan: available to all employees
 Thử truy cập trang được “điều khiển truy cập”
 Thử nhập các username & password giả
 Thử nhập đúng username & password nhưng của
người không có quyền truy cập (không có role phù
hợp)
47DatTT-DSE-SOICT-HUST
Basic Authentication Demo
48DatTT-DSE-SOICT-HUST
9/10/2011
9
Truy cập trang được “điều khiển truy cập”
với username giả
49DatTT-DSE-SOICT-HUST
Truy cập các trang “access controlled” với
tài khoản đúng
50DatTT-DSE-SOICT-HUST
Form-based Authentication
51DatTT-DSE-SOICT-HUST
Custom login page
52DatTT-DSE-SOICT-HUST
Custom error page
53DatTT-DSE-SOICT-HUST
3.6. Xác thực Client sử dụng kỹ
thuật xác thực dựa trên
Certificate
54DatTT-DSE-SOICT-HUST
9/10/2011
10
Tại sao cần xác thực dựa trên certificate?
 Xác thực Username/password không thể sử
dụng để xác thực giữa chương trình với
chương trình
 Chứng nhận (Certificates) có thể được dùng để
danh tính (identify) người dùng cuối, tổ chức
thương mai, server, hoặc các software entities
 Cặp Username/password không đem lại đủ
tin cậy
 Certificate có thể chứa nhiều hơn chỉ username
và password
55DatTT-DSE-SOICT-HUST
Xác thực dựa trên certificate
 Xác thực Client
 Server xác thực (verify) danh tính của client
(client's identity)
 Xác thực Server
 client xác thực danh tính của server
 Thực hiện một cách “trong suốt” trong giao tiếp
SSL giữa trình duyệt và web server
 Xác thực lẫn nhau (Mutual authentication)
 Cả server và client xác thực danh tính của nhau
56DatTT-DSE-SOICT-HUST
Định dạng Certificate
 Định dạng chuẩn của Certificate là X.509
 X.509 chỉ đặc tả định dạng của certificate
nhưng không chỉ định rõ cách thức certificate
được trao đổi
 SSL chỉ định rõ cách thức trao đổi các
certificates
57DatTT-DSE-SOICT-HUST
Xác thực Client sử dụng kỹ thuật xác thực
dựa trên certificate
 Client được xác thực bằng cách gửi Client
certificate đến Web server
 Khi server cũng xác thực nó với client, ta gọi là
xác thực lẫn nhau (mutual authentication)
 Tất cả Client (trình duyệt) phải có certificate
của mình
 Vì vậy, không phổ biến như kỹ thuật xác thực
Basic & Form-based authentication
 Sử dụng SSL cho HTTP (HTTPS)
58DatTT-DSE-SOICT-HUST
Chỉ dẫn web container sử dụng kỹ thuật
Client-Cert authentication
 Trong file web.xml của ứng dụng Web
<web-app>
...
<security-constraint>...</security-constraint>
<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>realm name</realm-name>
</login-config>
...
</web-app>
59DatTT-DSE-SOICT-HUST
3.7. Digest Authentication
60DatTT-DSE-SOICT-HUST
9/10/2011
11
Digest Authentication
 User, password được chuyển sang dạng “digested
form” trước khi được gửi cho server
 Người dùng không thể lấy được password gốc từ
password đã được biến đổi
 Chỉ thay đổi 1 bit ở password gốc cũng dẫn đến thay đổi
giá trị của password biến đổi
  user, password không bị lộ trên đường truyền, ngay
cả khi không sử dụng kết nối SSL
 Server so sánh giá trị biến đổi nhận được với giá trị
nó có, nếu trùng, việc xác thực là thành công
61DatTT-DSE-SOICT-HUST
Chỉ dẫn web container sử dụng kỹ thuật
xác thực “Digest authentication”
 Trong file web.xml của ứng dụng Web
<web-app>
...
<security-constraint>...</security-constraint>
<login-config>
<auth-method>DIGEST</auth-method>
<realm-name>realm name</realm-name>
</login-config>
...
</web-app>
62DatTT-DSE-SOICT-HUST
3.8. Xác thực theo kiểu
Programmatic trong tầng Web
63DatTT-DSE-SOICT-HUST
Xác thực theo kiểu Programmatic trong
tầng Web
 Ứng dụng Web có thể tự thực hiện xác thực
 Có nhiều tùy biến hơn (nhưng thường ít mang lại
lợi ích)
 Ít được sử dụng trong thực tế
64DatTT-DSE-SOICT-HUST
Các bước thực hiện
 Kiểm tra xem có authorization header không
 Decode username & password (đã được
encode Base64)
 Kiểm tra cặp username/password
 Nếu đúng, thực hiện điều khiển truy cập tiếp
 Nếu có quyền truy cập, trả về trang mong muốn
 Nếu không, trả về trang thông báo phù hợp
 Nếu không, (xác thực chưa thành công), yêu cầu
gửi lại username & password
65DatTT-DSE-SOICT-HUST
Kiểm tra có authentication Header không
public void doGet(…)… {
// Check if authentication header is present in
// HttpServletRequest. If not, ask for it.
String authorization =
request.getHeader("Authorization");
if (authorization == null) {
askForPassword(response);
} else {
...
66DatTT-DSE-SOICT-HUST
9/10/2011
12
Decode Username và Password
if (authorization == null) {
askForPassword(response);
} else {
String userInfo =
authorization.substring(6).trim();
BASE64Decoder decoder = new BASE64Decoder();
String nameAndPassword =
new String(decoder.decodeBuffer(userInfo));
int index = nameAndPassword.indexOf(":");
String user =
nameAndPassword.substring(0, index);
String password =
nameAndPassword.substring(index+1);
...
67DatTT-DSE-SOICT-HUST
Nếu thành công, trả về trang mong muốn, nếu
không, yêu cầu username & password mới
if (authorization == null) {
askForPassword(response);
} else {
...
// If authentication succeeds, return page.
// Otherwise, ask for correct username & password
if (areEqualReversed(user, password)) {
showStock(request, response);
} else {
askForPassword(response);
}
}
}
68DatTT-DSE-SOICT-HUST
4. Authorization
(Điều khiển truy cập-Access Control)
69DatTT-DSE-SOICT-HUST
4 loại Authorization (điều khiển truy cập)
trên J2EE
 Tầng Web & tầng EJB
 Có thể được sử dụng cùng nhau
 Declarative & Programmatic
 4 loại:
 Điều khiển truy cập Declarative ở tầng Web
 Điều khiển truy cập Programmatic ở tầng Web
 Điều khiển truy cập Declarative ở tầng EJB
 Điều khiển truy cập Programmatic ở tầng EJB
70DatTT-DSE-SOICT-HUST
EJB-tier
Web-tier vs. EJB-tier
• (D) Access control
to Web resources
• (D) Declared in
web.xml
• (D) Enforced by
web container
• (P) Coded in servlet
or JSP
• (D) Access control to
bean methods
• (D) Declared in EJB
deployment descriptor
• (D) Enforced by EJB
container
• (P) Coded in EJB
bean
Web-tier
(D): Declarative (P): Programmatic access control
71DatTT-DSE-SOICT-HUST
Programmatic
Declarative vs. Programmatic
• Access control is
declared in
deployment
descriptor
• Container handles
access control
• Does not handle
fine-grained access
control, it is all or
nothing deal
• Access control is
coded in your
program
• Your code handles
access control
• Can handle fine-
grained access
control, i.e. instance-
based or business
logic based access
control
Declarative
72DatTT-DSE-SOICT-HUST
9/10/2011
13
4.1. Điều khiển truy cập
Declarative ở tầng Web
73DatTT-DSE-SOICT-HUST
Các bước điều khiển truy cập Declarative
ở tầng Web
 Người triển khai (Deployer) thực hiện map định
danh người dùng thật với các security roles (vd:
<install-dir>/config/tomcat-users.xml)
 Deployer khai báo các security roles trong file
web.xml
 Deployer khai báo các URL permissions trong
file web.xml cho mỗi security role
(Đã trình bày trong phần trước! )
74DatTT-DSE-SOICT-HUST
4.2. Điều khiển truy cập
Programmatic ở tầng Web
75DatTT-DSE-SOICT-HUST
Điều khiển truy cập declarative &
Programmatic
 Thường được sử dụng cùng nhau
 Declarative: điều khiển truy cập dựa trên role
 Programmatic: điều khiển truy cập dựa trên từng
thực thể user & dựa trên logic nghiệp vụ
 User instance
 Thời gian trong ngày
 Các Parameters trong request
 Các trạng thái bên trong của các web component
76DatTT-DSE-SOICT-HUST
Các bước thực hiện điều khiển truy cập
Programmatic cho tầng Web
 Thiết lập username, passwords, và roles
(realms)
 LTV viết các đoạn code Servlet xử lý logic
điều khiển truy cập, sử dụng các abstract
security roles
 Trong file web.xml, deployer thực hiện map
các abstract security roles với role trong thực
tế (VD, Tomcat có flat file based, RDBMS,
LDAP)
77DatTT-DSE-SOICT-HUST
Bước 2: LTV viết code Servlet xử lý điều
khiển truy cập
public interface javax.servlet.http.HTTPServletRequest{
...
// Find out who is accessing your web resource
public java.security.Principal getUserPrincipal();
public String getRemoteUser();
// Is the caller in a particular role?
public boolean isUserInRole(String role);
...
}
78DatTT-DSE-SOICT-HUST
9/10/2011
14
Ví dụ: “Employees” chỉ truy cập được
thông tin về lương của chính họ
public double getSalary(String employeeId) {
java.security.Principal userPrincipal =
request.getUserPrincipal();
String callerId = userPrincipal.getName();
// “manager” role can read employee salary information
// employee can read only his/her own salary information
if ( (request.isUserInRole(“manager”)) ||
((request.isUserInRole(“employee”)) &&
(callerId == employeeId)) ) {
// return Salary information for the employee
getSalaryInformationSomehow(employId);
} else {
throw new SecurityException(“access denied”);
}
}
79DatTT-DSE-SOICT-HUST
Bước 3: Deployer thực hiện map các
abstract security roles với các roles thực tế
<web-app>
...
<servlet>
<servlet-name>...</servlet-name>
<servlet-class>...</servlet-class>
<!-- Servlet programmer declared abstract security roles -->
<security-role-ref>
<role-name>manager</role-name> <!-- preexisting -->
<role-link>managerOfAcme</role-link> <!-- alias -->
<security-role-ref>
</servlet>
...
</web-app>
80DatTT-DSE-SOICT-HUST

Bai18 web app_security_scheme

  • 1.
    9/10/2011 1 LẬP TRÌNH WEBHƯỚNG JAVA Bài 18: Kỹ thuật security cho ứng dụng Web Giảng viên: ThS. Trịnh Tuấn Đạt Bộ môn CNPM Email: trinhtuandat.bk@gmail.com/dattt@soict.hut.edu.vn Bộ môn Công nghệ Phần mềm Viện CNTT & TT Trường Đại học Bách Khoa Hà Nội 1DatTT-DSE-SOICT-HUST Nội dung  1. Các vấn đề chung về Security  2. Các kỹ thuật và yêu cầu về Security trên tầng Web  3. Xác thực  3.1. Kỹ thuật xác thực “HTTP basic authentication”  3.2. Kỹ thuật xác thực “Form-based authentication”  3.3. Quản lý Realm  3.4. Bảo mật Passwords trên đường truyền cho kỹ thuật Basic & Form-based Authentications  3.5. Hướng dẫn cài đặt Security cho tầng Web  3.6. Xác thực Client sử dụng kỹ thuật xác thực dựa trên Certificate 2DatTT-DSE-SOICT-HUST Nội dung  3.7. Digest Authentication  3.8. Programmatic authentication  4. Authorization  4.1. Điều khiển truy cập Declarative ở tầng Web  4.2. Điều khiển truy cập Programmatic ở tầng Web 3DatTT-DSE-SOICT-HUST 1. Các vấn đề chung về Security 4DatTT-DSE-SOICT-HUST Các vấn đề chung về Security  Xác thực (Authentication)  Đảm bảo 1 user đúng là người mà anh ta tuyên bố  Phân quyền-Authorization (Điều khiển truy cập - Access control)  Đảm bảo chỉ những người có quyền truy cập mới được truy cập tài nguyên  Người dùng phải được xác thực trước  Bảo mật-Confidentiality (Chính sách-Privacy)  Bảo vệ dữ liệu khỏi những kẻ nghe nén/tòmò khi đang trên đường truyền 5DatTT-DSE-SOICT-HUST 2. Các kỹ thuật và yêu cầu về Security trên tầng Web 6DatTT-DSE-SOICT-HUST
  • 2.
    9/10/2011 2 Các yêu cầuvề Security trên tầng Web  Ngăn người dùng chưa xác thực truy cập vàp các tài nguyên được “điều khiển truy cập”  Nếu 1 người dùng chưa xác thực cố gắng truy cập vào tài nguyên web có “điều khiển truy cập”, web container sẽ tự động yêu cầu user xác thực (authenticate) trước  Một khi user đã xác thực, web container (và/hoặc web components) tiến hành áp dụng điều khiển truy cập  Ngăn attackers thay đổi hoặc đọc các dữ liệu nhạy cảm trên đường truyền  Dữ liệu có thể được bảo vệ nhờ SSL 7DatTT-DSE-SOICT-HUST Các kỹ thuật về Security trên tầng Web cần tập trung vào “Authentication”  Lấy về thông tin danh tính (identity information) của người dùng cuối  Thường qua giao diện trên trình duyệt  Thông tin danh tính người dùng bao gồm username và password  Gọi là “logging in”  Truyền thông tin danh tính lấy được cho web server  unsecurely (HTTP) hoặc securely (HTTP trên SSL) 8DatTT-DSE-SOICT-HUST Các kỹ thuật về Security trên tầng Web cần tập trung vào “Authentication” (2)  Thực hiện kiểm tra danh tính, so khớp với “security database”  Web container kiểm tra xem danh tính người dùng có trùng với danh tính nào trong “security database” phía sau không  “Security database” còn được gọi là Realms  Realms lưu trữ/bảo trì  Username, password, roles, ...  Cách thức tổ chức & quản lý các realms phụ thuộc vào sản phẩm, môi trường  LDAP, RDBMS, Flat-file, Solaris PAM, Windows AD 9DatTT-DSE-SOICT-HUST Các kỹ thuật về Security trên tầng Web cần tập trung vào “Authentication” (3)  Web container lưu vết lại các user đã xác thực cho các thao tác HTTP về sau  Sử dụng trạng thái session được lưu trữ, web container biết được rằng 1 người dùng khi gửi các HTTP requests đã được xác thực chưa  Web container cũng tạo các đối tượng HttpServletRequest ứng với các HTTP requests gửi đến  Đối tượng HttpServletRequest chứa các thông tin “security context”  Principal, Role, Username 10DatTT-DSE-SOICT-HUST Các kỹ thuật về Security trên tầng Web cần tập trung vào “Access control”  Access control: điều khiển truy cập  LTV và những người triển khai ứng dụng Web (deployer) chỉ định điều khiển truy cập cho các tài nguyên web (web resources)  Sử dụng điều khiển truy cập loại Declarative và/hoặc loại Programmatic 11DatTT-DSE-SOICT-HUST Các kỹ thuật về Security trên tầng Web cần tập trung vào “Data confidentiality”  Data confidentiality: bảo mật dữ liệu  Cung cấp cơ chế bảo mật (confidentiality) cho các dữ liệu nhạy cảm (sensitive) trên đường truyền  Giữa trình duyệt và web server  Ví dụ: số thẻ tín dụng (Credit card number)  Sử dụng SSL 12DatTT-DSE-SOICT-HUST
  • 3.
    9/10/2011 3 Các kỹ thuậtxác thực trên tầng Web  Xác thực HTTP cơ bản (HTTP basic authentication)  Sử dụng hoặc không sử dụng với SSL  Xác thực sử dụng Form (Form-based authentication)  Sử dụng hoặc không sử dụng với SSL  Xác thực sử dụng Client-certificate (Client-certificate authentication)  Phải sử dụng SSL  Xác thực biến đổi (Digest authentication)  Không cần sử dụng SSL 13DatTT-DSE-SOICT-HUST 3.1. Kỹ thuật xác thực “HTTP basic authentication” 14DatTT-DSE-SOICT-HUST HTTP Basic Authentication  Web server thu thập thông tin danh tính người dùng (user name & password) qua 1 hộp thoại trên browser  Không an toàn vì user name và password ở dạng “dễ dàng decodable” trên đường truyền  Kỹ thuật encoding là Base64  Một người bất kỳ dễ dàng thực hiện decode  Không được mật mã (encrypted)   Cần SSL để mật mã password 15DatTT-DSE-SOICT-HUST Các bước cài đặt “Basic Authentication”  Thiết lập username, passwords, và roles (realms)  Thông báo với web container kỹ thuật đang sử dụng là Basic authentication  Chỉ định URLs nào (web resources) cần được điều khiển truy cập (password-protected)  Chỉ định URLs nào được cung cấp chỉ với SSL (data integrity & confidentiality protected) 16DatTT-DSE-SOICT-HUST Bước 1: Thiết lập username, passwords, và roles (Realms)  Các kỹ thuật (Schemes), APIs, và các công cụ thiết lập usernames, passwords & roles (realms) tùy theo web container và môi trường (operational environment)  Flat-file based, Database, LDAP server  Passwords có thể ở dạng mã mật (encrypted form) hoặc không  Tomcat 4.0 có thể làm việc với các loại realms sau  Mặc định: file, dạng không mã mật (unencrypted form)  Relational database (qua JDBCRealm)  LDAP server (qua LDAPRealm) 17DatTT-DSE-SOICT-HUST Ví dụ: mặc định trong Tomcat  <install-dir>/config/tomcat-users.xml  Dạng không mã mật: không an toàn nhưng dễ cài đặt và bảo trì <?xml version='1.0'?> <tomcat-users> <role rolename="manager"/> <role rolename="employee"/> <role rolename="admin"/> <user username="sang" password="sangPassword" roles="manager,employee"/> </tomcat-users> 18DatTT-DSE-SOICT-HUST
  • 4.
    9/10/2011 4 Bước 2: Chỉdẫn web container sử dụng kỹ thuật Basic authentication  Trong file web.xml của ứng dụng web: <web-app> ... <security-constraint>...</security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>realm name</realm-name> </login-config> ... </web-app> 19DatTT-DSE-SOICT-HUST Bước 3: Chỉ định các URLs được điều khiển truy cập <web-app> ... <security-constraint> <web-resource-collection> <web-resource-name>WRCollection</web-resource-name> <url-pattern>/loadpricelist</url-pattern> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <role-name>admin</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name></realm-name> </login-config> ... </web-app> 20DatTT-DSE-SOICT-HUST Bước 4: Chỉ định các resources (URLs) áp dụng SSL <web-app> ... <security-constraint> <web-resource-collection> <web-resource-name>WRCollection</web-resource-name> <url-pattern>/loadpricelist</url-pattern> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <role-name>admin</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name></realm-name> </login-config> ... </web-app> 21DatTT-DSE-SOICT-HUST 3.2. Kỹ thuật xác thực “Form- based authentication” 22DatTT-DSE-SOICT-HUST Form-based Authentication  Ứng dụng Web application thu thập thông tin danh tính người dùng (user name, password, và các thông tin khác) qua 1 trang login thông thường  Không an toàn vì user name và password ở dạng “dễ dàng decodable” trên đường truyền  Kỹ thuật encoding là Base64  Một người bất kỳ dễ dàng thực hiện decode  Không được mật mã (encrypted)   Cần SSL để mật mã password 23DatTT-DSE-SOICT-HUST Luồng điều khiển 1. Request made by client 2. Is client authenticated? 3. Unauthenticated client redirected 4. Login form returned to client 5. Client submits login form 6. Authentication Login succeeded, redirected to resource 7. Authorization Permission tested, result returned 8. Login failed, redirect to error page 9. Error page returned to client 1 2 Protected Resource Login.jsp j_security_check Error.html Request Response Page Login Form Error Page 3 6 8 7 4 5 9 24DatTT-DSE-SOICT-HUST
  • 5.
    9/10/2011 5 Các bước càiđặt “Form-based Authentication”  Thiết lập các username, passwords, and roles (realms)  Thông báo với web container kỹ thuật đang sử dụng là Form-based authentication  Tạo trang “Login page”  Tạo trang “Login failure error page”  Chỉ định URLs nào (web resources) được điều khiển truy cập (password-protected)  Chỉ định URLs nào được cung cấp chỉ với SSL (data integrity & confidentiality protected) 25DatTT-DSE-SOICT-HUST Bước 1: Thiết lập username, passwords, và roles (Realms)  Như trong Basic-authentication 26DatTT-DSE-SOICT-HUST Bước 2: Chỉ dẫn web container sử dụng kỹ thuật “Form-based authentication”  Trong file web.xml của ứng dụng Web: <web-app> ... <security-constraint>...</security-constraint> <login-config> <auth-method>FORM</auth-method> <realm-name>realm name</realm-name> </login-config> ... </web-app> 27DatTT-DSE-SOICT-HUST Bước 3: Tạo trang “Login Page”  Có thể là trang HTML hoặc JSP  Chứa form HTML như sau: <FORM ACTION="j_security_check" METHOD="POST"> … <INPUT TYPE="TEXT" NAME="j_username"> … <INPUT TYPE="PASSWORD" NAME="j_password"> … </FORM> 28DatTT-DSE-SOICT-HUST Bước 4: Tạo trang login fail  Có thể là trang HTML hoặc JSP  Nội dung bất kỳ 29DatTT-DSE-SOICT-HUST Bước 5: Chỉ định URLs nào được điều khiển truy cập (như trong Basic Auth) <web-app> ... <security-constraint> <web-resource-collection> <web-resource-name>WRCollection</web-resource-name> <url-pattern>/loadpricelist</url-pattern> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <role-name>admin</role-name> <role-name>executive</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <realm-name></realm-name> </login-config> ... </web-app> 30DatTT-DSE-SOICT-HUST
  • 6.
    9/10/2011 6 Bước 6: Chỉđịnh các resources (URLs) áp dụng SSL (như trong Basic Auth) <web-app> ... <security-constraint> <web-resource-collection> <web-resource-name>WRCollection</web-resource-name> <url-pattern>/loadpricelist</url-pattern> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <role-name>admin</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <realm-name></realm-name> </login-config> ... </web-app> 31DatTT-DSE-SOICT-HUST Form-based Basic vs. Form-based Authentication • Uses “browser provided dialog box” to get username and password • Only username and password can be collected • Might result in different look and feel • HTTP Authentication header is used to convey username and password • No good way to enter a new user name • Uses “web application provided login page” to get username and password • Custom data can be collected • Can enforce consistent look and feel • Form data is used to convey username and password • Can enter a new user name via login page Basic 32DatTT-DSE-SOICT-HUST 3.3. Quản lý Realm 33DatTT-DSE-SOICT-HUST Quản lý Realm  Quản lý các thông tin danh tính của người dùng  username, password, roles, ...  Dạng mã mật hoặc không  Phụ thuộc vào container và các môi trường tính toán của nó (operational environment)  Tomcat  flat file based, RDBMS, LDAP  GlassFish App server 34DatTT-DSE-SOICT-HUST Security Roles  Sử dụng các security roles để “điểu khiển truy cập” (với cả loại declarative & programmatic)  Là các abstract roles, không liên quan đến usernames, passwords, groups của hệ điều hành  Khi triển khai ứng dụng, các abstract security roles cần được map với các usernames, passwords, groups của hệ điều hành  Trên thực tế, CSDL security realm ngoài (vd: LDAP) có thể được sử dụng cho cả ứng dụng Web và hệ điều hành 35DatTT-DSE-SOICT-HUST Ví dụ: Mặc định trong Tomcat  <install-dir>/config/tomcat-users.xml  Dạng không mã mật: không an toàn nhưng dễ cài đặt và bảo trì <?xml version='1.0'?> <tomcat-users> <role rolename="manager"/> <role rolename="employee"/> <role rolename="admin"/> <user username="sang" password="sangPassword" roles="manager,employee"/> </tomcat-users> 36DatTT-DSE-SOICT-HUST
  • 7.
    9/10/2011 7 Mặc định trongTomcat  Flat file based realm được lưu trong  <install-dir>/config/tomcat-users.xml  Có thể thay đổi theo 2 cách  Bằng tay - mannually  Sử dụng công cụ admin - admintool 37DatTT-DSE-SOICT-HUST Ví dụ-công cụ admin của Tomcat 38DatTT-DSE-SOICT-HUST GlassFish Admin Console 39DatTT-DSE-SOICT-HUST 3.4. Bảo mật Passwords trên đường truyền cho kỹ thuật Basic & Form- based Authentications 40DatTT-DSE-SOICT-HUST Bảo mật Passwords  Với loại Basic & Form-based authentication, trừ khi được chỉ định rõ ràng, password sẽ được truyền ở dạng không được mã mật (Base64)  Khai báo bảo mật cho pasword như các loại dữ liệu khác:  Nếu chọn giá trị CONFIDENTIAL hoặc INTEGRAL trong <transport-guarantee> (con của <user-data-constraint>), ràng buộc này sẽ được áp dụng cho tất cả các requests khớp với các URL patterns định nghĩa trong <web- resource-collection> (không chỉ trong login)  Đã sử dụng SSL 41DatTT-DSE-SOICT-HUST Chế độ bảo mật SSL áp dụng cho tất cả dữ liệu truyền dẫn, bao gồm cả password <web-app> ... <security-constraint> <web-resource-collection> <web-resource-name>WRCollection</web-resource-name> <url-pattern>/loadpricelist</url-pattern> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <role-name>admin</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <realm-name></realm-name> </login-config> ... </web-app> 42DatTT-DSE-SOICT-HUST
  • 8.
    9/10/2011 8 3.5. Hướng dẫncài đặt Security cho tầng Web 43DatTT-DSE-SOICT-HUST Chuyển đối giữa SSL và non-SSL cho các tài nguyên Web (Web resources)  Khi đã chuyển sang chế độ SSL, không chấp nhận các request non-SSL trong session đó  Vì session ID không ở dạng mã mật, kẻ giả mạo có thể thực hiện các transaction liên quan đến dữ liệu nhạy cảm (vd: số thẻ tín dụng)  Sử dụng Servlet filter để từ chối (reject) mọi non-SSL requests 44DatTT-DSE-SOICT-HUST SSL có chi phí cao  Chỉ sử dụng SSL cho những tài nguyên Web cần đến security 45DatTT-DSE-SOICT-HUST Ví dụ demo:  Download mã nguồn từ:  http://archive.moreservlets.com/Chapter7.html  2 ví dụ, sử dụng Basic Auth và Form-based Auth  hotdotcom-internal.war  hotdotcom.war  Thêm usernames, roles (được sử dụng trong code) thích hợp vào trong môi trường Tomcat (tomcat-users.xml)  Khởi động lại Tomcat 46DatTT-DSE-SOICT-HUST Basic Authentication Demo  hotdotcom-internal.war  Financial plan page: cho tất cả các employees  Business plan page: cho tất cả các executives  Employee compensation plan: available to all employees  Thử truy cập trang được “điều khiển truy cập”  Thử nhập các username & password giả  Thử nhập đúng username & password nhưng của người không có quyền truy cập (không có role phù hợp) 47DatTT-DSE-SOICT-HUST Basic Authentication Demo 48DatTT-DSE-SOICT-HUST
  • 9.
    9/10/2011 9 Truy cập trangđược “điều khiển truy cập” với username giả 49DatTT-DSE-SOICT-HUST Truy cập các trang “access controlled” với tài khoản đúng 50DatTT-DSE-SOICT-HUST Form-based Authentication 51DatTT-DSE-SOICT-HUST Custom login page 52DatTT-DSE-SOICT-HUST Custom error page 53DatTT-DSE-SOICT-HUST 3.6. Xác thực Client sử dụng kỹ thuật xác thực dựa trên Certificate 54DatTT-DSE-SOICT-HUST
  • 10.
    9/10/2011 10 Tại sao cầnxác thực dựa trên certificate?  Xác thực Username/password không thể sử dụng để xác thực giữa chương trình với chương trình  Chứng nhận (Certificates) có thể được dùng để danh tính (identify) người dùng cuối, tổ chức thương mai, server, hoặc các software entities  Cặp Username/password không đem lại đủ tin cậy  Certificate có thể chứa nhiều hơn chỉ username và password 55DatTT-DSE-SOICT-HUST Xác thực dựa trên certificate  Xác thực Client  Server xác thực (verify) danh tính của client (client's identity)  Xác thực Server  client xác thực danh tính của server  Thực hiện một cách “trong suốt” trong giao tiếp SSL giữa trình duyệt và web server  Xác thực lẫn nhau (Mutual authentication)  Cả server và client xác thực danh tính của nhau 56DatTT-DSE-SOICT-HUST Định dạng Certificate  Định dạng chuẩn của Certificate là X.509  X.509 chỉ đặc tả định dạng của certificate nhưng không chỉ định rõ cách thức certificate được trao đổi  SSL chỉ định rõ cách thức trao đổi các certificates 57DatTT-DSE-SOICT-HUST Xác thực Client sử dụng kỹ thuật xác thực dựa trên certificate  Client được xác thực bằng cách gửi Client certificate đến Web server  Khi server cũng xác thực nó với client, ta gọi là xác thực lẫn nhau (mutual authentication)  Tất cả Client (trình duyệt) phải có certificate của mình  Vì vậy, không phổ biến như kỹ thuật xác thực Basic & Form-based authentication  Sử dụng SSL cho HTTP (HTTPS) 58DatTT-DSE-SOICT-HUST Chỉ dẫn web container sử dụng kỹ thuật Client-Cert authentication  Trong file web.xml của ứng dụng Web <web-app> ... <security-constraint>...</security-constraint> <login-config> <auth-method>CLIENT-CERT</auth-method> <realm-name>realm name</realm-name> </login-config> ... </web-app> 59DatTT-DSE-SOICT-HUST 3.7. Digest Authentication 60DatTT-DSE-SOICT-HUST
  • 11.
    9/10/2011 11 Digest Authentication  User,password được chuyển sang dạng “digested form” trước khi được gửi cho server  Người dùng không thể lấy được password gốc từ password đã được biến đổi  Chỉ thay đổi 1 bit ở password gốc cũng dẫn đến thay đổi giá trị của password biến đổi   user, password không bị lộ trên đường truyền, ngay cả khi không sử dụng kết nối SSL  Server so sánh giá trị biến đổi nhận được với giá trị nó có, nếu trùng, việc xác thực là thành công 61DatTT-DSE-SOICT-HUST Chỉ dẫn web container sử dụng kỹ thuật xác thực “Digest authentication”  Trong file web.xml của ứng dụng Web <web-app> ... <security-constraint>...</security-constraint> <login-config> <auth-method>DIGEST</auth-method> <realm-name>realm name</realm-name> </login-config> ... </web-app> 62DatTT-DSE-SOICT-HUST 3.8. Xác thực theo kiểu Programmatic trong tầng Web 63DatTT-DSE-SOICT-HUST Xác thực theo kiểu Programmatic trong tầng Web  Ứng dụng Web có thể tự thực hiện xác thực  Có nhiều tùy biến hơn (nhưng thường ít mang lại lợi ích)  Ít được sử dụng trong thực tế 64DatTT-DSE-SOICT-HUST Các bước thực hiện  Kiểm tra xem có authorization header không  Decode username & password (đã được encode Base64)  Kiểm tra cặp username/password  Nếu đúng, thực hiện điều khiển truy cập tiếp  Nếu có quyền truy cập, trả về trang mong muốn  Nếu không, trả về trang thông báo phù hợp  Nếu không, (xác thực chưa thành công), yêu cầu gửi lại username & password 65DatTT-DSE-SOICT-HUST Kiểm tra có authentication Header không public void doGet(…)… { // Check if authentication header is present in // HttpServletRequest. If not, ask for it. String authorization = request.getHeader("Authorization"); if (authorization == null) { askForPassword(response); } else { ... 66DatTT-DSE-SOICT-HUST
  • 12.
    9/10/2011 12 Decode Username vàPassword if (authorization == null) { askForPassword(response); } else { String userInfo = authorization.substring(6).trim(); BASE64Decoder decoder = new BASE64Decoder(); String nameAndPassword = new String(decoder.decodeBuffer(userInfo)); int index = nameAndPassword.indexOf(":"); String user = nameAndPassword.substring(0, index); String password = nameAndPassword.substring(index+1); ... 67DatTT-DSE-SOICT-HUST Nếu thành công, trả về trang mong muốn, nếu không, yêu cầu username & password mới if (authorization == null) { askForPassword(response); } else { ... // If authentication succeeds, return page. // Otherwise, ask for correct username & password if (areEqualReversed(user, password)) { showStock(request, response); } else { askForPassword(response); } } } 68DatTT-DSE-SOICT-HUST 4. Authorization (Điều khiển truy cập-Access Control) 69DatTT-DSE-SOICT-HUST 4 loại Authorization (điều khiển truy cập) trên J2EE  Tầng Web & tầng EJB  Có thể được sử dụng cùng nhau  Declarative & Programmatic  4 loại:  Điều khiển truy cập Declarative ở tầng Web  Điều khiển truy cập Programmatic ở tầng Web  Điều khiển truy cập Declarative ở tầng EJB  Điều khiển truy cập Programmatic ở tầng EJB 70DatTT-DSE-SOICT-HUST EJB-tier Web-tier vs. EJB-tier • (D) Access control to Web resources • (D) Declared in web.xml • (D) Enforced by web container • (P) Coded in servlet or JSP • (D) Access control to bean methods • (D) Declared in EJB deployment descriptor • (D) Enforced by EJB container • (P) Coded in EJB bean Web-tier (D): Declarative (P): Programmatic access control 71DatTT-DSE-SOICT-HUST Programmatic Declarative vs. Programmatic • Access control is declared in deployment descriptor • Container handles access control • Does not handle fine-grained access control, it is all or nothing deal • Access control is coded in your program • Your code handles access control • Can handle fine- grained access control, i.e. instance- based or business logic based access control Declarative 72DatTT-DSE-SOICT-HUST
  • 13.
    9/10/2011 13 4.1. Điều khiểntruy cập Declarative ở tầng Web 73DatTT-DSE-SOICT-HUST Các bước điều khiển truy cập Declarative ở tầng Web  Người triển khai (Deployer) thực hiện map định danh người dùng thật với các security roles (vd: <install-dir>/config/tomcat-users.xml)  Deployer khai báo các security roles trong file web.xml  Deployer khai báo các URL permissions trong file web.xml cho mỗi security role (Đã trình bày trong phần trước! ) 74DatTT-DSE-SOICT-HUST 4.2. Điều khiển truy cập Programmatic ở tầng Web 75DatTT-DSE-SOICT-HUST Điều khiển truy cập declarative & Programmatic  Thường được sử dụng cùng nhau  Declarative: điều khiển truy cập dựa trên role  Programmatic: điều khiển truy cập dựa trên từng thực thể user & dựa trên logic nghiệp vụ  User instance  Thời gian trong ngày  Các Parameters trong request  Các trạng thái bên trong của các web component 76DatTT-DSE-SOICT-HUST Các bước thực hiện điều khiển truy cập Programmatic cho tầng Web  Thiết lập username, passwords, và roles (realms)  LTV viết các đoạn code Servlet xử lý logic điều khiển truy cập, sử dụng các abstract security roles  Trong file web.xml, deployer thực hiện map các abstract security roles với role trong thực tế (VD, Tomcat có flat file based, RDBMS, LDAP) 77DatTT-DSE-SOICT-HUST Bước 2: LTV viết code Servlet xử lý điều khiển truy cập public interface javax.servlet.http.HTTPServletRequest{ ... // Find out who is accessing your web resource public java.security.Principal getUserPrincipal(); public String getRemoteUser(); // Is the caller in a particular role? public boolean isUserInRole(String role); ... } 78DatTT-DSE-SOICT-HUST
  • 14.
    9/10/2011 14 Ví dụ: “Employees”chỉ truy cập được thông tin về lương của chính họ public double getSalary(String employeeId) { java.security.Principal userPrincipal = request.getUserPrincipal(); String callerId = userPrincipal.getName(); // “manager” role can read employee salary information // employee can read only his/her own salary information if ( (request.isUserInRole(“manager”)) || ((request.isUserInRole(“employee”)) && (callerId == employeeId)) ) { // return Salary information for the employee getSalaryInformationSomehow(employId); } else { throw new SecurityException(“access denied”); } } 79DatTT-DSE-SOICT-HUST Bước 3: Deployer thực hiện map các abstract security roles với các roles thực tế <web-app> ... <servlet> <servlet-name>...</servlet-name> <servlet-class>...</servlet-class> <!-- Servlet programmer declared abstract security roles --> <security-role-ref> <role-name>manager</role-name> <!-- preexisting --> <role-link>managerOfAcme</role-link> <!-- alias --> <security-role-ref> </servlet> ... </web-app> 80DatTT-DSE-SOICT-HUST