• shiro小记


    今天主要看了Shiro的认证,授权功能初步了解了一下,其他的功能用的不多,之后再看。

    先说一下Shiro的三个核心概念:

    1、Subject: 代表当前正在执行操作的用户,但Subject代表的可以是人,也可以是任何第三方系统帐号。当然每个subject实例都会被绑定到SercurityManger上。
    2、SecurityManger:SecurityManager是Shiro核心,主要协调Shiro内部的各种安全组件,这个我们不需要太关注,只需要知道可以设置自定的Realm。
    3、Realm:用户数据和Shiro数据交互的桥梁。比如需要用户身份认证、权限认证。都是需要通过Realm来读取数据。

    认证

    下面的例子是以继承了AuthenticatingRealm的自定义Realm来实现自定义认证。
    认证依赖于方法doGetAuthenticationInfo,需要返回一个AuthenticationInfo,通常返回一个他的子类SimpleAuthenticationInfo,构造方法的第一个参数是用户名,第二个是验证密码,第三个是当前realm的className。

    package com.demo.realms;
    
    import org.apache.shiro.authc.*;
    import org.apache.shiro.realm.AuthenticatingRealm;
    
    public class MyRealm extends AuthenticatingRealm {
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)  {
            System.out.println("MyRealm认证中---->用户:"+token.getPrincipal());
            // 可以从token中获取用户名来从数据库中查询数据
            UsernamePasswordToken upToken = (UsernamePasswordToken) token;
            String password="123456";// 假设这是从数据库中查询到的用户密码
            // 创建一个SimpleAuthenticationInfo,第一个参数是用户名,第二个是验证密码,第三个是当前realm的className
            // 验证密码会与用户提交的密码进行比对
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(upToken.getUsername(),password,this.getName());
            return info;
        }
    }
    

    授权

    下面的例子是以继承了AuthorizingRealm的自定义Realm来实现自定义认证和自定义授权。
    授权依赖于方法doGetAuthorizationInfo,需要返回一个AuthorizationInfo,通常返回一个他的子类SimpleAuthorizationInfo。构造SimpleAuthorizationInfo可以空构造,也可以传入一个Set<String> roles来构造。

    package com.demo.realms;
    
    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 java.util.HashSet;
    import java.util.Set;
    
    public class RealmForDouble extends AuthorizingRealm {
    //    授权
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            // 1. 获取授权的用户
            Object principal = principals.getPrimaryPrincipal();
            System.out.println("RealmForDouble授权中---->用户:"+principal);
            //2.下面使用Set<String> roles来构造SimpleAuthorizationInfo
            SimpleAuthorizationInfo info = null;
    //        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            Set<String> roles = new HashSet<>();
            if ("admin".equals(principal)){
                roles.add("admin"); // 假设这个角色是从数据库中查出的
                // 如果SimpleAuthorizationInfo实例化了,
                // 可以这样来加角色,行为需要这样添加
                // 角色可以传构造函数来实例化SimpleAuthorizationInfo
    //            info.addRole("admin");
    //            info.addStringPermission("*");
            }
            if ("guest".equals(principal)){
                roles.add("guest");
            }
            info = new SimpleAuthorizationInfo(roles);
            return info;
        }
    
    //    认证
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            System.out.println("RealmForDouble认证中---->用户:"+token.getPrincipal());
            UsernamePasswordToken upToken = (UsernamePasswordToken) token;
            String password="123456";// 假设这是从数据库中查询到的用户密码
            // 创建一个SimpleAuthenticationInfo,第一个参数是用户名,第二个是验证密码,第三个是当前realm的className
            // 验证密码会与用户提交的密码进行比对
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(upToken.getUsername(),password,this.getName());
            return info;
        }
    }
    

      

    以上内容摘自博客:https://www.cnblogs.com/progor/p/10970971.html#%E4%BE%9D%E8%B5%96%E5%8C%85

  • 相关阅读:
    Jedis与Redisson选型对比
    使用rdbtools工具来解析redis rdb文件
    Generating equals/hashCode implementation but without a call to superclass
    解决 Order By 将字符串类型的数字 或 字符串中含数字 按数字排序问题
    File.delete()和Files.delete(Path path)的区别
    file.deleteOnExit()与file.delete()的区别
    java日期中YYYY与yyyy的区别
    IDEA git分支回退指定的历史版本
    Lucene介绍与使用
    Lombok 学习指南
  • 原文地址:https://www.cnblogs.com/steveshao/p/11636856.html
Copyright © 2020-2023  润新知