Spring整合Shiro

本文详细介绍了Spring Security框架中Shiro的配置过程,包括ShiroFilter的设置、安全管理器的配置、自定义Realm的实现、凭证匹配器的创建、缓存管理及Session管理等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
      
      
      <!--这里的名字要和前面的代理的名字一直否则就要出现问题的-->
      <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
         <!--登陆表单提交的action的地址-->
         <property name="loginUrl" value="/login.action"></property>
         <!--登陆成功的跳转页面  没用-->
         <property name="successUrl" value="/main.jsp"></property>
         <!--当用户没有这个权限的时候跳转的页面-->
         <property name="unauthorizedUrl" value="/refuse.jsp"></property>
         <!--注入安全管理器-->
         <property name="securityManager" ref="defaultWebSecurityManager"></property>
         
         <!--在这里要注入form表单的认证器-->
         <property name="filters">
            <map>
              <entry key="authc" value-ref="myFormAuthenticationFilter"></entry>
            </map>
         </property>
         
         <!--配置过滤器的地方-->
         <property name="filterChainDefinitions">
            <value>
               <!--静态资源需要释放-->
               /img/** anon
               /js/** anon
               /css/** anon
            
               <!--测试过滤器实现权限的问题-->
               <!--只要remeberMe之后 首页就可以不从新认证就可以访问了-->
               /main.jsp=user
               
               /update.action=perms[user:update]
               /update.action=roles[sysmanager]
               /logout.action=logout
              <!--  /**=anon   -->         <!--这个表示的是都不需要人真正-->
                /**=authc        <!--所有页面都不需要认证就能访问-->
            </value>
         </property> 
      </bean> 
      
      <!--配置的是安全管理器-->
      <bean id="defaultWebSecurityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
         <!--注入自定义的realm-->
         <property name="realm" ref="myRealm"></property>
         <!--注入缓存的管理-->
         <property name="cacheManager" ref="cacheManager"></property>
         <!--注入Session的管理器-->
         <!--<property name="sessionManager" ref="sessionManager"></property> -->
         <!--就像安全管理器中进行注入-->
         <property name="rememberMeManager" ref="rememberMeManager"></property>
      </bean>
      
      <!--自定义的realm-->
      <bean id="myRealm" class="com.qf.shiro.realm.MyRealm">
         <!--注入凭证的匹配器-->
         <property name="credentialsMatcher" ref="credentialsMatcher"></property>
      </bean>
      
      <!--配置密码的散列  创建一个凭证的匹配器-->
      <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
          <!--使用的是Md5进行散列-->
          <property name="hashAlgorithmName" value="md5"></property>
          <!--散列的次数-->
          <property name="hashIterations" value="1"></property>
          <!--添加的盐是什么-->
         <!--  <property name="hashSalted" value="abc" ></property> -->
      </bean>
      
      <!--下面就配置个缓存   配置的是shiro的缓存管理-->
      <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
         <!--这里面需要注入缓存的实现类-->
         <property name="cacheManager" ref="ehCacheManagerFactoryBean"></property>
      </bean>
      
      <!--配置缓存的具体实现类-->
      <bean id="ehCacheManagerFactoryBean" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
         <!--当前的缓存是否进行共享-->
         <property name="shared" value="true"></property>
         <!--引入的是缓存的配置文件-->
         <property name="configLocation" value="classpath:ehcache.xml"></property>
      </bean>
      
      
      <!--Session的管理-->
     <!-- <bean id="sessionManager" class="org.apache.shiro.session.mgt.DefaultSessionManager">
         配置下session的过期时间
         <property name="globalSessionTimeout" value="10000"></property>
      </bean>  -->
      

      
      <!--1>:申明form表单的认证器-->
      <bean id="myFormAuthenticationFilter" class="com.qf.shiro.filter.MyFormAuthenticationFilter">
         <!--这个叫做改名字-->
         <property name="usernameParam" value="userName"></property>
         <!--改密码的这个-->
         <property name="passwordParam" value="passWord"></property>
         <!--这个是用来改名字的-->
         <property name="rememberMeParam" value="rememberMe"></property>
      </bean>
      
            
      <!--rememberMe-->
      <!--配置Cookie的一个管理器-->
      <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
         <!--注入Cookie-->
         <property name="cookie" ref="cookie"></property>
      </bean>
      
      <bean id="cookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <!--这个就是Cookie的过期时间-->
        <property name="maxAge" value="2592000"></property>
        <!--这个表示的是当前访问的路径上的很路径只有是当前的项目名/的时候  这个Cookie才会被带到服务器去-->
        <property name="path" value="/Day1801_shiro_02"></property>
        <!--存在客户端的这个名字 就叫做rememenberMe-->
        <property name="name" value="rememberMe"></property>
      </bean>
</beans> 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值