zoukankan      html  css  js  c++  java
  • Shiro 学习笔记(Realm)

    Shiro Realm 域

    1.Realm

      域,Shiro 从从 Realm 获取安全数据(如用户、角色、权限),就是说 SecurityManager 要验证用户身份,那么它需要从 Realm 获取相应的用户进行比较以确定用户身份是否合法;

         也需要从 Realm 得到用户相应的角色 / 权限进行验证用户是否能进行操作;可以把 Realm 看成 DataSource,即安全数据源。

        如我们之前的 ini 配置方式将使用 org.apache.shiro.realm.text.IniRealm

    2.Realm 接口方法

        org.apache.shiro.realm.Realm 接口如下:

    String getName(); //返回一个唯一的Realm名字
    boolean supports(AuthenticationToken token); //判断此Realm是否支持此Token
    AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException;  //根据Token获取认证信息
    

    3.自定义Realm实现

    public class MyRealm1 implements Realm {
        @Override
        public String getName() {
            return "myrealm1";
        }
        @Override
        public boolean supports(AuthenticationToken token) {
            //仅支持UsernamePasswordToken类型的Token
            return token instanceof UsernamePasswordToken; 
        }
        @Override
        public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            String username = (String)token.getPrincipal();  //得到用户名
            String password = new String((char[])token.getCredentials()); //得到密码
            if(!"zhang".equals(username)) {
                throw new UnknownAccountException(); //如果用户名错误
            }
            if(!"123".equals(password)) {
                throw new IncorrectCredentialsException(); //如果密码错误
            }
            //如果身份认证验证成功,返回一个AuthenticationInfo实现;
            return new SimpleAuthenticationInfo(username, password, getName());
        }
    };
    

    4.Shiro 默认提供的Realm

     

        一般继承 AuthorizingRealm(授权)即可,其继承了 AuthenticatingRealm(即身份验证),而且也间接继承了 CachingRealm(带有缓存实现)

    public class CustomRealm extends AuthenticatingRealm{
    
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            
            // 从数据库获取用户名和密码
            String username = "draco";
            String password = "615";
            
            //从用户的输入中生成token,拿到用户名密码
            String inputUsername = (String)token.getPrincipal();
            if(!inputUsername.equals(username)){
                throw new UnknownAccountException("用户不存在");
            }
            
         /* if(status == 0){
                throw new LockedAccountException("用户被锁定");
            }*/
            
            String inputPassword = (String)token.getCredentials();
            if(!inputPassword.equals(password)){
                throw new IncorrectCredentialsException("密码不正确");
            }
            
            System.out.println(this.getName());
             
            String realName = this.getName();
    
            //拿到授权信息
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(inputUsername, inputPassword, realName);
            
            return info;
        }
        
    }

       默认实现:

      org.apache.shiro.realm.text.IniRealm:[users] 部分指定用户名 / 密码及其角色;[roles] 部分指定角色即权限信息;

      org.apache.shiro.realm.text.PropertiesRealm: user.username=password,role1,role2 指定用户名 / 密码及其角色;

                              role.role1=permission1,permission2 指定角色及权限信息;

      org.apache.shiro.realm.jdbc.JdbcRealm通过 sql 查询相应的信息,如 “select password from users where username = ?” 获取用户密码,“select password, password_salt from users where username = ?” 获取用户密码及盐;“select role_name from user_roles where username = ?” 获取用户角色;“select permission from roles_permissions where role_name = ?” 获取角色对应的权限信息;也可以调用相应的 api 进行自定义 sql;

     

      

  • 相关阅读:
    Python中xlrd和xlwt模块使用方法
    python正则表达式中含有变量的写法
    python中取整的几种方法
    python request 获取cookies value值的方法
    MySQL数据库初识
    三次登录验证以及购物车
    常用设计模式学习
    test0805
    生成器和各种推导式
    第一类对象 闭包 迭代器
  • 原文地址:https://www.cnblogs.com/dxjx/p/12572619.html
Copyright © 2011-2022 走看看