2015-10-05 105 views
1

我有一個Tomcat 8 Web應用程序,它在服務器上的端口8080上運行。任何到端口443的傳入請求都會使用Nginx轉發到localhost:8080以提供Web應用程序。使用Nginx和Tomcat Web應用程序進行TLS客戶端身份驗證

我試圖建立相互認證&然後解析用於應用程序認證的客戶端證書。這些信息將被應用程序用來決定用戶是否應具有adminuser權限。客戶證書將在通用名稱(CN)字段中攜帶字符串adminuser

我能夠實現相互認證&下面是當前的nginx ssl.conf但是問題在於cert信息沒有傳遞給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; 
    } 
} 

回答

1

可以使用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

+0

這' proxy_set_header SSL_DN $ ssl_client_s_dn;'確實有效!謝謝。 – OK999

+1

請注意,如果您需要完整證書,則有兩個變量:'ssl_client_cert',其中Nginx將用製表符替換'\ n';和'ssl_client_raw_cert',它有新的行,但會破壞你的HTTP連接。 使用[官方Tomcat SSL閥](https://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/catalina/valves/SSLValve.html)不起作用(僅替換withspaces和而不是標籤) – lucasvc

+0

而對於ssl_client_cert官方的Nginx文檔似乎沒有提到「\ n」被替換。只提到「\ t」是前置的(但\ n實際上被替換):http://nginx.org/en/docs/http/ngx_http_ssl_module.html – Khanna111

相關問題