springMVC结合Shiro实现登录失败次数过多锁定账户功能

5 篇文章 0 订阅
  • 本文主要记录了自己遇到了一些坑,以及需要注意的细节
    主要部分源码点我
    提取码:jwvx

  • 前言
    添加依赖等等其他人的教程里都有,就不浪费时间解释了;

  • application-shiro.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>

<description>Shiro安全配置</description>

<!--安全管理器-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <!--设置自定义Realm-->
    <property name="realm" ref="shiroDbRealm"/>
    <property name="sessionManager" ref="sessionManager"/>
    <!--将缓存管理器,交给安全管理器-->
    <property name="cacheManager" ref="shiroEhcacheManager"/>
</bean>

<!--session管理器-->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
    <property name="globalSessionTimeout" value="1800000"/>
    <property name="deleteInvalidSessions" value="true"/>
    <property name="sessionDAO" ref="sessionDAO"/>
    <property name="sessionIdCookieEnabled" value="true"/>
    <property name="sessionIdCookie" ref="sessionIdCookie"/>
    <property name="sessionValidationSchedulerEnabled" value="true"/>
    <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
    <property name="cacheManager" ref="shiroEhcacheManager"/>
</bean>

<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <!-- 安全管理器 -->
    <property name="securityManager" ref="securityManager"/>
    <!-- 默认的登陆访问url -->
    <property name="loginUrl" value="/login"/>
    <!-- 登陆成功后跳转的url -->
    <property name="successUrl" value="/index"/>
    <!-- 没有权限跳转的url -->
    <property name="unauthorizedUrl" value="/unauth"/>
    <property name="filterChainDefinitions">
        <value>
            /commons/** = anon <!-- 不需要认证 -->
            /static/** = anon
            /fast/aliPay/** = anon
            /login = anon
            /dictionary/listValidItems = anon
            /FileDown/download = anon
            /FileDown/downloadPdf = anon
            /tokenLogin = anon
            /bulletinsShow/** = anon
            /aliPay/** = anon
            /api/**/** = anon
            /ebid/ticketLogin/** = anon
            /swagger-ui.html = anon
            /webjars/** = anon
            /fast/project/projectList = anon
            /fast/project/result = anon
            /configuration/listItems = anon
            /configuration/findItemByCode = anon
            /acquireLoginId = anon
            /open/** = anon
            /** = authc <!-- 需要认证 -->
        </value>
    </property>
</bean>

<!--登录失败次数限制start-->
<!-- 用户授权信息Cache, 采用EhCache -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
    <property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
</bean>
<!--凭证匹配器   ehcache记录登录失败次数-->
<!--数据库中存储到的md5的散列值,在realm中需要设置数据库中的散列值它使用散列算法 及散列次数,让shiro进行散列对比时和原始数据库中的散列值使用的算法 一致。-->
<!--storedCredentialsHexEncoded表示是否存储散列后的密码为16 进制,默认是base64 -->
<bean id="credentialsMatcher" class="com.sgo.core.shiro.RetryLimitCredentialsMatcher">
    <constructor-arg ref="shiroEhcacheManager"/>
    <property name="hashAlgorithmName" value="md5"/>
    <!--这里可能会有人出现问题,原因是加解密规则不一致,看个人环境配置了-->
    <property name="hashIterations" value="1"/>
    <!--<property name="storedCredentialsHexEncoded" value="true"/>-->
</bean>
<bean id="shiroDbRealm" class="com.sgo.core.shiro.ShiroDbRealm">
    <property name="credentialsMatcher" ref="credentialsMatcher"/>
</bean>
<!--登录失败次数限制end-->

<!-- 在方法中 注入  securityManager ,进行代理控制 -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
    <property name="arguments" ref="securityManager"/>
</bean>

<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- AOP式方法级权限检查  -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
      depends-on="lifecycleBeanPostProcessor"/>

<!-- 启用shrio授权注解拦截方式 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean>
  • ehcache-shiro.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="shiroOracleCache">

    <diskStore path="java.io.tmpdir"/>

    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
    />
    <!-- 登录记录缓存 锁定1分钟 -->
    <!--statistics开启统计信息-->
    <cache name="passwordRetryCache"
           maxEntriesLocalHeap="20000"
           eternal="false"
           timeToIdleSeconds="60"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true">
    </cache>
</ehcache>
  • 主要代码就这些,详见源码
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值