Powered by Blogger.

Secure web sử dụng HTTPS

Muốn sử dụng HTTPS với SSL trong Tomcat, bạn phải có 1 số công việc phải làm:
– Tự cấp cho mình 1 chứng chỉ
– Cấu hình cho Tomcat có thể lắng nghe các request https
– Viết web1. Cấp chứng chỉ: Chạy Keytool để sinh file .keystroke

%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA

Password mặc định là changeit
Điền các thông tin vào, ví dụ như hình:
Lệnh này sẽ tạo ra 1 file .keystroke trong thư mục của bạn. Hãy copy file nay2 vào thư mục cài đặt Tomcat.

2. Cấu hình cho Tomcat có thể lắng nghe các request https
Mở file %TOMCAT_HOME%\conf\server.xml lên. Enable phân đoạn sau lên:
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" keystoreFile="Đường dẫn đến TOMCAT_HOME/.keystore" keystorePass="changeit" />

Khởi động Tomcat lên, bạn sẽ thấy như hình:

3. Viết web:
Khi viết web xong, bạn cấu hình secure cho trang web bình thường như các phần trước ngoại trừ ta thêm vào 1 constraint khác như sau:
<security-constraint> <display-name>Constraint1</display-name> <web-resource-collection> <web-resource-name>test</web-resource-name> <description/> <url-pattern>/index.jsp</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <description/> <role-name>manager</role-name> </auth-constraint> <user-data-constraint> <description/> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>DIGEST</auth-method> </login-config> <security-role> <description/> <role-name>manager</role-name> </security-role>

<security-constraint> <display-name>Constraint1</display-name> <web-resource-collection> <web-resource-name>test</web-resource-name> <description/> <url-pattern>/index.jsp</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <description/> <role-name>manager</role-name> </auth-constraint> <user-data-constraint> <description/> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>DIGEST</auth-method> </login-config> <security-role> <description/> <role-name>manager</role-name> </security-role>

Ở đây dòng lệnh<transport-guarantee>CONFIDENTIAL</transport-guarantee> đảm bảo dữ liệu truyền đi đi được mã hóa với SSL.

Chúng ta có 3 giá trị để chọn lựa đó là NONE, CONFIDENTIAL và INTERGRAL. Tất nhiên mỗi loại có tính chất khác nhau.
    Blogger Comment
    Facebook Comment