springboot + shiro 实现限制用户登录次数

背景:

       shiro 可以实现限制用户登录尝试次数的功能,要限制用户登录尝试次数,必然要对用户名密码验证失败做记录,当登录失败次数达到限制,修改数据库中的状态字段,并返回前台错误信息。

创建比较器:

       创建比较器类 RetryLimitHashedCredentialsMatcher 它继承了 HashedCredentialsMatcher,在这里面实现判断登录失败次数的记录,代码如下所示:

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.dao.UserMapper;
import com.entity.User;

/**
 * 
 * 登陆次数限制
 */
public class RetryLimitHashedCredentialsMatcher extends HashedCredentialsMatcher{

	private static final Logger logger = Logger.getLogger(RetryLimitHashedCredentialsMatcher.class);
	
    @Autowired
    private UserMapper userMapper;
    
    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) {
            // 如果用户没有登陆过,登陆次数加1 并放入缓存
            retryCount = new AtomicInteger(0);
            passwordRetryCache.put(username, retryCount);
        }
        if (retryCount.incrementAndGet() > 3) {
            // 如果用户登陆失败次数大于3次 抛出锁定用户异常  并修改数据库字段
            User user = userMapper.selectByUserName(username);
            if (user != null && "0".equals(user.getStatus())){
                // 数据库字段 默认为 0  就是正常状态 所以 要改为1
                // 修改数据库的状态字段为锁定
                user.setStatus("1");
                userMapper.updateByPrimaryKey(user);
            }
            logger.info("锁定用户" + user.getUserName());
            // 抛出用户锁定异常
            throw new LockedAccountException();
        }
        // 判断用户账号和密码是否正确
        boolean matches = super.doCredentialsMatch(token, info);
        if (matches) {
            // 如果正确,从缓存中将用户登录计数 清除
            passwordRetryCache.remove(username);
        }
        return matches;
    }

    /**
     * 根据用户名 解锁用户
     * @param username
     * @return
     */
    public void unlockAccount(String username){
        User user = userMapper.selectByUserName(username);
        if (user != null){
            // 修改数据库的状态字段为锁定
            user.setStatus("0");
            userMapper.updateByPrimaryKey(user);
            passwordRetryCache.remove(username);
        }
    }
}

修改shiroConfig:

       我们在之前的盐值加密的文章中配置过 HashedCredentialsMatcher bean ,我们在这里需要修改一些,改成下面的这个样子:

    @Bean
	public HashedCredentialsMatcher hashedCredentialsMatcher() {
        // 主要是这块创建的对象是我们上面的继承类
		RetryLimitHashedCredentialsMatcher retryLimitHashedCredentialsMatcher = new RetryLimitHashedCredentialsMatcher(ehCacheManager());
		// 散列算法:这里使用MD5算法;
		retryLimitHashedCredentialsMatcher.setHashAlgorithmName("md5");
		// 散列的次数,比如散列两次,相当于 md5(md5(""));
		retryLimitHashedCredentialsMatcher.setHashIterations(2);
		// storedCredentialsHexEncoded默认是true,此时用的是密码加密用的是Hex编码;false时用Base64编码
		retryLimitHashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
		return retryLimitHashedCredentialsMatcher;
	}

       如果你是之前加密存储密码的配置,就不需要配置下面的代码,看一下你的代码是不是使用加密算法类来验证密文。

    // 将自己的验证方式加入容器
	@Bean
	public CustomRealm myShiroRealm() {
		CustomRealm customRealm = new CustomRealm();
		// 告诉realm,使用credentialsMatcher加密算法类来验证密文
		customRealm.setCredentialsMatcher(hashedCredentialsMatcher());
		/* 开启支持缓存,需要配置如下几个参数 */
		customRealm.setCachingEnabled(true);
	    // 启用身份验证缓存,即缓存AuthenticationInfo信息,默认false
		customRealm.setAuthenticationCachingEnabled(true);
	    // 缓存AuthenticationInfo信息的缓存名称 在 ehcache-shiro.xml 中有对应缓存的配置
		customRealm.setAuthenticationCacheName("authenticationCache");
	    // 启用授权缓存,即缓存AuthorizationInfo信息,默认false
		customRealm.setAuthorizationCachingEnabled(true);
	    // 缓存AuthorizationInfo 信息的缓存名称  在 ehcache-shiro.xml 中有对应缓存的配置
		customRealm.setAuthorizationCacheName("authorizationCache");
		return customRealm;
	}

