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 filterfilter-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应用程序的安全相关推荐

  1. 使用SSL和Spring Security保护Tomcat应用程序的安全

    如果您看过我的上一个博客,您会知道我列出了Spring Security可以做的十件事 . 但是,在认真开始使用Spring Security之前,您真正要做的第一件事就是确保您的Web应用使用正确的 ...

  2. gwt格式_使用Spring Security保护GWT应用程序的安全

    gwt格式 在本教程中,我们将看到如何将GWT与Spring的安全模块(即Spring Security)集成. 我们将看到如何保护GWT入口点,如何检索用户的凭据以及如何记录各种身份验证事件. 此外 ...

  3. 使用Spring Security保护GWT应用程序

    在本教程中,我们将看到如何将GWT与Spring的安全模块(即Spring Security)集成在一起. 我们将看到如何保护GWT入口点,如何检索用户的凭据以及如何记录各种身份验证事件. 此外,我们 ...

  4. 使用Spring Security保护REST服务

    总览 最近,我正在一个使用REST服务层与客户端应用程序(GWT应用程序)进行通信的项目中. 因此,我花了很多时间来弄清楚如何使用Spring Security保护REST服务. 本文介绍了我找到的解 ...

  5. Spring Boot + Spring Security + JWT + 微信小程序登录

    Spring Boot + Spring Security + JWT + 微信小程序登录整合教程 参考文章 文章目录 整合思想 整合步骤 1. AuthenticationToken 2. Auth ...

  6. 使用带有OAuth的Spring Security保护资源

    1.简介 在本教程中,我们将研究如何使用Spring Security和OAuth来基于路径模式( / api / ** )保护服务器上的管理资源. 我们配置的另一个路径模式( / oauth / t ...

  7. spring security:第一个程序解析

    上一篇在一个项目里配置了spring security,这里大致说一些这些配置的作用. pom.xml 文件解析 <!-- spring security --><!-- sprin ...

  8. spring security:第一个程序

    spring security虽然已经简化了,但配置还是要小心翼翼的.这里运行起第一个spring security程序. 环境: spring 4.2.4 spring security 4.0.4 ...

  9. 使用JWT和Spring Security保护REST API

    通常情况下,把API直接暴露出去是风险很大的,不说别的,直接被机器攻击就喝一壶的.那么一般来说,对API要划分出一定的权限级别,然后做一个用户的鉴权,依据鉴权结果给予用户开放对应的API.目前,比较主 ...

最新文章

  1. Shell脚本_备份/etc数据
  2. 数据恢复,恢复丢失的数据,突破1G限制!
  3. mysql用索性的好处_MySql索引的优缺点
  4. [转]IP动态切换脚本
  5. 左神算法:单调栈结构(Java版)
  6. AMD GPU+VS2010的OpenCL配置
  7. 不同vlan通信(三层交换)
  8. ROS笔记(16) ArbotiX
  9. jQuery EasyUI combobox多选及赋值
  10. 筑业软件加密锁驱动_如何在不使用额外软件的情况下对USB驱动器进行加密和密码保护...
  11. Ae效果控件快速参考:3D 通道
  12. mysql数据库在哪里写语句_MySQL数据库基本操作以及SQL语句
  13. C#中new一个对象的过程说明
  14. 航测无人机航线规划原理
  15. B站视频下载器推荐(简单又好用)
  16. [经验分享]长期有效的推广网店的方法
  17. 魅族云同步的实践-协议和架构
  18. Java编程的三个就业方向有哪些
  19. 深度学习入门笔记(五):神经网络的编程基础
  20. 勒索软件对企业的重大威胁分析

热门文章

  1. Shell入门(十)之echo
  2. 接口 DataOutput
  3. 信息时代与人工智能时代的教育变革
  4. 判断字符串相等能否用==
  5. ubuntu 16.4 安装postgreSQL,使C++链接到数据库
  6. 单例模式——Java
  7. 作为 IT 行业的过来人,你有什么话想对后辈说的?2
  8. Myeclipse 创建web项目的一些基本操作
  9. 在html页面中怎么打印区域,在HTML中指定打印区域进行打印机打印
  10. 日期相减 python_如果将excel的数字转化为日期(高级教程)