美文网首页
shiro验证与授权

shiro验证与授权

作者: John_Phil | 来源:发表于2019-05-06 00:17 被阅读0次

    Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理等功能。

    Shiro有三大核心组件:

    Subject

    Subject表示当前用户,在权限管理的应用程序里往往需要知道谁能够操作什么,谁拥有操作该程序的权利,shiro中则需要通过Subject来提供基础的当前用户信息,Subject 不仅仅代表某个用户,与当前应用交互的任何东西都是Subject,如网络爬虫等。所有的Subject都要绑定到SecurityManager上,与Subject的交互实际上是被转换为与SecurityManager的交互。

    SecurityManager

    SecurityManager是所有Subject的管理者,这是Shiro框架的核心组件,可以把他看做是一个Shiro框架的全局管理组件,用于调度各种Shiro框架的服务。作用类似于SpringMVC中的DispatcherServlet,用于拦截所有请求并进行处理。

    Realm

    Realm是用户的信息认证器和用户的权限认证器,我们需要自己来实现Realm来自定义的管理我们自己系统内部的权限规则。SecurityManager要验证用户,需要从Realm中获取用户。可以把Realm看做是数据源。

    shiro控件组成部分

    Authentication:身份认证/登录,验证用户是不是拥有相应的身份;

    Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限;即判断用户是否能做事情,常见的如:验证某个用户是否拥有某个角色。或者细粒度的验证某个用户对某个资源是否具有某个权限;

    Session Manager:会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中;会话可以是普通JavaSE环境的,也可以是如Web环境的;

    Cryptography:加密,保护数据的安全性,如密码加密存储到数据库,而不是明文存储;

    Web Support:Web支持,可以非常容易的集成到Web环境;

    Caching:缓存,比如用户登录后,其用户信息、拥有的角色/权限不必每次去查,这样可以提高效率;

    Concurrency:shiro支持多线程应用的并发验证,即如在一个线程中开启另一个线程,能把权限自动传播过去;

    Testing:提供测试支持;

    Run As:允许一个用户假装为另一个用户(如果他们允许)的身份进行访问;

    Remember Me:记住我,这个是非常常见的功能,即一次登录后,下次再来的话不用登录了。

    Shiro不会去维护用户、维护权限;这些需要我们自己去设计/提供;然后通过相应的接口注入给Shiro即可。

    shiro的验证

    shiro验证流程图

    关于认证中的 subject.login(token)方法 可以使用realm域进行验证
    可以查看源码


    subject.login(token)方法源码 Securitymanager.login(this,token)方法源码 向下查找源码最终找到执行doGetAuthenticationInfo此方法可调用自定义realm的doGetAuthenticationInfo方法 类所在位置

    此时可以执行自定义的realm

    //非加密密码realm实现

    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            String userName = (String) token.getPrincipal();
            String password = new String((char[])token.getCredentials()); //得到密码
           //  从数据库 取得username-》 zhang 与 password-》 123
    //根据用户输入的userName 从数据库查询
           如果查询不到返回UnknownAccountException或者null
                throw new UnknownAccountException(); // 如果用户名错误
            
            if (!"123".equals(password)) {
                throw new IncorrectCredentialsException(); // 如果密码错误
            }
            // 如果身份认证验证成功,返回一个AuthenticationInfo实现;
            return new SimpleAuthenticationInfo(userName, password, getName());
        }
    
    

    //加密密码realm实现

    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            String userName = (String) token.getPrincipal();
    //根据用户输入的userName 从数据库查询
       // 如果查询不到返回null 或者UnknownAccountException
    //模拟从数据库查询到密码password,散列值
    String password = "f3694f162729b7d0254c6e40260bf15c";
    //从数据库获取 盐salt
    String salt = "qwerty";
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                    userName , password, ByteSource.Util.bytes(salt), this.getName());
                return simpleAuthenticationInfo;
        }
    }
    

    shiro的授权

    shiro授权流程图

    关于认证中的 subject.isPermitted("user:update")方法 可以使用realm域中的doGetAuthorizationInfo方法进行验证


    subject.isPermitted(String permission)方法 AuthorizingRealm类中的isPermitted方法 调用getAuthorizationInfo方法会调用realm中的getAuthorizationInfo授权方法

    realm授权方法

    分为两大类第一是基于角色授权,第二是基于资源(许可)授权
    基于角色授权方法:setRoles(Set<String>);addRoles(String);addRoles(Collection<String>);
    基于资源(许可)授权方法:
    setStringPermissions(Set<String>);addStringPermissions(String);addStringPermissions(Collection<String>);
    setObjectPermissions(Set<String>);addtObjectPermissions(String);addtObjectPermissions(Collection<String>);


    authrization授权方法

    realm授权基于角色role授权

     @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
                    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
                Set rolesets=new HashSet();
                rolesets.add("user");
                rolesets.add("admin");
                simpleAuthorizationInfo.setRoles(rolesets);
                return simpleAuthorizationInfo;
        }
    

    realm授权基于资源(许可)permit授权

     @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
                    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
                Set permitset=new HashSet();
                permitset.add("update");
                permitset.add("delete");
                simpleAuthorizationInfo.setStringPermissions(permitset);
                return simpleAuthorizationInfo;
        }
    
     @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
                    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
                Set permitset=new HashSet();
                permitset.add(new WildcardPermission("update"));
                permitset.add(new WildcardPermission("delete"));
                simpleAuthorizationInfo.setObjectPermissions(permitset);
                return simpleAuthorizationInfo;
        }
    

    相关文章

      网友评论

          本文标题:shiro验证与授权

          本文链接:https://www.haomeiwen.com/subject/ipdywqtx.html