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="MD5"/>
            <property name="hashIterations" value="3"/>
            <!-- 设置成false,表示用base64加密,默认是true,加密方式为hex-->
            <property name="storedCredentialsHexEncoded" value="false"/>
        </bean>
    
        <!-- 第三步 -->
        <!-- 配置realm-->
        <bean id="realm" class="com.zsm.shiro.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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值