背景:
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 次,就会出现下面的页面:
点击解除用户,就会出现下面的页面:
再输入正确的密码即可正常登录: