tomcat 结合spring security 配置SSL认证

小结一下tomcat搭配spring security做ssl,其实单tomcat就可以搞你的应用
ssl认证了,这里只不过顺道使用了spring secruity(如果你的应用是用了
spring security的话)。


1 首先是制作证书了,步骤比较传统,简单带过,不懂的请去google

keytool -genkey -alias MyKeyAlias -keyalg RSA -keystore /home/test.keystore

然后回答一大堆问题,最后生成自签证书

2 tomcat的conf目录中的sevrer.xml中,配置如下:

<Connector SSLEnabled='true' keystoreFile='/home/test.keystore' keystorePass='password' port='8443' scheme='https' secure='true' sslProtocol='TLS'/>


3 如果不使用spring security的话,需要这样在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,则这样:

<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>




<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>


然后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/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/security


http://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,可以很方便配置,哪些连接要用https,哪些不用了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值