配置缓存:

       在 ehcache-shiro.xml 添加缓存项 passwordRetryCache ,当项目关闭的时候,当前用户存储的次数也会被清空。

    <!-- 登录失败次数缓存
    	注意 timeToLiveSeconds 设置为300秒 也就是5分钟
     	可以根据自己的需求更改
 	-->
	<cache name="passwordRetryCache"
	       maxEntriesLocalHeap="2000"
	       eternal="false"
	       timeToIdleSeconds="0"
	       timeToLiveSeconds="300"
	       overflowToDisk="false"
	       statistics="true">
	</cache>

修改 LoginController:

       在 LoginController 中添加解除用户限制的方法,整个文件的代码如下所示:

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.entity.ResultData;
import com.entity.User;
import com.service.UserService;
import com.shiro.CustomRealm;
import com.shiro.RetryLimitHashedCredentialsMatcher;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class LoginController {

	@Autowired
	UserService userService;
	@Autowired
	RetryLimitHashedCredentialsMatcher retryLimitHashedCredentialsMatcher;

	@GetMapping("/login")
	public String login() {
		return "login";
	}

	@PostMapping("/login")
	@ResponseBody
	public ResultData login(String userName,String password,String rememberMe) {
		ResultData resultData = new ResultData();
		if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
			resultData.setCode(200);
			resultData.setMessage("用户名和密码不能为空!");
			return resultData;
		}
		boolean rememberBoolean = false;
		if("on".equals(rememberMe)) {
			rememberBoolean = true;
		}
		//用户认证信息
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
				userName,password,rememberBoolean);
		try {
			// 用户账号和密码进行验证
			subject.login(usernamePasswordToken);
			resultData.setCode(100);
			return resultData;
		} catch (Exception e) {
			if(e instanceof UnknownAccountException) {
				log.error("用户名不存在!", e);
				resultData.setCode(200);
				resultData.setMessage("用户名不存在!");
			}
			if(e instanceof IncorrectCredentialsException) {
				log.error("用户名或者密码错误!", e);
				resultData.setCode(200);
				resultData.setMessage("用户名或者密码错误!");
			}
			if(e instanceof LockedAccountException) {
				log.error("账号已被锁定,请联系管理员!", e);
				resultData.setCode(200);
				resultData.setMessage("账号已被锁定,请联系管理员!");
			}
			return resultData;
		}
	}

	@GetMapping("/page_skip")
	public String page_skip() {
		return "page_skip";
	}

	@RequestMapping("/shiro_index")
	public ModelAndView shiro_index() {
		ModelAndView view = new ModelAndView();

		view.setViewName("shiro_index");
		return view;
	}

	/**
	 * 解除admin 用户的限制登录 
	 * 写死的 方便测试
	 * @return
	 */
	@RequestMapping("/unlockAccount")
	@ResponseBody
	public ResultData unlockAccount(String userName){
		ResultData resultData = new ResultData();
		try {
			retryLimitHashedCredentialsMatcher.unlockAccount(userName);
			resultData.setCode(100);
			resultData.setMessage("解锁成功!");
		}catch(Exception e) {
			e.printStackTrace();
			resultData.setCode(200);
			resultData.setMessage("解锁失败!");
		}
		return resultData;
	}
}

       为了方便测试,记得将 unlockAccount 权限改为任何人可访问,需要修改 ShiroConfig 类的 ShiroFilterFactoryBean 的方法,代码如下所示:

    // Filter工厂,设置对应的过滤条件和跳转条件
	@Bean
	public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
		// .....省略	
		Map<String, String> map = new LinkedHashMap<>();
	    // 添加这块的过滤,否则请求进不来
		map.put("/unlockAccount", "anon");	
        // .....省略
		shiroFilter.setFilterChainDefinitionMap(map);
		return shiroFilter;
	}

修改登录界面:

       整个 login.jsp 的登录界面的内容如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>一路发咨询网站</title>
</head>
<body>
<script type="text/javascript" src="/static/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/static/js/login.js"></script>
<link rel="stylesheet" type="text/css" href="/static/css/login.css"/>
<h1>欢迎登录一路发咨询网站</h1>

