美文网首页
使用Shiro默认提供的JDBCRealm

使用Shiro默认提供的JDBCRealm

作者: tingshuo123 | 来源:发表于2018-08-23 17:04 被阅读205次

    项目结构

    image.png
    一个测试函数,个配置Realm,一个maven依赖

    创建数据库

    首先创建一个Shiro数据库,然后建立三个表,分别是:users(用户表)、user_roles(角色表)、roles_permissions(权限表)
    ,添加一个用户。
    Sql:

    /*
    
    SET FOREIGN_KEY_CHECKS=0;
    
    -- ----------------------------
    -- Table structure for roles_permissions
    -- ----------------------------
    DROP TABLE IF EXISTS `roles_permissions`;
    CREATE TABLE `roles_permissions` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `role_name` varchar(100) DEFAULT NULL,
      `permission` varchar(100) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `idx_roles_permissions` (`role_name`,`permission`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    -- Records of roles_permissions
    -- ----------------------------
    
    -- ----------------------------
    -- Table structure for users
    -- ----------------------------
    DROP TABLE IF EXISTS `users`;
    CREATE TABLE `users` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `username` varchar(100) DEFAULT NULL,
      `password` varchar(100) DEFAULT NULL,
      `password_salt` varchar(100) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `idx_users_username` (`username`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    -- Records of users
    -- ----------------------------
    INSERT INTO `users` VALUES ('1', 'admin', 'ad123', null);
    
    -- ----------------------------
    -- Table structure for user_roles
    -- ----------------------------
    DROP TABLE IF EXISTS `user_roles`;
    CREATE TABLE `user_roles` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `username` varchar(100) DEFAULT NULL,
      `role_name` varchar(100) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `idx_user_roles` (`username`,`role_name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    -- ----------------------------
    -- Records of user_roles
    -- ----------------------------
    
    

    pom.xml

    <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">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.project.tingshuo</groupId>
        <artifactId>shior02</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
    
        <dependencies>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.3</version>
            </dependency>
            
            <!-- Shiro -->
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-core</artifactId>
                <version>1.2.2</version>
            </dependency>
    
            <!-- Mysql数据库连接 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.25</version>
            </dependency>
    
            <!-- 阿里Druid连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>0.2.23</version>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-nop</artifactId>
                <version>1.7.2</version>
            </dependency>
    
        </dependencies>
    
    </project>
    

    使用的是阿里的Druid连接池,Mysql 数据库

    shiro-jdbc-realm.ini

    #声明Realm
    jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
    #配置数据源
    dataSource=com.alibaba.druid.pool.DruidDataSource
    dataSource.driverClassName=com.mysql.jdbc.Driver
    dataSource.url=jdbc:mysql://localhost:3306/shiro
    dataSource.username=root
    dataSource.password=""
    #指定数据源
    jdbcRealm.dataSource=$dataSource
    #指定SecurityManager的Realms实现
    securityManager.realms=$jdbcRealm
    

    文件格式必须为ini,编码为ANSI

    测试类

    package shiro02;
    
    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.config.IniSecurityManagerFactory;
    import org.apache.shiro.mgt.SecurityManager;
    import org.apache.shiro.subject.Subject;
    import org.apache.shiro.util.Factory;
    import org.junit.jupiter.api.Test;
    
    public class TestMain {
    
        public static void main(String[] args) {
            // 获取SecurityManager工厂
            Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
                    "classpath:shiro-jdbc-realm.ini");
    
            // 获通过工厂获取SecurityManager实例
            SecurityManager securityManager = factory.getInstance();
    
            // 将实例绑定给SecurityUtils
            SecurityUtils.setSecurityManager(securityManager);
    
            // 获取Subject
            Subject subject = SecurityUtils.getSubject();
    
            // 创建Token
            UsernamePasswordToken token = new UsernamePasswordToken("admin", "ad123");
    
            // 登录(验证用户)
            try {
                subject.login(token);
            } catch (AuthenticationException e) {
                // 验证失败
                e.printStackTrace();
            }
    
            if (subject.isAuthenticated()) {
                System.out.println("登录成功!!!");
                subject.logout();
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:使用Shiro默认提供的JDBCRealm

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