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

如果您看过我的上一个博客,您会知道我列出了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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/369750.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

模块 hashlib模块

hashlib模块 提供摘要算法 主要做对比&#xff0c;比较两段代码是否完全一致 不管算法多么不同&#xff0c;摘要功能始终不变&#xff0c; 对同一个字符串进项同一算法摘要得到的值始终不变 MD5值的生成 import hashlib sha1 hashlib.md5() #一定加括号 sha1.update(bytes(a…

css渲染(一) 字体

一、字体属性 1.默认字体系列 chrome/opera:"宋体" firefox:"微软雅黑" safari/IE:Times,"宋体" 2.字体属性 字体类型 font-family  初始化时定义字体类型&#xff0c;如宋体 font-family: arial&#xff0c;“宋体”,“微软雅黑”;   /…

使用SynchronousQueue实现生产者/消费者

Java提供了许多用于并发支持的有用类中&#xff0c;有一个我想谈一谈&#xff1a; SynchronousQueue 。 特别是&#xff0c;我想通过使用方便的SynchronousQueue作为交换机制来完成Producer / Consumer实现。 除非我们了解SynchronousQueue实现的内幕&#xff0c;否则可能不清…

python含多个附件的邮件_Python发送带有多个图像附件的电子邮件

我试图用Python发送一封带有多个图像附件的电子邮件。但是通过下面的代码&#xff0c;我可以在正文中包含第一个图像&#xff0c;但是第二个图像会作为附件附加到电子邮件中。有没有办法可以在HTML的主体中同时获得这两个图像&#xff1f;下面是我当前的代码。在from email.mim…

Oracle存储过程总结

1.存储过程结构 ":"是赋值语句 如: l_name :sky;..."" 是判断是否相等. 如: if 11 then...":" 是变量绑定 如: if :P_NAME sky then... 1.1 第一个存储过程 create or replace procedure proc1( para1 varchar2, para2 out varchar2, para3 in…

图表测试点

测试点1&#xff0c;默认状态下&#xff0c;时间和时间插件还有图表显示一致2&#xff0c;看各种表&#xff08;折线图&#xff0c;柱状图&#xff0c;等&#xff09;与下表格显示一致3&#xff0c;数据库里的与页面上的数据位置显示的数据一致&#xff0c;点击页面 默认的折线…

CSS布局(五) 网页布局方式

网页实质是块与块之间的位置&#xff0c;块挨着块&#xff0c;块嵌套块&#xff0c;块叠着块。 三种关系&#xff1a;相邻&#xff0c;嵌套&#xff0c;重叠。 下面介绍网页布局的常用几种方式 1.一列布局&#xff1a; 一般都是固定的宽高&#xff0c;设置margin : 0 auto来水…

使用Mozilla Persona认证用户的指南

到目前为止&#xff0c;只有Twitter和Facebook身份验证&#xff0c;我决定将Mozilla Persona添加到我最新项目&#xff08; 计算机 &#xff0c;计算机生成的音乐&#xff09;的列表中。 为什么&#xff1f; 我喜欢尝试新事物 存储密码是一个艰巨的过程&#xff0c;尽管我知道…

python字典与json转换_python字典与json转换的方法总结

在python中json分别由列表和字典组成&#xff0c;本文主要介绍python中字典与json相互转换的方法。使用json.dumps可以把字典转成json字符串。使用json.loads可以把json字符串转为字典类型的数据。1、字典转json使用json.dumpsjson.dumps是对python对象编码成json对象&#xff…

变量声明declare,简单运算符运算,变量测试与内容替换

declare -/ 选项 变量名 - 设类型 取消类型 -i 设为整型 -x 设为环境变量 -p 显示类型属性&#xff08;property&#xff09; [rootlocalhost ~]# a1 [rootlocalhost ~]# declare -p a declare -- a"1" [rootlocalhost ~]# export a [rootlocalhost ~]# declare -p …

如何水平居中一个元素

在项目中经常会遇到居中问题&#xff0c;这里借鉴度娘的众多答案&#xff0c;做一个总结&#xff1a; 一、元素的水平居中 1、行级元素的水平居中 <div style"width: 200px;height: 100px;border: 1px solid cyan; text-align: center;"><span>行级元素…

Yammer Metrics,一种监视应用程序的新方法

当您运行诸如Web应用程序之类的长期应用程序时&#xff0c;最好了解一些关于它们的统计信息&#xff0c;例如&#xff0c;服务的请求数&#xff0c;请求持续时间或活动请求数。 但是还有一些更通用的信息&#xff0c;例如内部集合的状态&#xff0c;代码的某些部分被执行了多少…

mysql教程目录_MySql目录(二)

MySql索引(二) 转自&#xff1a; http://www.cnblogs.com/dreamhome/archive/2013/04/16/3025304.html 所有MySQL列类型可以被索引。根据存储引擎定义每个表的最大索引数和最大索引长度。 所有存储引擎支持每个表至少16个索引&#xff0c;总索引长度至少为256字节。大多数存储引…

solr和Lucene的配置方式和应用

solr字段类型 类说明BinaryField二进制数据BoolField布尔值&#xff0c;其中’t’/’T’/’1’都是trueCollationFiled支持Unicode排序CurrencyField支持货币和汇率DateRangeFiled支持索引date rangeExternamFiledFiledpull磁盘上的文件EnumField支持定义枚举值ICUCollationFie…

PostgreSQL 9.6 keepalived主从部署

## 环境&#xff1a; PostgreSQL版&#xff1a;9.6 角色 OS IPmaster CentOS7   10.100.12.73 slave CentOS7 10.100.12.74 vIP 10.1…

CSS——清除浮动的六种解决方案

内容的高度撑起父元素容器的高度&#xff0c;效果图如下HTML和CSS代码如下给&#xff50;标签加上浮动以后&#xff0c;&#xff50;&#xff5b;float&#xff1a;left&#xff1b;&#xff5d;&#xff0c;此时DIV塌陷&#xff0c;两段内容同行显示&#xff0c;效果如下&…

40个Java Collections面试问答

Java Collections Framework是Java编程语言的基本方面。 这是Java面试问题的重要主题之一。 在这里&#xff0c;我列出了Java集合框架的一些重要问题和解答。 什么是Java Collections Framework&#xff1f; 列出Collections框架的一些好处&#xff1f; 集合框架中泛型的好处…

vs mysql iss_MySQL5.7与8.0的连接问题(vs2015\2017)

1.MySQL8.0 root密码忘记重置与5.7不同&#xff0c;绝大多数经验帖不适用8.0https://dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html8.0 重置密码的方式2.MySQL连接vs2015时报错提示&#xff1a;Authentication method ‘caching_sha2_password‘ not supported …

191. Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1 bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11 has binary representation 00000000000000000000000000001011, so the function should return 3. …

AtCoder Beginner Contest 084(AB)

A - New Year 题目链接&#xff1a;https://abc084.contest.atcoder.jp/tasks/abc084_a Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement How many hours do we have until New Year at M oclock (24-hour notation) on 30th, December? Cons…