<div>
	<div id="father" style="background-image: url('/static/image/index.png');">
		<div style="width:300px;height:100px;">
			<div style="width:150px;float:left">
				<span>用户名:<span></span>
			</div>
			<div style="width:150px;float:left;">
				<input  style="height:34px" type="text" id="userName" name="userName"/>
			</div>
		</div>
		<div style="width:300px;height:100px;">
			<div style="width:150px;float:left;">
				<span>密码:<span></span>
			</div>
			<div style="width:150px;float:left;">
				<input  style="height:34px" type="password" id="password"  name="password"/>
			</div>
		</div>
		<div style="width:300px;height:100px;">
			<div style="width:64px;float:left;margin-left:280px">
				<input style="height:34px;width:34px;" type="checkbox" id="rememberMe"  name="rememberMe" />
			</div>
			 <div style="width:150px;float:left;margin-top:-4px">
					<span>记住我<span></span>
			</div>
		</div>
		 <div style="margin-left:190px">
				<input type="button" style="height:50px;width:90px;margin-top:5px;font-size:34px;font-weight:bold" onclick="login()" value="提交"/>
				<input type="button" style="height:50px;margin-left:30px;width:150px;font-size:24px;font-weight:bold" onclick="unlock()" value="解锁用户"/>
		</div>
	</div>
</div>
</body>
</html>

       所涉及到的 login.js 的内容如下所示:

function login(){
	var userName = $("#userName").val();
	var password = $("#password").val();
	var rememberMe = $("#rememberMe").val();
	if (userName == '') {
		alert('用户名不能为空!');
		return;
	}
	if (password == '') {
		alert('密码不能为空!');
		return;
	}
	$.ajax({
		url: 'login',
		type: "POST",
		async: false,
		data: {"userName":userName, "password":password, "rememberMe":rememberMe},
		success: function (data) {
			if (data.code == 100) {
				window.location = 'shiro_index';
			}else {
				alert(data.message);
			}
		},
		error:function(){
			alert(data.message);
		}
	});
}
function unlock(){
	var userName = $("#userName").val();
	if (userName == '') {
		alert('用户名不能为空!');
		return;
	}
	$.ajax({
		url: '/unlockAccount',
		type: "POST",
		async: false,
		data: {"userName":userName},
		success: function (data) {
			if (data.code == 100) {
				alert(data.message);
			}else {
				alert(data.message);
			}
		},
		error:function(){
			alert(data.message);
		}
	});
}

测试:

       启动工程,在登录界面故意输错密码 3 次,就会出现下面的页面:

       点击解除用户,就会出现下面的页面:

       再输入正确的密码即可正常登录:

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Spring Boot是一个用于快速开发Java应用程序的开源框架,Shiro是一个强大且易于使用的Java安全框架,Redis是一个开源的内存数据库。结合使用这些技术可以实现单点登录功能。 在Spring Boot中使用Shiro来处理认证和授权,可以通过配置Shiro的Realm来实现用户的登录认证和权限控制。将用户的信息存储在Redis中,利用Redis的持久化特性来实现用户登录状态的共享和存储。 首先,在Spring Boot项目的配置文件中配置Redis的连接信息,以便连接到Redis数据库。 然后,创建一个自定义的Shiro的Realm,在其中重写认证和授权的方法。在认证方法中,将用户的登录信息存储到Redis中,以便其他服务可以进行验证。在授权方法中,根据用户的角色和权限进行相应的授权操作。 接着,在Spring Boot项目的配置类中配置Shiro的相关设置,包括Realm、Session管理器、Cookie管理器等。 最后,可以在Controller层中使用Shiro的注解来标记需要进行认证和授权的接口,以确保只有登录后且具备相应权限的用户才能访问这些接口。 总的来说,通过使用Spring Boot、Shiro和Redis的组合,可以实现单点登录的功能。用户在登录后,将登录信息存储到Redis中,其他服务可以通过验证Redis中的数据来判断用户的登录状态。同时,Shiro提供了强大的认证和授权功能,可以确保只有具备相应权限的用户才能访问受保护的接口。这些功能的具体实现可以通过深入研究Spring Boot、Shiro和Redis的源码来了解。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的小三菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值