[英]TLS client authentication with Nginx and Tomcat web application
我有一個Tomcat 8 Web應用程序,它在服務器上的端口8080上運行。 使用Nginx為Web應用程序提供服務的端口443的任何傳入請求都轉發到localhost:8080。
我正在嘗試設置相互身份驗證,然后解析該應用程序用於身份驗證的客戶端證書。 然后,應用程序將使用此信息來確定用戶是否應具有admin
或user
權限。 客戶端證書將在“公用名(CN)”字段中包含字符串admin
或user
。
我能夠實現相互身份驗證,以下是當前的nginx ssl.conf,但是問題是證書信息沒有傳遞到tomcat Web應用程序來解析數據。 nginx中是否有一種方法可以傳遞客戶端證書數據,因此tomcat8應用程序可以使用該方法?
server {
listen 443 default_server;
server_name name.domain.com;
ssl on;
ssl_certificate /etc/nginx/self-signed.pem;
ssl_certificate_key /etc/nginx/self-signed.pem;
ssl_protocols SSLv2 TLSv1 TLSv1.1 TLSv1.2;
ssl_client_certificate /etc/nginx/ca.cert.pem;
ssl_verify_client optional;
ssl_verify_depth 2;
ssl_session_timeout 5m;
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
ssl_prefer_server_ciphers on;
port_in_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#test page for the load balancer
location /loadbalancer {
add_header X-Frame-Options "DENY";
auth_basic off;
#proxy_pass http://localhost:8080;
try_files $uri /test.html;
}
location /webapi {
add_header X-Frame-Options "DENY";
auth_basic off;
proxy_pass http://localhost:8080;
}
location / {
if ($ssl_client_verify != SUCCESS)
{
return 403;
break;
}
add_header X-Frame-Options "DENY";
proxy_pass http://localhost:8080;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
您可以使用proxy_set_header指令將其他標頭傳遞給您的tomcat。
可用變量
http://nginx.org/en/docs/http/ngx_http_ssl_module.html#var_ssl_cipher
例
proxy_set_header SSL_DN $ssl_client_s_dn;
在Java應用程序中,您可以閱讀此標頭以進行進一步處理。
附帶說明,我不會將訪問級別保存在證書中,而是保存在服務器端數據庫中,這樣您可以更輕松地重新分配/更改/添加角色或撤銷有效證書。
編輯實際上,nginx也支持證書吊銷列表: http : //nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_crl
關於nginx + php的好文章,可以很容易地適應您的用例:
http://nategood.com/client-side-certificate-authentication-in-ngi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.