ssm整合shiro

4 篇文章 0 订阅
2 篇文章 0 订阅
  1. web.xml 文件配置filter
<filter>
  	<filter-name>shiroFilter</filter-name>
  	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  	<init-param>
  		<param-name>targetFilterLifecycle</param-name>
  		<param-value>true</param-value>
  	</init-param>
  	<init-param>
  		<param-name>targetBeanName</param-name>
  		<param-value>shiroFilter</param-value>
  	</init-param>
  </filter>
  
  <filter-mapping>
  	<filter-name>shiroFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  1. applicationContext.xml配置shiro Bean
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        		<property name="realm" ref="userRealm"></property>
        </bean>
        
       <bean id="userRealm" class="'">
        	<property name="credentialsMatcher " ref="credentialsMatcher"></property>
        </bean>
        <!-- 配置凭证匹配器 -->
        <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
        	<property name="hashAlgorithmName" value="md5"></property>
        	<property name="hashIterations" value="2"></property>
        </bean>
        
        <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        	<property name="securityManager"  ref="securityManager"></property>
        	<property name="loginUrl" value="/login"></property>
        		<!-- 配置认证成功后跳转地址,如果没有配置则跳转上一个路径 -->
        		<property name="successUrl" value="/index"></property>
        		<!-- 权限认证不通过访问路径 -->
        		<property name="unauthorizedUrl " value="/logingUrl"></property>
        		<!-- shiro过滤器链 -->
        		<property name="filterChainDefinitions">
        				<value>
        					/index=anon
        					/login=authc
        					/logout=logout
        					/images/**=anon
        					/js/**=anon
        					/css/**=anon
        					/**=authc
        				</value>
        		</property>
        		
        </bean>
            <!-- 配置logout过滤器 -->
        <bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
        		<property name="redirectUrl " value="/login"></property>
        </bean>
  1. 创建实体类Realm -> UserRealm
public class UserRealm extends AuthorizingRealm{
	...
}
  1. 配置权限三种方式
    注解方式
    标签
    过滤器

缓存管理

可以使用ehcache 或者redis集成

ehcache

引用ehcache.jar文件

ehcacee.xml


<ehcache>

    <diskStore path="java.io.tmpdir/shiro-ehcache"/>


    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            />

    

</ehcache>



配置applicationContext.xml

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        		<property name="realm" ref="userRealm"></property>
        		<property name="cacheManager" ref="cacheManager"/>
        </bean>
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"  >
        	<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
        </bean>

SessionManager

  <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        	<property name="globalSessionTimeout" value="1800000"/>
        	<property name="deleteInvalidSessions" value="true"/>
        </bean>

RemeberMe

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值