歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring Security 學習之X.509認證

Spring Security 學習之X.509認證

日期:2017/3/1 9:47:52   编辑:Linux編程

一、基本概念
X.509認證:國際證書格式標准,比較常用的一種認證方式,如日常使用的網銀和支付寶等都在使用。
SSL:是一種安全協議,目的是為網絡通信提供安全及數據完整性保障,SSL在傳輸層中對網絡通信進行加密。

二、X.509證書制作
可以使用JDK自帶的keytool工具制作證書,命令參考一下說明,參數值可以修改。

服務器證書生成:

keytool -genkey -keyalg RSA -dname "cn=localhost,OU=java, O=spring, L=landon, ST=YY, C=CN" -alias server1 -keypass stevex -keystore d:\server1.jks -storepass stevex -validity 3650注:cn=localhost,這裡localhost不能隨便寫,否則客戶端認證時會出錯。

客戶端證書生成(雙向認證才需要):

keytool -genkey -keyalg RSA -dname "CN=stevex, OU=gookle, O=goo, L=landon, ST=RR, C=CN" -alias client1 -storetype PKCS12 -keypass stevex -keystore d:\client1.p12 -storepass stevex -validity 3650
添加信任證書(雙向認證才需要):

##1.導出客戶端證書為cer文件
keytool -export -alias client1 -file d:\client1.cer -keystore d:\client1.p12 -storepass stevex -storetype PKCS12 -rfc
##2.將導出的文件導入服務器證書
keytool -import -v -alias client1 -file d:\client1.cer -keystore d:\server1.jks -storepass stevex

客戶端導入證書(雙向認證才需要):
雙擊client1.p12然後根據提示操作可以將證書導入到IE;對於Firefox, 可以通過Options-->Advanced-->Certificate-->View Certificate-->Import 導入證書。

三、單向SSL認證
1. Spring 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<security:http auto-config='true' use-expressions="true">
<security:intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" requires-channel="https"/>
<security:intercept-url pattern="/list" access="hasRole('ROLE_USER')" requires-channel="https" />
<security:intercept-url pattern="/**" requires-channel="https" />
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service id="userService">
<security:user authorities="ROLE_USER" name="stevex"
password="stevex" />
<security:user authorities="ROLE_USER, ROLE_ADMIN"
name="admin" password="admin" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>注:每個intercept-url element的requires-channel="https"都需要設置

2. Tomcat配置(server.xml):
在server.xml中增加一個Connector配置

<Connector port="8443"
protocol="HTTP/1.1"
SSLEnabled="true" scheme="https" secure="true" clientAuth="false"
sslProtocol="TLS" keystoreType="JKS" keystoreFile="D:/server.jks" keystorePass="stevex" />注:原來的HTTP 8080端口的Connector不能注釋掉,否則無法啟動服務器。這個Connector是新增而不是修改HTTP的,即同時配置兩個Connector.

四、雙向SSL認證
1. Spring配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<security:http auto-config='true' use-expressions="true">
<security:intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" requires-channel="https"/>
<security:intercept-url pattern="/list" access="hasRole('ROLE_USER')" requires-channel="https" />
<security:intercept-url pattern="/**" requires-channel="https" />
<security:x509/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service id="userService">
<security:user authorities="ROLE_USER" name="stevex"
password="" />
<security:user authorities="ROLE_USER, ROLE_ADMIN"
name="admin" password="" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>

注: 增加<x509/>,密碼也不需要了,Spring支持x509已經做得很到位了。

2. Tomcat配置:
在server.xml文件中增加一個Connector配置

<Connector port="8443"
protocol="HTTP/1.1"
SSLEnabled="true" scheme="https" secure="true" clientAuth="true"
sslProtocol="TLS" keystoreType="JKS" keystoreFile="D:/server1.jks" keystorePass="stevex"
truststoreFile="D:/server1.jks" truststorePass="stevex"/>注:增加truststoreFile和truststorePass兩個屬性,同時clientAuth屬性的值更改為true。

3. 運行結果:

Spring Security 學習之X.509認證相關文件下載

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2014年資料/2月/28日/Spring Security 學習之X.509認證

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

Copyright © Linux教程網 All Rights Reserved