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;
        }
    }
  • 相关阅读:
    为什么Redis比Memcached易
    请注意CSDN社区微通道,许多其他的精彩等着你
    采用ACE登录设施(一)HelloWorld
    AIX 7.1 install python
    spring mvc入门
    iOS开展——全球应对MotionEvent
    2015第35周日
    2015第35周六转相见恨晚的知识列表
    2015第35周五JavaScript变量
    2015第35周四
  • 原文地址:https://www.cnblogs.com/cwshuo/p/13885628.html
Copyright © 2011-2022 走看看