Pre Merge pull request !41 from zhaocc/master

This commit is contained in:
Gitee 2019-06-02 21:25:58 +08:00
commit c5d963ae38
1 changed files with 20 additions and 30 deletions

View File

@ -1,13 +1,5 @@
package com.ruoyi.framework.shiro.service; package com.ruoyi.framework.shiro.service;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.user.UserPasswordNotMatchException; import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
import com.ruoyi.common.exception.user.UserPasswordRetryLimitExceedException; import com.ruoyi.common.exception.user.UserPasswordRetryLimitExceedException;
@ -15,15 +7,23 @@ import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.framework.manager.AsyncManager; import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory; import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.domain.SysUser; import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* 登录密码方法 * 登录密码方法
* *
* @author ruoyi * @author ruoyi
*/ */
@Component @Component
public class SysPasswordService public class SysPasswordService {
{
@Autowired @Autowired
private CacheManager cacheManager; private CacheManager cacheManager;
@ -33,52 +33,42 @@ public class SysPasswordService
private String maxRetryCount; private String maxRetryCount;
@PostConstruct @PostConstruct
public void init() public void init() {
{
loginRecordCache = cacheManager.getCache("loginRecordCache"); loginRecordCache = cacheManager.getCache("loginRecordCache");
} }
public void validate(SysUser user, String password) public void validate(SysUser user, String password) {
{
String loginName = user.getLoginName(); String loginName = user.getLoginName();
AtomicInteger retryCount = loginRecordCache.get(loginName); AtomicInteger retryCount = loginRecordCache.get(loginName);
if (retryCount == null) if (retryCount == null) {
{
retryCount = new AtomicInteger(0); retryCount = new AtomicInteger(0);
loginRecordCache.put(loginName, retryCount); loginRecordCache.put(loginName, retryCount);
} }
if (retryCount.incrementAndGet() > Integer.valueOf(maxRetryCount).intValue()) if (retryCount.incrementAndGet() > Integer.valueOf(maxRetryCount).intValue()) {
{
AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", maxRetryCount))); AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", maxRetryCount)));
throw new UserPasswordRetryLimitExceedException(Integer.valueOf(maxRetryCount).intValue()); throw new UserPasswordRetryLimitExceedException(Integer.valueOf(maxRetryCount).intValue());
} }
if (!matches(user, password)) if (!matches(user, password)) {
{
AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.count", retryCount))); AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.count", retryCount)));
loginRecordCache.put(loginName, retryCount); loginRecordCache.put(loginName, retryCount);
throw new UserPasswordNotMatchException(); throw new UserPasswordNotMatchException();
} } else {
else
{
clearLoginRecordCache(loginName); clearLoginRecordCache(loginName);
} }
} }
public boolean matches(SysUser user, String newPassword) public boolean matches(SysUser user, String newPassword) {
{
return user.getPassword().equals(encryptPassword(user.getLoginName(), newPassword, user.getSalt())); return user.getPassword().equals(encryptPassword(user.getLoginName(), newPassword, user.getSalt()));
} }
public void clearLoginRecordCache(String username) public void clearLoginRecordCache(String username) {
{
loginRecordCache.remove(username); loginRecordCache.remove(username);
} }
public String encryptPassword(String username, String password, String salt) public String encryptPassword(String username, String password, String salt) {
{
return new Md5Hash(username + password + salt).toHex().toString(); return new Md5Hash(username + password + salt).toHex().toString();
} }