shiro认识

一.认识shiro

Apache Shiro是一个强大且易用的Java安全框架,有身份验证授权密码学会话管理

  • 轻量级的权限框架
  • shiro(轻量级,粗粒度) , Spring security(细粒度)
  • RBAC:权限(登录,授权) 用户(n)-角色(n)-权限(n)(资源)

1.1 shiro的四大基石

  1. 身份认证(登录) Authentication
  2. 授权(权限) Authorization
  3. 密码学 Cryptography
    1. 过使用加密算法保持数据安全同时易于使用。

  4. 会话管理 Session Management
    1. 管理用户特定的会话,即使在非 Web 或 EJB 应用程序。

image

1.2 重要的对象

  • Subject:当前用户
  • SecurityManager:权限管理器(所有功能管理)
  • Realm:获取权限数据

image

二.Hello案例

建一个普通的maven项目

2.1 导包

<!--使用shiro需要先导包-->
<dependencies>
    <!--shiro的核心包-->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-core</artifactId>
        <version>1.4.0</version>
    </dependency>
    <!--日志包-->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <!--测试包-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
    </dependency>
</dependencies>

2.2 ini文件

文件中有咱们的用户角色权限

# ini文件里面放的就是咱们的用户,角色,权限,资源

# -----------------------------------------------------------------------------
#  users:用户
#   root:用户名  123456:密码  admin:角色
# -----------------------------------------------------------------------------
[users]
root = 123456, admin
guest = guest, it

# -----------------------------------------------------------------------------
# roles:角色
#   admin = * :admin这个用户拥有所有权限
#   it = employee:* :it这个角色拥有员工的所有权限
#   hr = employee:save :hr这个角色拥有员工添加权限
# -----------------------------------------------------------------------------
[roles]
admin = *
it = employee:*
hr = employee:save

2.3 功能测试

主要测试登录,权限认证;认识API

@Test
public void testHello() throws Exception{
    //①.拿到权限管理对象
    /**
     * 读取了shiro.ini的文件(隐藏了realm) -> 隐藏了iniRealm
     * SecurityManager:权限管理器,shiro的所有功能都放在里面
     */
    Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
    SecurityManager securityManager = factory.getInstance();
    //②.相当于把SecurityManager放到了当前上下文
    /**
     * 可以让我们在当前系统的任何位置都可以拿到SecurityManager对象
     */
    SecurityUtils.setSecurityManager(securityManager);
    //③.拿到当前用户(没有登录就是游客)
    Subject currentUser = SecurityUtils.getSubject();
    System.out.println("用户是否登录:"+currentUser.isAuthenticated());

    //④.如果没有登录,让他进行登录
    if(!currentUser.isAuthenticated()){
        //ctrl+alt+t :包含代码
        try {
            //4.1 准备令牌(对象) 用户名密码令牌
            UsernamePasswordToken token = new UsernamePasswordToken("guest","guest");
            //4.2 进行登录功能
            currentUser.login(token);
        } catch (UnknownAccountException e) {
            //Unknown(未知)Account(账号)Exception:用户名不存在
            e.printStackTrace();
            System.out.println("哥,你是傻子嘛?");
        }catch (IncorrectCredentialsException e){
            //Incorrect(不正确)Credentials(凭证)Exception:密码错误
            e.printStackTrace();
            System.out.println("哥,密码错误了?");
        }catch (AuthenticationException e){
            //AuthenticationException:登录中最大的那个异常
            e.printStackTrace();
            System.out.println("发生了一个神秘的错误!!!");
        }
    }
    System.out.println("用户是否登录:"+currentUser.isAuthenticated());

    System.out.println("是否是管理员角色:"+currentUser.hasRole("admin"));
    System.out.println("是否是IT角色:"+currentUser.hasRole("it"));

    System.out.println("是否可以操作employee:save权限:"+currentUser.isPermitted("employee:save"));
    System.out.println("是否可以操作employee:index权限:"+currentUser.isPermitted("employee:index"));
    System.out.println("是否可以操作department:index权限:"+currentUser.isPermitted("department:index"));

    //⑤.还可以登出(注销)
    currentUser.logout();
    System.out.println("用户是否登录:"+currentUser.isAuthenticated());
}

三.自定义Realm

刚才咱们使用的是准备的好的一个Realm,在Shiro中,除了咱们 刚才使用的iniRealm,还有普通SimpleAccountRealm,JDBCRealm等等.

但是这些Realm都不能满足咱们的需求,而这种情况下,就需要我们自己定义一个Realm来实现相应的功能!

3.1 准备自定义Realm

  • 写一个Realm,继承AuthorizingRealm
  • 提供了两个方法,一个是授权doGetAuthorizationInfo,一个是身份认证 doGetAuthenticationInfo
