spring-shiro.xml授权验证的配置

spring与shiro整合时,需要将shiro的ini配置放到xml文件中,此文是授权验证模块配置的属性,如有问题,多谢指点。

首先单独创建一个spring-shiro.xml文件,这样shiro的配置全部放在这个文件里面,为了查看方便清晰,当然,所有文件都放在一个spring配置文件里也可以,不过感觉这样太乱了,对于各个技术和spring的整合,感觉单独创建一个xml文件比较好。不啰嗦了,下面直接列出配置步骤。

需要配置的文件:web.xml、spring-shiro.xml

  1. 先在web.xml中配置shiro的过滤器进行拦截请求。
    <!-- 配置shiro过滤器,用来拦截所有请求,进行认证和授权 -->
      <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <!-- 将当前的Filter的生命周期将由web容器管理 -->
        <init-param>
          <param-name>targetFilterLifecycle</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
    
      <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
  2. 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">
    
        <!-- 第四步 -->
        <!-- 配置密码匹配器-->
        <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
            <property name="hashAlgorithmName" value="${algorithmName}"/>
            <property name="hashIterations" value="${hashIterations}"/>
            <!-- 设置成false,表示用base64加密,默认是true,加密方式为hex-->
            <property name="storedCredentialsHexEncoded" value="false"/>
        </bean>
    
        <!-- 第三步 -->
        <!-- 配置realm-->
        <bean id="realm" class="shiro04.JdbcSaltRealm">
            <!-- 注入DataSource -->
            <property name="dataSource" ref="dataSource"/>
            <!-- 注入密码匹配器 -->
            <property name="credentialsMatcher" ref="credentialsMatcher"/>
            <!-- 重写带salt认证的sql-->
            <property name="authenticationQuery">
                <value>
                    select password,login_name from t_user where login_name=?
                </value>
            </property>
        </bean>
    
        <!-- 第二步 -->
        <!-- 配置-securityManager -->
        <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
            <property name="realms">
                <list>
                    <ref bean="realm"/>
                </list>
            </property>
        </bean>
    
        <!-- 第一步 -->
        <!-- 定义一个名为shiroFilter的bean,用来配置url过滤规则-->
        <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
            <property name="securityManager" ref="securityManager"/>
        </bean>
    
    </beans>

    建议看看大神的shiro介绍:https://jinnianshilongnian.iteye.com/blog/2018936

猜你喜欢

转载自blog.csdn.net/scorpio_meng/article/details/85157240