You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bsdgy-server/src/main/java/com/kiisoo/ic/config/AuthorizationRealm.java

81 lines
2.7 KiB
Java

package com.kiisoo.ic.config;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.ic.common.utils.MD5FileUtil;
import com.kiisoo.ic.constants.Constants;
import com.kiisoo.ic.system.entity.PrivilageAccountDO;
import com.kiisoo.ic.system.mapper.PrivilageAccountDOMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import javax.annotation.Resource;
/**
* Shiro
* @author Arvin
*
*/
public class AuthorizationRealm extends AuthorizingRealm {
/**
*
*/
@Resource
private PrivilageAccountDOMapper privilageAccountDOMapper;
/**
*
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return new SimpleAuthorizationInfo();
}
/**
*
* @param authcToken token
* @return authenticationInfo
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
// 获取用户的输入的账号.
String account = (String)token.getPrincipal();
// encrypt
token.setPassword(MD5FileUtil.getMD5String(new String(token.getPassword())).toCharArray());
// 通过username从数据库中查找 User对象如果找到没找到.
// 实际项目中这里可以根据实际情况做缓存如果不做Shiro自己也是有时间间隔机制2分钟内不会重复执行该方法
QueryWrapper<PrivilageAccountDO> wrapper = new QueryWrapper<>();
wrapper.eq("login",account).last("limit 1");
PrivilageAccountDO userInfo = privilageAccountDOMapper.selectOne(wrapper);
if(userInfo == null){
throw new UnknownAccountException("用户不存在");
}
if (Constants.ACCOUNT_STATUS_UNABLE != userInfo.getStatus()) {
throw new LockedAccountException("无效账号");
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userInfo, userInfo.getPassword(), getName());
return info;
}
/**
*
* @param principal
*/
public void clearCachedAuthorizationInfo(String principal) {
SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
clearCachedAuthorizationInfo(principals);
}
}