2010-11-25 36 views
3

我最近使用SSL和客戶機認證配置了tomcat 6,但唯一能做到的方式是修改服務器配置文件(web.xml,server.xml)。但是因爲我沒有完全控制部署服務器,所以我想爲我的應用程序的某些頁面或url模式配置所有內容,而無需修改主配置文件。用於tomcat中特定頁面/ url模式的SSL

例如: 主服務器:

  • 應用1 - > HTTP
  • 應用2 - > HTTP
  • 所有MyApplication - > HTTPS

如果有人知道如何做到這一點,請告訴我。

回答

8

獲取https的唯一方法是在<service>標記下的server.xml文件中寫入相應的連接器。一旦你設置了連接器,你就可以使用http或https訪問服務器中的所有應用程序。唯一的區別是連接器被使用。通常用於HTTP和HTTPS連接器看起來像這樣:

<Connector port="80" protocol="HTTP/1.1" 
      maxThreads="150" connectionTimeout="20000" 
      redirectPort="443" 
      URIEncoding="UTF-8" compression="on"/> 

<Connector port="443" protocol="HTTP/1.1" 
      maxThreads="150" connectionTimeout="20000" 
      SSLEnabled="true" scheme="https" secure="true" 
      keystoreFile="conf/.keystore" 
      keystorePass="changeit" 
      clientAuth="false" sslProtocol="TLS" 
      URIEncoding="UTF-8" compression="on"/> 

然後,您可以迫使你的應用程序始終使用https通過添加transport-guarantee標籤web.xml這最終是這樣的:

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>Administrators</web-resource-name> 
     <url-pattern>/*</url-pattern> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>Administrators</role-name> 
    </auth-constraint> 
    <user-data-constraint> 
     <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
    </user-data-constraint> 
</security-constraint> 

你可以更改您定義的不同網絡資源的transport-guarantee。因此,您可以保護網站的某些部分而不是其他網站。

server.xml連接器的末尾不會強制您對所有應用程序使用https。它只允許使用https連接器。

+0

感謝您的描述這個問題,對我來說這似乎是要走的路。 – 2010-11-26 13:34:44

+0

只是別的,這個web.xml配置是否會激活客戶端認證?因爲我在server.xml配置中看到'clientAuth =「false」'。 – 2010-11-26 14:26:26

-1

除了@ rmarimon的答案,如果你不想改變的server.xml,你就必須寫一個Filter來檢查你的應用程序的URL,並重定向回HTTP/HTTPS適當。

然而,過濾器仍然需要定義和<filter-mapping>在web.xml