歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring Security 學習之HTTP基本認證和HTTP摘要認證

Spring Security 學習之HTTP基本認證和HTTP摘要認證

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

HTTP基本認證

1. 簡介
在HTTP中,基本認證是一種用來允許Web浏覽器或其他客戶端程序在請求時提供用戶名和口令形式的身份憑證的一種登錄驗證方式。

在發送之前是以用戶名追加一個冒號然後串接上口令,並將得出的結果字符串再用Base64算法編碼。例如,提供的用戶名是Aladdin、口令是open sesame,則拼接後的結果就是Aladdin:open sesame,然後再將其用Base64編碼,得到QWxhZGRpbjpvcGVuIHNlc2FtZQ==。最終將Base64編碼的字符串發送出去,由接收者解碼得到一個由冒號分隔的用戶名和口令的字符串。

雖然對用戶名和口令的Base64算法編碼結果很難用肉眼識別解碼,但它仍可以極為輕松地被計算機所解碼,就像其容易編碼一樣。編碼這一步驟的目的並不是安全與隱私,而是為將用戶名和口令中的不兼容的字符轉換為均與HTTP協議兼容的字符集。

2. Spring Security配置
Spring對其進行內置支持,所以配置超簡單,<security:http-basic /> 覆蓋自動配置就搞定了。
<security:http auto-config="true">
<security:intercept-url pattern="/hello"
access="ROLE_ADMIN" />
<security:intercept-url pattern="/**" access="ROLE_USER" />
<security:http-basic />
</security:http>

3. 小結
HTTP Basic認證需要浏覽器支持(當然絕大部分浏覽器都支持),彈出的認證窗口是浏覽器實現的而非應用提供的。由於密碼幾乎是以明文方式傳送,所以極其不安全,生產環境一般不會使用這種認證。

HTTP摘要認證
1. 簡介
HTTP摘要認證可以看做是HTTP基本認證的升級版,解決了HTTP基本認證最大的缺點,即將傳送的密碼加密,而且是使用不可逆的MD5加密算法。

2. Spring Security配置
HTTP摘要認證配置稍微復雜點,需要更改entry-point-ref,和增加custom-filter替換原來的HTTP基本認證Filter。
由於使用自定義Filter,所以不能同時使用auto-config。
對DigestAuthenticationFilter bean,我們可以通過passwordAlreadyEncoded屬性指定密碼是否已經加密,Spring Security Web庫中有DigestAuthUtils類可以用來做HTTP 摘要加密,但該類對包外不可見,所以自己可以拷貝實現一個備用。
<?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-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<bean id="digestFilter" class=
"org.springframework.security.web.authentication.www.DigestAuthenticationFilter">
<property name="userDetailsService" ref="userService"/>
<property name="authenticationEntryPoint" ref="digestEntryPoint"/>
</bean>
<bean id="digestEntryPoint" class=
"org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint">
<property name="realmName" value="com.stevex.demo"/>
<property name="key" value="acegi"/>
<property name="nonceValiditySeconds" value="30"/>
</bean>

<security:http entry-point-ref="digestEntryPoint">
<security:intercept-url pattern="/admin"
access="ROLE_ADMIN" />
<security:intercept-url pattern="/list" access="ROLE_USER" />
<security:logout />
<security:custom-filter ref="digestFilter" position="BASIC_AUTH_FILTER" />
</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>

HTTP摘要MD5密碼生成實現
public class DigestUtil {
public static void main(String[] args) {
System.out.println(encodePasswordInA1Format("stevex","com.stevex.demo","stevex"));
}

static String encodePasswordInA1Format(String username, String realm, String password) {
String a1 = username + ":" + realm + ":" + password;
return md5Hex(a1);
}
static String md5Hex(String data) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("No MD5 algorithm available!");
}
return new String(Hex.encode(digest.digest(data.getBytes())));
}
}

3. 小結
摘要訪問認證有意成為一個安全性的折衷,它意圖代替非加密的HTTP基本認證,但是它沒有被設計為替換強認證協議,實際使用也不多。
當然HTTP摘要認證也是需要浏覽器支持的,彈出的驗證窗口來自浏覽器而非應用提供,經過測試,個人感覺Firefox支持比Chrome好。

Spring Security 學習之HTTP基本認證和HTTP摘要認證相關文件下載

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

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

具體下載目錄在 /2014年資料/2月/28日/Spring Security 學習之HTTP基本認證和HTTP摘要認證

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

Copyright © Linux教程網 All Rights Reserved