acegi学习随感
自接触到acegi认证体系来,在网上搜了些资料,到目前为止,做个笔记,来加深对acegi的学习
acegi1.04,spring2.0
acegi 配置,涉及登陆验证filter,没有Interceptor
xml 代码- xml version="1.0" encoding="UTF-8"?>
- >
- <beans>
- <bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
- <property name="filterInvocationDefinitionSource">
- <value>
- CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
- PATTERN_TYPE_APACHE_ANT
- /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter
- value>
- property>
- bean>
- beans>
Fiter chain说明:
1. httpSessionContextIntegrationFilter:
每次request前 HttpSessionContextIntegrationFilter从Session中获取Authentication对象,在request完后又把Authentication对象保存到Session中供下次request使用,此filter必须其他Acegi filter前使用
在上述代码添加下列段,实例化中属性context默认为SecurityContextImpl.class
xml 代码- <bean id="httpSessionContextIntegrationFilter" class="org.acegisecurity.context.HttpSessionContextIntegrationFilter" />
2. authenticationProcessingFilter:
通过Providers提供认证者列表,如果一个认证提供者失败可以尝试另外一个认证提供者,以保证获取不同来源的身份认证,如
DaoAuthenticationProvider 从数据库中读取用户信息验证身份,可自定义验证身份
AnonymousAuthenticationProvider 匿名用户身份认证
RememberMeAuthenticationProvider 已存cookie中的用户信息身份认证
RunAsImplAuthenticationProvider 对身份已被管理器替换的用户进行验证
其它的还有
AuthByAdapterProvider 使用容器的适配器验证身份
CasAuthenticationProvider 根据Yale中心认证服务验证身份, 用于实现单点登陆
JaasAuthenticationProvider 从JASS登陆配置中获取用户信息验证身份
RemoteAuthenticationProvider 根据远程服务验证用户身份
X509AuthenticationProvider 从X509认证中获取用户信息验证身份
TestingAuthenticationProvider 单元测试时使用
每个认证者会对自己指定的证明信息进行认证,如DaoAuthenticationProvider仅对UsernamePasswordAuthenticationToken这个证明信息进行认证。
本人代码如下:DaoAuthenticationProvider,AnonymousAuthenticationProvider ,RememberMeAuthenticationProvider ,RunAsImplAuthenticationProvider ,只需其中的一个通过认证,即可
- <bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
- <property name="providers">
- <list>
- <ref local="daoAuthenticationProvider" />
- <ref local="anonymousAuthenticationProvider" />
- <ref local="rememberMeAuthenticationProvider" />
- <ref local="runAsAuthenticationProvider" />
- list>
- property>
- bean>
2.1.基于数据库的认证提供者daoAuthenticationProvider:
authenticationDao 认证数据访问对象,用于获取用户信息,包括:用户名,用户密码,用户状态和用户权限
可自定义,本人采用继承org.acegisecurity.providers.dao.DaoAuthenticationProvider的DAO,覆盖retrieveUser和additionalAuthenticationChecks方法,其中的字段userDetailsService,可以通过两种inMemory 或 jdbcDaoImpl获取用户信息,包括:用户名,用户密码,用户状态和用户权限,也可以通过自定义其他方法获取用户信息,覆盖loadUserByUsername方法,本人采用后一种
userCache ehcache 缓存user信息
- <bean id="daoAuthenticationProvider" class="com.d3.gamestat.web.AuthenticationProviderDao">
- <property name="userDetailService"><ref local="jdbcHbmServiceImp"/>property>
- <property name="userCache"><ref local="userCache"/>property>
- bean>
- <bean id="jdbcHbmServiceImp" class="com.d3.gamestat.web.JdbcHbmServiceImp">
- <property name="serviceProxy"><ref bean="gameStatServiceProxy"/>property>
- <property name="serviceId"><value>scurityUserManagervalue>property>
- <property name="methodName"><value>getUservalue>property>
- bean>
- <bean id="userCache" class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
- <property name="cache"><ref local="userCacheBackend"/>property>
- bean>
- <bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
- <property name="cacheManager"><ref local="cacheManager"/>property>
- <property name="cacheName"><value>userCachevalue>property>
- bean>
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
2.2认证提供者anonymousAuthenticationProvider,用于认证匿名用户,字段key中是org.acegisecurity.providers.anonymous.AnonymousProcessingFilter中的key字段
xml 代码- <bean id="anonymousAuthenticationProvider" class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider">
- <property name="key"><value>sslvalue>property>
- >
2.3认证提供者rememberMeAuthenticationProvider,用于认证 cookies 登陆,字段key中是org.acegisecurity.ui.rememberme.RememberMeProcessingFilter中rememberMeServices中的key字段
xml 代码- <bean id="rememberMeAuthenticationProvider" class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider">
- <property name="key"><value>springRocksvalue>property>
- >
2.4认证提供者runAsAuthenticationProvider,用于认证 更改用户信息,字段key中是org.acegisecurity.runas.RunAsManagerImpl中的key字段
xml 代码- <bean id="runAsAuthenticationProvider" class="org.acegisecurity.runas.RunAsImplAuthenticationProvider">
- <property name="key"><value>runAsvalue>property>
- bean>
3.anonymousProcessingFilter
anonymousProcessingFilter的作用是判断ContextHolder中是否有Authentication对象,如果没有就创建一个Authentication对象,其中包含的用户名是anonymousUser,用户权限是ROLE_ANONYMOUS。这使得没有登录的匿名用户能够自动的获得匿名的用户名和权限
xml 代码- <bean id="anonymousProcessingFilter" class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter">
- <property name="key"><value>sslvalue>property>
- <property name="userAttribute">
- <value>anonymousUser,ROLE_ANONYMOUSvalue>
- property>
- bean>
4.exceptionTranslationFilter:
用户尚未通过身份验证时,会将控制转交到一个认证入口点,提供三种实现
BasicProcessingFilterEnteyPoint :HTTP基本认证处理
AuthenticationProcessingFilterEntryPoint :将用户重新定向到一个基于HTML表单的登入界面
CasProcessingFilterEntryPoint :将用户重新定向到一个基于Yale CAS登入界面
- <bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
- <property name="authenticationEntryPoint">
- <bean class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
- <property name="loginFormUrl" value="/login.jsp"/>
- <property name="forceHttps" value="false"/>
- bean>
- property>
- <property name="accessDeniedHandler">
- <bean class="org.acegisecurity.ui.AccessDeniedHandlerImpl">
- <property name="errorPage" value="/login.jsp"/>
- bean>
- property>
- bean>
编辑器总出问题,本想全部发布在上面,无奈,先写这些,鉴于本人能力有限,希望给某些人一点参考
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
