不同的Realm就是不同的认证数据源。

1. IniRealm

1.1 导入Maven

pom.xml

<dependencies>
  <dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.2.2</version>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>RELEASE</version>
  </dependency>
</dependencies>

1.2. 在资源文件夹内创建user.ini

resources/user.ini

[users]
BenZhu=123456,admin
[roles]
admin=user:delete

1.3. 编写测试类

IniRealmTest.java

package com.benzhu.test;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;

public class IniRealmTest {

    @Test
    public void tsetAuthentication(){

        IniRealm iniRealm = new IniRealm("classpath:user.ini");

//1.构建SecurityManager环境
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(iniRealm);
//2.主体提交认证请求
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("BenZhu","123456");
        try {
            subject.login(token);
            try {
                subject.checkRoles("admin");
                subject.checkPermission("user:delete");
            }catch (UnauthorizedException exception){
                System.out.println("角色授权或者权限授权失败!");
            }

        }catch (AuthenticationException e){
            System.out.println("认证失败!");
        }

        System.out.println("isAuthenticated:"+subject.isAuthenticated());
        subject.logout();
        System.out.println("isAuthenticated:"+subject.isAuthenticated());
    }
}

2. 默认JdbcRealm

2.1 导入Maven

pom.xm

<dependencies>
  <dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.2.2</version>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>RELEASE</version>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.45</version>
  </dependency>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.6</version>
  </dependency>
</dependencies>

2.2. 创建数据库表

这里用到数据库为mysql5.7

2.3. 编写测试类

JdbcRealmTest.java

package com.benzhu.test;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class JdbcRealmTest {

    DruidDataSource dataSource = new DruidDataSource();

    {
        dataSource.setUrl("jdbc:mysql://localhost:3306/shiro?&useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
    }

    @Test
    public void tsetAuthentication(){

        JdbcRealm jdbcRealm = new JdbcRealm();
        jdbcRealm.setDataSource(dataSource);
        jdbcRealm.setPermissionsLookupEnabled(true); //开启权限授权

//1.构建SecurityManager环境
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(jdbcRealm);
//2.主体提交认证请求
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("benzhu","123456");
        try {
            subject.login(token);
            try {
                subject.checkRole("admin");
                subject.checkPermission("user:update");
            }catch (UnauthorizedException exception){
                System.out.println("角色授权或者权限授权失败!");
            }

        }catch (AuthenticationException e){
            System.out.println("认证失败!");
        }

        System.out.println("isAuthenticated:"+subject.isAuthenticated());
        subject.logout();
        System.out.println("isAuthenticated:"+subject.isAuthenticated());
    }
}

3. 自定义JdbcRealm

3.1. 如同2一样先导入Maven

3.2. 编写自定义的数据库

参考如下:

3.3 编写测试类

JdbcRealmTest.java

package com.benzhu.test;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class JdbcRealmTest {

    DruidDataSource dataSource = new DruidDataSource();

    {
        dataSource.setUrl("jdbc:mysql://localhost:3306/shiro?&useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
    }

    @Test
    public void tsetAuthentication(){

        JdbcRealm jdbcRealm = new JdbcRealm();
        jdbcRealm.setDataSource(dataSource);
        jdbcRealm.setPermissionsLookupEnabled(true);

//编写自定义Sql认证语句
        String sql = "select password from test_user where username = ?";
        jdbcRealm.setAuthenticationQuery(sql);

//编写自定义Sql角色授权语句
        String rolesql = "select role from test_user_roles where username = ?";
        jdbcRealm.setUserRolesQuery(rolesql);

//编写自定义Sql权限授权语句
        String permissionssql = "select permission from test_permissions where role = ?";
        jdbcRealm.setPermissionsQuery(permissionssql);

//1.构建SecurityManager环境
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        defaultSecurityManager.setRealm(jdbcRealm);
//2.主体提交认证请求
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("benzhu","666666");
        try {
            subject.login(token);
            try {
                subject.checkRole("zhu");
                subject.checkPermission("select");
            }catch (UnauthorizedException exception){
                System.out.println("角色授权或者权限授权失败!");
            }

        }catch (AuthenticationException e){
            System.out.println("认证失败!");
        }

        System.out.println("isAuthenticated:"+subject.isAuthenticated());
        subject.logout();
        System.out.println("isAuthenticated:"+subject.isAuthenticated());
    }
}

4. 效果图

三个都一样滴: