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