springboot+shiro实现登录系数限定,thymeleaf中使用shiro标签

1.实现登录次数限定

CredentialsMatcher是shiro提供的用于加密密码和验证密码服务的接口,而HashedCredentialsMatcher正是CredentialsMatcher的一个实现类。

新写一个类继承HashedCredentialsMatcher,在重写doCredentialsMatch,判断如果验证密码次数超过5次,抛出异常。

public class RetryLimitHashedCredentialsMatcher extends HashedCredentialsMatcher {
    private Cache<String, AtomicInteger> passwordRetryCache;

    public RetryLimitHashedCredentialsMatcher(CacheManager cacheManager) {
        passwordRetryCache = cacheManager.getCache("passwordRetryCache");
    }

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        String username = (String) token.getPrincipal();
        AtomicInteger retryCount = passwordRetryCache.get(username);
        // 查看缓存中的尝试次数
        if (retryCount == null) {
            retryCount = new AtomicInteger(0);
            passwordRetryCache.put(username, retryCount);
        }
        // 超过五次,抛异常
        if (retryCount.incrementAndGet() > 5) {
            throw new ExcessiveAttemptsException();
        }
        // 判断登录
        boolean matches = super.doCredentialsMatch(token, info);
        if (matches) {
            passwordRetryCache.remove(username);
        }
        return matches;
    }
}

在shiroconfig中,原本HashedCredentialsMatcher的实现类是HashedCredentialsMatcher,现在换成我们新写的RetryLimitHashedCredentialsMatcher

@Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new RetryLimitHashedCredentialsMatcher(ehCacheManager());

        hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法;
        hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5(""));

        return hashedCredentialsMatcher;
    }

在登录中加上对这个异常的处理

else if (ExcessiveAttemptsException.class.getName().equals(exception)) {
                System.out.println("ExcessiveAttemptsException -- > 登录失败次数过多:");
                msg = "ExcessiveAttemptsException -- > 登录失败次数过多:";
            }

连续登录五次之后的效果:

165004_c3aT_2942412.png

2.thymeleaf中使用shiro标签

导入包

		<dependency>
			<groupId>com.github.theborakompanioni</groupId>
			<artifactId>thymeleaf-extras-shiro</artifactId>
			<version>2.0.0</version>
		</dependency>

https://github.com/theborakompanioni/thymeleaf-extras-shiro

这里有个问题:

我为了开启thymeleaf的弱语法检测,用的不是springboot默认的thymeleaf版本,而是指定了thymefeaf版本

		<!--提高thymeleaf的版本,以支持thymeleaf的若语法检测-->
		<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>

所以thymeleaf-extras-shiro的版本也要相应的升级。

165334_2I9W_2942412.png

thymeleaf是低版本的话,可以用1.0.2之类的hymeleaf-extras-shiro

 

转载于:https://my.oschina.net/zk875/blog/830079

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值