public class MyRealm extends AuthorizingRealm {

    //授权认证功能就写在这里面
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //从数据库中获取角色并放且放到授权对象中
        Set<String> roles = getRoles();
        authorizationInfo.setRoles(roles);
        //从数据库中获取权限并放且放到授权对象中
        Set<String> perms = getPerms();
        authorizationInfo.setStringPermissions(perms);
        return authorizationInfo;
    }

    /**
     * 假设这里获取到当前用户的角色
     */
    private Set<String> getRoles(){
        Set<String> roles = new HashSet<>();
        roles.add("admin");
        roles.add("it");
        return roles;
    }
    /**
     * 假设这里获取到当前用户的权限
     */
    private Set<String> getPerms(){
        Set<String> perms = new HashSet<>();
        perms.add("employee:index");
        return perms;
    }

    /**
     * 记住:如果这个方法返回null,就代表是用户名错误,shiro就会抛出:UnknownAccountException
     */
    //身份认证(登录)就写在这里面
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1.拿到令牌(UsernamePasswordToken)
        UsernamePasswordToken token =  (UsernamePasswordToken)authenticationToken;
        //2.拿到用户名,判断这个用户是否存在
        // 2.1 拿到传过来的用户名
        String username = token.getUsername();
        // 2.2 根据用户名从数据库中拿到密码(以后会拿用户对象)
        String password = this.getUsers(username);
        // 2.3 如果没有拿到密码(没有通过用户名拿到相应的用户->用户不存在)
        if(password==null){
            return null;
        }

        //记住:我们只在正常完成这里的功能,shiro会判断密码是否正确
        //3.返回 AuthenticationInfo这个对象
        /**
         * 咱们创建对象需要传的参数:
         * Object principal:主体(可以乱写) -> 登录成功后,你想把哪一个对象存下来
         * Object credentials:凭证(就是密码) -> 数据库中的密码
         * credentials(密码)Salt:盐值
         * String realmName : realm的名称(可以乱写)
         */
        //拿到咱们的盐值对象(ByteSource)
        ByteSource salt = ByteSource.Util.bytes("itsource");
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username,password,salt,"myRealm");
        return authenticationInfo;
    }

    /**
     * 假设这里是根据用户名进行的查询
     *  MD5:e10adc3949ba59abbe56e057f20f883e
     *  MD5+10次:4a95737b032e98a50c056c41f2fa9ec6
     *  MD5+10次+itsource:831d092d59f6e305ebcfa77e05135eac
     */
    public String getUsers(String username){
        if("admin".equals(username)){
            return "831d092d59f6e305ebcfa77e05135eac";
        }else if("zhang".equals(username)){
            return "123";
        }
        return null;
    }
}

3.2 测试自定义Realm

 @Test
    public void testMyRealm() throws Exception{
        //一.创建一个SecurityManager对象
        // 1.创建realm对象
        MyRealm myRealm = new MyRealm();
        // 2.创建SecurityManager对象
        DefaultSecurityManager securityManager = new DefaultSecurityManager(myRealm);
        //②.相当于把SecurityManager放到了当前上下文
        SecurityUtils.setSecurityManager(securityManager);
        //③.拿到当前用户
        Subject subject = SecurityUtils.getSubject();

        //Hashed(哈希)Credentials(认证)Matcher(匹配器)
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        //设置哈希算法
        matcher.setHashAlgorithmName("MD5");
        //设置迭代次数
        matcher.setHashIterations(10);
        //把匹配器交给shiro
        myRealm.setCredentialsMatcher(matcher);

        System.out.println("用户是否登录:"+subject.isAuthenticated());
        //④.如果没有登录,让他登录
        if(!subject.isAuthenticated()){
            try {
                UsernamePasswordToken token = new UsernamePasswordToken("admin","123456");
                subject.login(token);
            } catch (UnknownAccountException e) {
                e.printStackTrace();
                System.out.println("用户名错误");
            } catch (IncorrectCredentialsException e) {
                e.printStackTrace();
                System.out.println("密码错误");
            } catch (AuthenticationException e) {
                e.printStackTrace();
                System.out.println("神迷错误");
            }
        }
        System.out.println("用户是否登录:"+subject.isAuthenticated());

        System.out.println("是否是admin角色:"+subject.hasRole("admin"));
        System.out.println("是否是hr角色:"+subject.hasRole("hr"));

        System.out.println("是否有employee:index权限:"+subject.isPermitted("employee:index"));
        System.out.println("是否有employee:save权限:"+subject.isPermitted("employee:save"));
        System.out.println("是否有department:index权限:"+subject.isPermitted("department:index"));


    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心之所向...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值