• shiro之SimpleAccountRealm


    我使用的是maven构建的工程,junit测试

    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.测试类

    
    
    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.SimpleAccountRealm;
    import org.apache.shiro.subject.Subject;
    import org.junit.Before;
    import org.junit.Test;

    public class AuthenticateTest {

    //定义realm
    SimpleAccountRealm realm = new SimpleAccountRealm();

    @Before
    public void addUser(){
    //realm定义多个角色
    //username,password,roles(后面用逗号隔开)
    realm.addAccount("admin","admin","admin","user");
    }

    @Test
    public void testAuthenticate() {
    //1.创建SecurityManager
    DefaultSecurityManager securityManager = new DefaultSecurityManager();
    securityManager.setRealm(realm);

    //2.主题提交认证
    SecurityUtils.setSecurityManager(securityManager);
    Subject subject = SecurityUtils.getSubject();

    UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");
    subject.login(token);

    System.out.println(subject.isAuthenticated());

    //subject.logout();

    //System.out.println(subject.isAuthenticated());
    //验证多个授权
    subject.checkRoles("admin","user");

    }
    }
  • 相关阅读:
    解决显示器闪屏的问题
    装完系统后由于分辨率问题不能进入系统怎么办?怎么能不进入系统设置分表率?
    如何设置win7任务栏的计算机快速启动
    【转载】Oracle层次查询和分析函数
    【原创】birt 报表工具 不能运行 不能预览问题
    【转】最牛B的编码套路
    Windows下python环境配置
    python:numpy 下载
    TEX(LaTEX)输出PDF设置US LETTER或者LETTERPAPER办法
    Adobe Illustrator Cs6【AI cs6】中文破解版安装图文教程、破解注册方法
  • 原文地址:https://www.cnblogs.com/ylht/p/10201280.html
Copyright © 2020-2023  润新知