• shiro之IniRealm


    Shiro认证过程
    创建SecurityManager---》主体提交认证---》SecurityManager认证---》Authenticsto认证---》Realm验证
    
    Shiro授权过程
    创建SecurityManager---》主体授权---》ecurityManager授权---》Authorizer授权---》Realm获取角色权限数据

    1.pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>ylht-shiro</artifactId>
            <groupId>com.ylht</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>shiro-test</artifactId>
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-core</artifactId>
                <version>1.4.0</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.7</version>
                <scope>test</scope>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.45</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.6</version>
            </dependency>
    
    
        </dependencies>
    
    </project>

    2.user.ini

    [users]
    zhanxuewei=123456,admin
    [roles]
    admin=user:delete,user:update

    3.测试类

    package com.ylht.shiro.test;
    
    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.mgt.DefaultSecurityManager;
    import org.apache.shiro.realm.text.IniRealm;
    import org.apache.shiro.subject.Subject;
    import org.junit.Test;
    
    public class IniRealTest {
    
        @Test
        public void testIniReal() {
            //创建iniRealm并指定user.ini位置
            IniRealm iniRealm = new IniRealm("classpath:user.ini");
    
            //1.创建SecurityManager
            DefaultSecurityManager securityManager = new DefaultSecurityManager();
            securityManager.setRealm(iniRealm);
    
            //2.主题提交认证
            SecurityUtils.setSecurityManager(securityManager);
            Subject subject = SecurityUtils.getSubject();
    
            //获取token
            UsernamePasswordToken token = new UsernamePasswordToken("zhanxuewei", "123456");
            subject.login(token);
    
            //认证
            System.out.println(subject.isAuthenticated());
    
            //角色验证
            subject.checkRole("admin");
    
            //权限验证
            subject.checkPermissions("user:delete", "user:update");
        }
    }
  • 相关阅读:
    海龟交易
    暑假攻略:怎样让孩子过一个充实又省钱的假期
    值得追随
    在哪里能找的你想要的答案?
    顺势加仓策略
    交易中 你的加仓策略是怎样的?背后的逻辑是什么?
    驻守深寒:寻找那些有效地关键K线
    统计相关
    求助Ubuntu16.10如何设置默认启动为字符界面
    【Linux系列】Ubuntu ping通,xshell无法连接
  • 原文地址:https://www.cnblogs.com/ylht/p/10201281.html
Copyright © 2020-2023  润新知