zoukankan      html  css  js  c++  java
  • MyRealm V2.0(注:加上了权限字符串)

    
    import com.aaa.entity.User;
    import com.aaa.service.MenuService;
    import com.aaa.service.UserService;
    import com.baomidou.mybatisplus.mapper.EntityWrapper;
    import lombok.extern.slf4j.Slf4j;
    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.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.Set;
    
    /**
     * Created by cws
     *
     * @author Administrator
     */
    @Slf4j
    @Component
    public class MyRealm extends AuthorizingRealm {
    
        private static final int ZERO = 0;
    
        @Autowired
        private MenuService menuService;
        @Autowired
        private UserService userService;
    
        /**
         * @Author : cws
         * @Description : 授权(验证权限时调用)
         */
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
            log.info("授权开始");
            User user = (User) principalCollection.getPrimaryPrincipal();
            //获取用户id 将权限字符串添加到授权对象中
            String userId = user.getId();
            //用户权限列表
            Set<String> permsSet = menuService.getPermissions(userId);
            //授权对象
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            info.setStringPermissions(permsSet);
            return info;
        }
    
        /**
         * @Author : cws
         * @Description : 认证(登录时调用)
         */
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
            log.info("认证开始");
            //获取所有身份验证成功的Realm名字
            String username = (String) authenticationToken.getPrincipal();
            String password = new String((char[]) authenticationToken.getCredentials());
            //查询用户信息userService.findByUserName(username)
            EntityWrapper<User> wrapper = new EntityWrapper<>();
            User user = userService.selectOne(wrapper.eq("username",username));
            //账号不存在4
            if (user == null) {
                throw new UnknownAccountException("用户名不正确");
            }
            //密码错误
            if (!password.equals(user.getPassword())) {
                throw new IncorrectCredentialsException("密码不正确");
            }
            //账号禁用
            if ("0".equals(user.getStatus())) {
                throw new LockedAccountException("用户待审核中,请联系管理员");
            }
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
            return info;
        }
    }
  • 相关阅读:
    Python --链接MYSQL数据库与简单操作 含SSH链接
    Jmeter 后置处理器--jp@gc
    Jmeter 接口测试 响应结果中文是Unicode转为中文
    Jmeter JDBC请求---把数据库结果参数化传递到其他请求
    MySQL根据某字段部分内容分组计数
    Linux 服务器命令,持续更新……
    APP网络测试要点和弱网模拟
    Jmeter JDBC Request 查询语句中有汉字查询结果为空的解决方法
    Jmeter连接Redis,获取Redis数据集
    Jmeter获取接口返回数组的长度
  • 原文地址:https://www.cnblogs.com/cwshuo/p/13885628.html
Copyright © 2011-2022 走看看