Merge remote-tracking branch 'origin/在线访问lhj' into 在线访问lhj

在线访问lhj
李宏杰 8 months ago
commit 01f855c5ef

@ -1,6 +1,7 @@
package com.tamguo.config.shiro;
import java.util.Set;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
@ -12,61 +13,87 @@ import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import com.tamguo.modules.member.model.MemberEntity;
import com.tamguo.modules.member.service.IMemberService;
/**
*
* Realm
*
*/
public class MemberRealm extends AuthorizingRealm {
// 依赖注入 MemberService 实例
@Autowired
private IMemberService iMemberService;
/**
* ()
*
*
* @param principals
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// 权限集合
Set<String> permsSet = null;
// 创建简单授权信息对象
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
// 设置权限集合
info.setStringPermissions(permsSet);
return info;
}
/**
* ()
*
*
* @param token
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// 获取用户名
String username = (String) token.getPrincipal();
// 获取密码
String password = new String((char[]) token.getCredentials());
// 根据用户名查询 MemberEntity 对象
MemberEntity member = iMemberService.findByUsername(username);
// 如果用户不存在
if (member == null) {
throw new UnknownAccountException("用户名或密码有误,请重新输入或找回密码");
}
// 获取登录失败次数
Integer loginFailureCount = iMemberService.getLoginFailureCount(member);
// 如果登录失败次数大于 10
if (loginFailureCount > 10) {
throw new LockedAccountException("账号被锁定");
}
// 如果密码不匹配
if (!new Sha256Hash(password).toHex().equals(member.getPassword())) {
loginFailureCount++;
iMemberService.updateLoginFailureCount(member, loginFailureCount);
throw new IncorrectCredentialsException("用户名或密码有误,请重新输入或找回密码");
}
// 更新登录时间
iMemberService.updateLastLoginTime(member.getId());
// 创建简单认证信息对象
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(member, password, getName());
return info;
}
}
Loading…
Cancel
Save