javascript
tomcat使用ssl_使用SSL和Spring Security保护Tomcat应用程序的安全
tomcat使用ssl
如果您看過我的上一個博客,您會知道我列出了Spring Security可以做的十件事 。 但是,在開始認真使用Spring Security之前,您真正要做的第一件事就是確保您的Web應用使用正確的傳輸協議,在這種情況下為HTTPS –畢竟,沒有一個安全的網站是沒有意義的如果您要在互聯網上以純文本格式廣播用戶密碼。 要設置SSL,需要執行三個基本步驟……
創建密鑰庫
您需要的第一件事是包含有效證書的私有密鑰庫,生成其中一個證書的最簡單方法是使用位于$JAVA_HOME/bin目錄中的Java的keytool實用程序。
keytool -genkey -alias MyKeyAlias -keyalg RSA -keystore /Users/Roger/tmp/roger.keystore在以上示例中,
- -alias是密鑰的唯一標識符。
- -keyalg是用于生成密鑰的算法。 您在網絡上找到的大多數示例通常都引用“ RSA”,但是您也可以使用“ DSA”或“ DES”
- -keystore是一個可選參數,用于指定密鑰存儲文件的位置。 如果缺少此參數,則默認位置為$ HOME目錄。
RSA代表Ron Rivest(也是RC4算法的創建者),Adi Shamir和Leonard Adleman
DSA代表數字簽名算法
DES代表數據加密標準
有關keytool及其參數的更多信息, keytool 參閱Jon Svede的Informit文章。
當您運行此程序時,系統會提示您一些問題:
Roger$ keytool -genkey -alias MyKeyAlias -keyalg RSA -keystore /Users/Roger/tmp/roger.keystore Enter keystore password: Re-enter new password: What is your first and last name?[Unknown]: localhost What is the name of your organizational unit?[Unknown]: MyDepartmentName What is the name of your organization?[Unknown]: MyCompanyName What is the name of your City or Locality?[Unknown]: Stafford What is the name of your State or Province?[Unknown]: NA What is the two-letter country code for this unit?[Unknown]: UK Is CN=localhost, OU=MyDepartmentName, O=MyCompanyName, L=Stafford, ST=UK, C=UK correct?[no]: YEnter key password for (RETURN if same as keystore password): 大多數字段是不言自明的; 但是對于名字和名字值,我通常使用機器名-在這種情況下
localhost
更新Tomcat配置
保護應用程序安全的第二步是確保您的tomcat具有SSL連接器。 為此,您需要找到tomcat的server.xml配置文件,該文件通常位于'conf'目錄中。 一旦掌握了這些,并且如果您使用的是tomcat,那么就不用注釋了:
<Connector port='8443' protocol='HTTP/1.1' SSLEnabled='true'maxThreads='150' scheme='https' secure='true'clientAuth='false' sslProtocol='TLS' />…并使它看起來像這樣:
<Connector SSLEnabled='true' keystoreFile='/Users/Roger/tmp/roger.keystore' keystorePass='password' port='8443' scheme='https' secure='true' sslProtocol='TLS'/>請注意,密碼“ password”為純文本格式,不是很安全。 有很多解決方法,但這超出了本博客的范圍。
如果您使用的是Spring的tcServer,那么您會發現它已經具有配置如下的SSL連接器:
<Connector SSLEnabled='true' acceptCount='100' connectionTimeout='20000' executor='tomcatThreadPool' keyAlias='tcserver' keystoreFile='${catalina.base}/conf/tcserver.keystore' keystorePass='changeme' maxKeepAliveRequests='15' port='${bio-ssl.https.port}' protocol='org.apache.coyote.http11.Http11Protocol' redirectPort='${bio-ssl.https.port}' scheme='https' secure='true'/>…在這種情況下,只需編輯各個字段,包括keyAlias,keystoreFile和keystorePass。
配置您的應用
如果現在啟動tomcat并運行您的Web應用程序,您現在會發現可以使用HTTPS訪問它。 例如,鍵入https://localhost:8443/my-app可以,但是http://localhost:8080/my-app也可以。這意味著您還需要對應用程序進行一些jiggery-pokery,以確保它僅響應HTTPS,可以采用兩種方法。
如果您不使用Spring Security,則可以在最后一個web-app標簽之前將以下內容添加到web.xml :
<security-constraint><web-resource-collection><web-resource-name>my-secure-app</web-resource-name><url-pattern>/*</url-pattern></web-resource-collection><user-data-constraint><transport-guarantee>CONFIDENTIAL</transport-guarantee></user-data-constraint> </security-constraint>如果您使用的是Spring Security,那么還有更多步驟可以解決問題。 常規Spring Security設置的一部分是將以下內容添加到您的web.xml文件中。 首先,您需要將一個Spring Security應用程序上下文文件添加到contextConfigLocation context-param :
<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring/root-context.xml/WEB-INF/spring/appServlet/application-security.xml </param-value></context-param>其次,您需要添加Spring Security filter和filter-mapping :
<filter><filter-name>springSecurityFilterChain</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>springSecurityFilterChain</filter-name><url-pattern>/*</url-pattern></filter-mapping>最后,您需要創建或編輯application-security.xml ,如以下非常簡單的示例所示:
<?xml version='1.0' encoding='UTF-8'?> <beans:beans xmlns='http://www.springframework.org/schema/security'xmlns:beans='http://www.springframework.org/schema/beans'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xsi:schemaLocation='http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.1.xsd'><http auto-config='true' ><intercept-url pattern='/**' requires-channel='https' /> </http><authentication-manager></authentication-manager></beans:beans>在上面的示例中,已經設置了intercept-url元素來攔截所有URL,并強制它們使用https通道。
上面的配置詳細信息可能給人的印象是使用簡單的web.xml配置更改會更快,但是如果您已經在使用Spring Security,那么只需在現有配置中添加一個requires-channel屬性即可。
可以在git hub上找到一個名為tomcat-ssl的示例應用程序來演示以上內容,網址為:https://github.com/roghughe/captaindebug
參考:來自Captain Debug博客博客的JCG合作伙伴 Roger Hughes 通過SSL和Spring Security保護Tomcat應用程序 。
翻譯自: https://www.javacodegeeks.com/2012/12/securing-your-tomcat-app-with-ssl-and-spring-security.html
tomcat使用ssl
總結
以上是生活随笔為你收集整理的tomcat使用ssl_使用SSL和Spring Security保护Tomcat应用程序的安全的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果 iPhone 15 Pro / M
- 下一篇: 拉瑞安工作室放出《博德之门 3》纪念动画