第十二章 与Spring集成(一) JavaSE应用

来源:互联网 发布:js判断用户是否移动端 编辑:程序博客网 时间:2024/05/16 17:08

跟我学Shiro第12章Demo(仅JAVA SE)


Shiro 的组件都是JavaBean/POJO 式的组件,所以非常容易使用Spring进行组件管理,可以非常方便的从ini配置迁移到Spring进行管理,且支持JavaSE应用及Web 应用的集成。

在示例之前,需要导入shiro-spring及spring-context依赖,具体请参考pom.xml。spring-beans.xml配置文件提供了基础组件如DataSource、DAO、Service组件的配置。


JavaSE应用:

spring-shiro.xml提供了普通JavaSE独立应用的Spring 配置:

<!-- 缓存管理器,使用Ehcache实现 --><bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"><property name="cacheManager" ref="ehCacheManager"></property></bean><bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache.xml"></property><property name="shared" value="true"></property></bean><!-- 凭证匹配器 --><bean id="credentialsMatcher" class="credentials.RetryLimitHashedCredentialsMatcher"><!-- 缓存管理器 --><constructor-arg ref="cacheManager"/><!-- 加密方式 --><property name="hashAlgorithmName" value="md5"/><!-- 循环次数 --><property name="hashIterations" value="2"/><!-- 是否存储散列后的密码为16进制,需要和生成密码时的一样,默认是base64 --><property name="storedCredentialsHexEncoded" value="true"/></bean><!-- Realm实现  --><bean id="userRealm" class="realm.UserRealm"><property name="userService" ref="userService"/><property name="credentialsMatcher" ref="credentialsMatcher"/><!-- 启用缓存 --><property name="cachingEnabled" value="true"/><!-- 启用身份验证缓存,即缓存AuthenticationInfo信息 --><property name="authenticationCachingEnabled" value="true"/><!-- 缓存AuthenticationInfo信息的缓存名称 --><property name="authenticationCacheName" value="authenticationCache"/><!-- 启用授权缓存,即缓存AuthorizationInfo信息 --><property name="authorizationCachingEnabled" value="true"/><!-- 缓存AuthorizationInfo信息的缓存名称 --><property name="authorizationCacheName" value="authorizationCache"/></bean><!-- 会话ID生成器 --><bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/><!-- 会话DAO --><bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"><property name="activeSessionsCacheName" value="shiro-activeSessionCache"/><property name="sessionIdGenerator" ref="sessionIdGenerator"/></bean><!-- 会话验证调度器 --><bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler"><property name="sessionValidationInterval" value="1800000"></property><property name="sessionManager" ref="sessionManager"></property></bean><!-- 会话管理器 --><bean id="sessionManager" class="org.apache.shiro.session.mgt.DefaultSessionManager"><property name="globalSessionTimeout" value="1800000"></property><property name="deleteInvalidSessions" value="true"></property><property name="sessionValidationSchedulerEnabled" value="true"></property><property name="sessionValidationScheduler" ref="sessionValidationScheduler"></property><property name="sessionDAO" ref="sessionDAO"></property></bean><!-- 安全管理器 --><bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager"><property name="realms"><list><ref bean="userRealm"/></list></property><property name="sessionManager" ref="sessionManager"></property><property name="cacheManager" ref="cacheManager"></property></bean><!-- 相当于调用SecurityUtils.setSecurityManager(securityManager) --><bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"><property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"></property><property name="arguments" ref="securityManager"></property></bean><!-- Shiro生命周期处理器 --><bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>

可以看出,只要把之前的ini 配置翻译为此处的spring xml 配置方式即可,无须多解释。

LifecycleBeanPostProcessor 用于在实现了Initializable 接口的Shiro bean 初始化时调用Initializable 接口回调,在实现了Destroyable 接口的Shiro bean 销毁时调用Destroyable 接口回调。

如UserRealm就实现了Initializable,而DefaultSecurityManager实现了Destroyable。具体可以查看它们的继承关系。

原创粉丝点击