spring的基本认证和摘要认证

#Basic and Digest Authentication 基本摘要认证web

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

##16.1 BasicAuthenticationFilterexpress

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被回调.session

##16.2 DigestAuthenticaitonFilteride

摘要认证处理器用来处理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.

相关文章
相关标签/搜索