DAO认证提供器
9.1. 综述
Spring Security包含了一个产品级别的AuthenticationProvider实现,叫做DaoAuthenticationProvider。 这个认证提供器兼容所有生成UsernamePasswordAuthenticationToken的验证机制,它可能是框架里最常用到的提供器。 与其他认证提供器一样,DaoAuthenticationProvider通过一个UserDetailsService来获得用户名,密码和GrantedAuthority[]。 与其他认证提供器不同的是,这个认证提供器需要获得一个密码,它会根据认证请求对象里的密码来判断认证是否成功。
9.2. 配置
你需要把DaoAuthenticationProvider加入你的ProviderManager列表(如我们在参考指南这章开始讲到的),并确保合适的认证机制配置为UsernamePasswordAuthenticationToken,其他对提供器自己的配置基本和下面一样简单:
<bean id="daoAuthenticationProvider"
class="org.springframework.security.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="inMemoryDaoImpl"/>
<property name="saltSource" ref bean="saltSource"/>
<property name="passwordEncoder" ref="passwordEncoder"/>
</bean>
PasswordEncoder和SaltSource是可选的。 PasswordEncoder为从配置好的UserDetailsService中返回的UserDetails对象里的密码,提供编码和反编码的功能。 SaltSource使用盐值生成密码,这可以提升认证资源密码的安全性。 Spring Security支持MD5,SHA和纯文本编码的PasswordEncoder实现。 Spring Security提供了两种SaltSource实现:SystemWideSaltSource对所有密码都使用相同的盐值进行编码,ReflectionSaltSource,使用返回的UserDetails对象的属性来获得盐值。 请参考JavaDocs获得这些选项的更多信息。
除了上述的属性,DaoAuthenticationProvider还可以为UserDetails对象提供缓存。 UserCache接口可以让DaoAuthenticationProvider把一个UserDetails对象放到缓存里,在以后的认证进程中,如果需要同样的用户名就会重新获得它。 默认情况下,DaoAuthenticationProvider使用NullUserCache,这意味着不使用缓存。 Spring Security也提供了一个可用的缓存实现,EhCacheBasedUserCache,如下配置:
<bean id="daoAuthenticationProvider"
class="org.springframework.security.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService"/>
<property name="userCache" ref="userCache"/>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:/ehcache-failsafe.xml"/>
</bean>
<bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="cacheManager"/>
<property name="cacheName" value="userCache"/>
</bean>
<bean id="userCache" class="org.springframework.security.providers.dao.cache.EhCacheBasedUserCache">
<property name="cache" ref="userCacheBackend"/>
</bean>
所有Spring Security的EH-CACHE实现(包括EhCacheBasedUserCache)要求一个EH-CACHE的Cache对象。 这个Cache对象可以从任何地方获得,不过我们推荐你使用Spring的工厂类,请参考Spring文档获得更多的细节,如何优化缓存存储位置,内存使用,剔除策略,超时等。
Note
大部分情况,你的程序都是有状态的web应用程序,你不需要使用缓存,因为用户的认证信息会被保存在HttpSession里。
我们在设计的时候,决定不支持为DaoAuthenticationProvider加锁,因为这样会加大UserDetailsService接口的复杂度。 比如,一个方法可能需要在认证请求失败的时候进行累加计数。 这些功能都可以利用下面讨论到的应用程序事件发布功能,来轻松实现。
DaoAuthenticationProvider返回一个Authentication对象,包含有principal属性集合。 它的内容可能是String(基本就是用户名)或者是UserDetails对象(是从UserDetailsService获得的)。 默认返回的是UserDetails,它允许添加系统特定的潜在属性,比如用户的全名,邮件地址等。 如果使用容器适配器,或者如果你的程序就是要操作String(就像之前在Spring Security 0.6里发布的情况一样),你应该把你程序里的DaoAuthenticationProvider.forcePrincipalAsString属性设置成true。
分享到:
相关推荐
spring-beans-2.0.xsd
包含翻译后的API文档:spring-security-core-5.3.9.RELEASE-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.springframework.security:spring-security-core:5.3.9.RELEASE; 标签:springframework、security...
struts2-spring-plugin-2.0.11.1.jar,struts2-spring-plugin-2.0.11.1.jar
包含翻译后的API文档:spring-security-oauth2-2.3.5.RELEASE-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.springframework.security.oauth:spring-security-oauth2:2.3.5.RELEASE; 标签:spring、security...
包含翻译后的API文档:spring-security-web-5.2.0.RELEASE-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.springframework.security:spring-security-web:5.2.0.RELEASE; 标签:springframework、security、...
spring-dwr-2.0.xsd spring 与 DWR进行配置
包含翻译后的API文档:spring-boot-actuator-autoconfigure-2.3.12.RELEASE-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.springframework.boot:spring-boot-actuator-autoconfigure:2.3.12....
包含翻译后的API文档:spring-security-core-5.2.0.RELEASE-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.springframework.security:spring-security-core:5.2.0.RELEASE; 标签:springframework、security...
包含翻译后的API文档:spring-security-rsa-1.0.10.RELEASE-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.springframework.security:spring-security-rsa:1.0.10.RELEASE; 标签:spring、rsa、security、...
包含翻译后的API文档:spring-security-oauth2-2.3.5.RELEASE-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.springframework.security.oauth:spring-security-oauth2:2.3.5.RELEASE; 标签:spring...
spring-boot-starter-web-2.0.7.0.jar
spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-2.5.2.jar spring-aop-2.5.4.jar spring-aop-2.5.5.jar spring-aop-2.5.6.jar spring-aop-3.0.0.RELEASE.jar spring-aop-3.0.2.RELEASE.jar spring-aop-sources...
包含翻译后的API文档:spring-security-jwt-1.0.10.RELEASE-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.springframework.security:spring-security-jwt:1.0.10.RELEASE; 标签:spring、security...
包含翻译后的API文档:spring-security-jwt-1.0.10.RELEASE-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.springframework.security:spring-security-jwt:1.0.10.RELEASE; 标签:spring、security、jwt、...
spring上传文件jar包括commons-fileupload-1.2.1.jar和commons-io-2.0.jar。 官方版本,放心使用。
spring-framework-2.0-with-dependencies.zi
包含翻译后的API文档:spring-security-config-4.2.8.RELEASE-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:org.springframework.security,artifactId:spring-security-config,version:4.2.8....
包含翻译后的API文档:spring-security-crypto-5.5.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.springframework.security:spring-security-crypto:5.5.2; 标签:springframework、security、spring、...
spring-framework-2.0-with-dependencies.zip spring-framework-2.0-with-dependencies.zip spring-framework-2.0-with-dependencies.zip spring-framework-2.0-with-dependencies.zip spring-framework-2.0-with-...