如果您看过我的上一个博客,您会知道我列出了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的Blog博客上的JCG合作伙伴 Roger Hughes 通过SSL和Spring Security保护Tomcat应用程序 。

翻译自: https://www.javacodegeeks.com/2012/12/securing-your-tomcat-app-with-ssl-and-spring-security.html

使用SSL和Spring Security保护Tomcat应用程序的安全相关推荐

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

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

  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. 使用JWT和Spring Security保护REST API

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

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

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

  9. spring security:第一个程序

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

最新文章

  1. 【LeetCode-515 | 在每个树行中寻找最大值】
  2. 通信原理中的几个重要概念
  3. Python编程基础:第六节 math包的基础使用Math Functions
  4. 【机器学习】一文解读时间序列基本概念
  5. hortonworks/registry : ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
  6. Springboot — 用更优雅的方式发HTTP请求(RestTemplate详解)
  7. 【100题】第三十六 比赛淘汰问题(谷歌笔试)
  8. leetcode python3 简单题88. Merge Sorted Array
  9. 如何导出无水印_抖音视频怎么去水印 抖音怎么导出无水印视频
  10. atitit.插件体系设计总结o73.doc
  11. visio之图案填充
  12. 计算机技术排除故障网站有哪些,电脑技术交流之常见故障排除【详解】
  13. 去文字,如何用PS快速去除图片上的文字
  14. 东汉唯物主义哲学家——王充
  15. Company interview process
  16. python snmp-cmds get示例
  17. Chisel 手册(中文part1)
  18. “x经济”的2021:Z世代成价值载体,增量发掘与存量博弈共存
  19. 新浪微博Android客户端开发之OAuth认证篇
  20. 《自我分析》卡伦·霍尼阅读笔记1

热门文章

  1. 一起来学ES —— 浅谈Nested结构
  2. POJ3278(BFS入门)
  3. openjdk8 项目结构_OpenJDK织机和结构化并发
  4. zxing qr区域判断_如何在Java中使用Zxing和JFreeSVG创建QR Code SVG?
  5. antlr 语言 库_关于ANTLR的通用库的需求:使用反射来构建元模型
  6. jax-ws和jax-rs_JAX-RS和JSON-P集成
  7. js中使用camel框架_使用Fabric8在Kubernetes中使用Camel和CDI
  8. spring可用于数据层吗_Spring XD用于数据提取
  9. jooq 分页排序_将jOOQ与Spring结合使用:排序和分页
  10. 带有AWS DynamoDB的反应式Spring Webflux