spring的基本认证和摘要认证

本文深入探讨了HTTP认证机制中的基本认证(Basic Authentication)与摘要认证(Digest Authentication),详细介绍了这两种认证方式的工作原理、配置方法及安全性考量。

#Basic and Digest Authentication 基本摘要认证

通常会整合到基于form表单的认证的应用中,该应用使用一个基于浏览器的用户接口和web-service.但是,基本认证把密码当作平常文本运输,实际上只在无需加密的传输层使用,如https.

##16.1 BasicAuthenticationFilter

BasicAuthenticationFilter 被用来处理在http头部的基本认证资格.它用来验证spring的远程协议(例如Hessian,Burlap),以及一般的浏览器用户代理.http基本认证是由RFC1945,11节来定义的,BasicAuthenticationFilter也是有RFC来定义的.基本认证是一个非常吸引人的认证,因为它的用户代理和实现的部署非常简单.(只需要把用户名:密码用base64来加密,在http的header中指定即可).

###16.1.1 Configuration 你需要在你的拦截链里配置BasicAuthenticationFilter.应用的上下文应该包含BasicAuthenticationFilter及其必要的协助对象.

<bean id="basicAuthenticationFilter"
class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="authenticationEntryPoint" ref="authenticationEntryPoint"/>
</bean>

<bean id="authenticationEntryPoint"
class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
<property name="realmName" value="Name Of Your Realm"/>
</bean>

AuthenticationManager处理每个认证请求.如果authentication失败,那么配置的AuthenticaionEntryPoint将重试认证过程.通常你要和BasicAuthenticationEntryPoint一起使用,它会返回401的响应并附带一个合适的头来重试Http基本认证.如果认证成功,那么生成的Authentication对象将放到SecurityContextHolder中.

如果认证成功或不进行认证尝试时(当http头不包含支持认证的请求),那么拦截链会继续.当认证失败拦截链会中断,且AuthenticationEntryPoint被回调.

##16.2 DigestAuthenticaitonFilter

摘要认证处理器用来处理http头部的摘要认证消息.摘要认证主要用来解决基本认证的弱点,特别是保证认证证书在传输中不以文本形式发送.许多用户代理支持摘要认证,如Firefox,IE.HTTP Digest标准是由RFC 2617来定义的.spring security的DigestAuthenticationFilter兼容了RFC 2617的'auth'级别的防护,但对2069的做了兼容.Digest认证吸引点:使用不加密的http协议,你就可以做到最大程度的安全认证.另外,Digest认证是WebDAV协议的必需品.

你不应该在现在的应用中是用Digest,因为你需要以平常文本,加密或者MD5的方式存储密码.这些存储方式被认为是不安全的.相反,你需要使用一种方式来适配密码哈希.

摘要认证的核心是一个"nonce",这是服务器产生的值.spring security的nonce适配以下格式.

base64(expirationTime + ":" + md5Hex(expirationTime + ":" + key))
expirationTime:   The date and time when the nonce expires, expressed in milliseconds
key:              A private key to prevent modification of the nonce token

DigestAuthenticationEntryPoint有一个属性来指定nonce令牌的key值,用nonceValiditySeconds来指定过期时间.diges是通过各种字符串进行计算的,包括用户名,密码,nonce,请求路径,客户端的nonce,real名称等,然后执行MD5的散列.当服务器和用户代理同事执行摘要计算时,如果接受到的数据不一样(如密码不被接受),会产生不同结果.spring实现中,服务器产生的nonce很少过期,DigestAuthenticationEntryPoint会发送一个"stale=true"的头.它告诉浏览器不需要打扰用户(假设用户名,密码正确),只需重新申请一个新的nonce值.

DigestAuthenticationEntryPoint的nonceValiditySeconds的值取决于你的应用.非常重视安全的应用应该注意一个拦截认证头可以被模拟用户直到nonce中的expirationTime过期.这个是一个选择合适设置的关键点,但是对于不在TLS/https上运行的应用来说,这个极大的不安全.

因为有很多Digest认证的复杂实现,所有大部分是浏览器问题.例如,在相同的session下,IE无法在随后的请求中提供一个'opaque'令牌.spring security因此会将所有状态信息封装到'nonce'中.在我们测试中,spring security的实现和fireFox,IE协助中,会正确的处理nonce超时.

###16.2.1 Configuration 在拦截链中定义DigestAuthenticationFilter.

<bean id="digestFilter" class=
	"org.springframework.security.web.authentication.www.DigestAuthenticationFilter">
<property name="userDetailsService" ref="jdbcDaoImpl"/>
<property name="authenticationEntryPoint" ref="digestEntryPoint"/>
<property name="userCache" ref="userCache"/>
</bean>

<bean id="digestEntryPoint" class=
	"org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint">
<property name="realmName" value="Contacts Realm via Digest Authentication"/>
<property name="key" value="acegi"/>
<property name="nonceValiditySeconds" value="10"/>
</bean>

配置UserdetailsService是必须的,因为DigestAuthenticationFilter必须直接访问一个用户清晰的文本密码.如果你在你的DAO中使用编码密码,Digest Authentication则不会工作.DAO的写作者,UserCache,一般会和DaoAutnenticationProvider共享.这个authenticationEntryPoint属性必须是DigestAuthenticationEntryPoint,这样DigestAuthenticationFilter 才会在摘要计算中获取正确的realName和key.

认证成功,则在SecurityContextHolder中授予正确的Authentication请求token.如果没有触发拦截或拦截不成功,拦截链继续进行.其他情况触发了但验证失败,则拦截链会中断,并调用AuthenticationEntryPoint.

转载于:https://my.oschina.net/u/1590027/blog/915029

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值