Shiro【快速上手】

一、前言

Shiro【初识】中,已经总结了 Shiro 相关的概念和其架构图,

那么本文将介绍 Springboot2.x 整合 Apache Shiro ,实现快速体验认证和授权的基本流程。

本文中的项目使用环境为:JDK8 + Maven 3.6.3 + SpringBoot 2.4.1 + Shiro 1.4.0

二、步骤

1)引入maven依赖

2)编写认证和授权的测试代码

三、代码实现

Maven 依赖如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.4.0</version>
</dependency>
(一)认证
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;

/**
 * 认证流程测试
 * 其实创建 Realm 的目的除了获取数据之外,
 * 还有一个原因就是需要创建 SecurityManager 对象,从而构造 SecurityManager环境
 */
public class AuthenticationTest {
    private SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();
    private DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();

    @Before
    public void init(){
        // 初始化数据源
        simpleAccountRealm.addAccount("zhangsan", "123");
        simpleAccountRealm.addAccount("lisi", "456");

        // 构造SecurityManager环境
        defaultSecurityManager.setRealm(simpleAccountRealm);
        SecurityUtils.setSecurityManager(defaultSecurityManager);
    }

    @Test
    public void testAuthentication(){
        // 获取当前操作主体
        Subject subject = SecurityUtils.getSubject();

        // 用户输入的账号和密码
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("zhangsan", "123");

        // 通过Subject对象调用login()方法进行认证
        subject.login(usernamePasswordToken);

        // 获取认证结果
        System.out.println("是否认证成功:" + subject.isAuthenticated());
    }
}
(二)授权
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 AuthorizationTest {
    private SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();
    private DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();

    @Before
    public void init(){
        // 初始化数据源
        simpleAccountRealm.addAccount("zhangsan", "123", "admin");
        simpleAccountRealm.addAccount("lisi", "456", "root");

        // 构造SecurityManager环境
        defaultSecurityManager.setRealm(simpleAccountRealm);
        SecurityUtils.setSecurityManager(defaultSecurityManager);
    }

    @Test
    public void testAuthentication(){
        // 获取当前操作主体
        Subject subject = SecurityUtils.getSubject();

        // 用户输入的账号和密码
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("zhangsan", "123");

        // 通过Subject对象调用login()方法进行认证
        subject.login(usernamePasswordToken);

        // 获取认证结果
        System.out.println(" 认证结果:"+subject.isAuthenticated());
        // 判断是否有指定角色(有返回值)
        System.out.println(" 是否有对应的root角色:"+subject.hasRole("root"));
        // 获取账号
        System.out.println(" getPrincipal=" + subject.getPrincipal());
        // 检查是否有指定角色(无返回值)
        subject.checkRole("user");
		// 退出登录
        subject.logout();
        System.out.println("logout后认证结果:"+subject.isAuthenticated());
    }
}

Java新手,若有错误,欢迎指正!

posted @ 2021-03-04 15:41  跑调大叔!  阅读(83)  评论(0编辑  收藏  举报