shiro整合SpringMVC基于xml

在web.xml中的额外配置

<!-- 配置Shiro过滤器,拦截所有的请求 -->
  <!-- 使用于SpringMVC整合的过滤器 ,使用Spring代理过滤器实现-->
  <!-- 问题:为什么需要使用SpringWEB提供的代理过滤器拦截Shiro处理的请求呢?
    答:DelegatingFilterProxy的作用就是可以让过滤器器在Spring配置文件里面配置。其实就是将Filter过滤器对象交个Spring容器代理
    这样做的目的就是让Shiro整合SpringMVC,让Spring容器里面的对象可以访问Shiro的过滤器对象!!
   -->
  <filter>
    <filter-name>shiroFilterBean</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <!-- 指定代理的过滤器对应的容器的配置的过滤器的对象名,如果不指定就以 <filter-name>作为匹配的对象名-->
    <init-param>
      <param-name>targetBeanName</param-name>
      <param-value>shiroFilterBean</param-value>
    </init-param>
    <init-param>
      <!-- 让Spring容器管理Filter的生命周期 -->
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
     <filter-name>shiroFilterBean</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>

在spring和shiro整合的配置文件中的配置spring-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 第一步:指定Shiro的拦截过滤器 -->
    <bean name="shiroFilterBean" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
         <!-- 指定securityManager容器对象 -->
         <property name="securityManager" ref="securityManager"></property>
         <!-- 设置拦截器链 
                 说明:Shiro提供了很多拦截器,用于不同场景的路径拦截,我们就在拦截器链中设置拦截请求的场景
                 anon :指定不拦截的路径,如登录页面请求
                 /user/toLogin = anon
                 authc : 必须需要校验的路径
                 logout :注销拦截器。如果路径类型为logout就是一个注销路径
          
         -->

         <property name="filterChainDefinitions">
              <value>
                   /user/toLogin = anon
                   /** = authc
                   /logout = logout
              </value>
         </property>
         <!-- 配置自定义拦截器 -->
<!--          <property name="filters"></property> -->
         <!-- 指定登录的请求路径 -->
         <property name="loginUrl" value="/user/login" />
         <!-- 指定登录成功后跳转的路径 -->
         <property name="successUrl" value="/index" />
         <!-- 指定没有通过校验,跳转的路径,一般不指定,不指定默认跳转到上一次请求的路径 -->
<!--          <property name="unauthorizedUrl"></property> -->
    </bean>
    
    <!-- 第二步:创建securityManager对象 -->
    <bean name="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realms" ref="shiroRealm"></property>
    </bean>
    
    <!-- 第三步:创建自定义realm对象 (配置加密参数)-->
    <bean name="shiroRealm" class="cn.gzsxt.realm.ShiroRealm">
        <property name="credentialsMatcher">
           <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
              <property name="hashAlgorithmName" value="md5"></property>
              <property name="hashIterations" value="1"></property>
           </bean>
        </property>
    </bean>

</beans>
原文地址:https://www.cnblogs.com/lch-Hao/p/10843312.html