详解spring与shiro集成-创新互联

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="cacheManagerConfigFile" value="classpath:ehcache.xml"/> 
</bean> 
<!-- 凭证匹配器 --> 
<bean id="credentialsMatcher" class=" 
com.github.zhangkaitao.shiro.chapter12.credentials.RetryLimitHashedCredentialsMatcher"> 
  <constructor-arg ref="cacheManager"/> 
  <property name="hashAlgorithmName" value="md5"/> 
  <property name="hashIterations" value="2"/> 
  <property name="storedCredentialsHexEncoded" value="true"/> 
</bean> 
<!-- Realm实现 --> 
<bean id="userRealm" class="com.github.zhangkaitao.shiro.chapter12.realm.UserRealm"> 
  <property name="userService" ref="userService"/> 
  <property name="credentialsMatcher" ref="credentialsMatcher"/> 
  <property name="cachingEnabled" value="true"/> 
  <property name="authenticationCachingEnabled" value="true"/> 
  <property name="authenticationCacheName" value="authenticationCache"/> 
  <property name="authorizationCachingEnabled" value="true"/> 
  <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 name="sessionManager" ref="sessionManager"/> 
</bean> 
<!-- 会话管理器 --> 
<bean id="sessionManager" class="org.apache.shiro.session.mgt.DefaultSessionManager"> 
  <property name="globalSessionTimeout" value="1800000"/> 
  <property name="deleteInvalidSessions" value="true"/> 
  <property name="sessionValidationSchedulerEnabled" value="true"/> 
  <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/> 
  <property name="sessionDAO" ref="sessionDAO"/> 
</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 name="cacheManager" ref="cacheManager"/> 
</bean> 
<!-- 相当于调用SecurityUtils.setSecurityManager(securityManager) --> 
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
<property name="staticMethod"  
value="org.apache.shiro.SecurityUtils.setSecurityManager"/> 
  <property name="arguments" ref="securityManager"/> 
</bean> 
<!-- Shiro生命周期处理器--> 
<bean id="lifecycleBeanPostProcessor"  
class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> 

分享名称:详解spring与shiro集成-创新互联
当前URL:https://www.cdcxhl.com/article42/csegec.html

成都网站建设公司_创新互联,为您提供动态网站做网站网站制作网站建设Google定制网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设