Shiro的认证流程大体可以参考下面这幅图:

但是没有接触过shiro的同学看到上面的图片也不明白,下面我们来在代码中尝试体验Shiro的认证过程:

1.新建一个SpringBoot项目项目结构如下:

ShiroframeApplicationTests代码:

package com.shiro.shiroframe;

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.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; public class ShiroframeApplicationTests { //realm,暂时用来存储我们假造的用户信息
SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();
@BeforeEach//@BeforeEach注解的作用就是使她下面的方法在其他方法运行之前执行
public void addUser(){
//设置假造的用户信息,在Realm里面添加一个用户
simpleAccountRealm.addAccount("qqq", "aaa");
}
@Test
public void authenticator() {
//1.构建SecurityManager环境
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
//设置SecurityManager环境下的Realm
defaultSecurityManager.setRealm(simpleAccountRealm);
//SecurityUtils先获取SecurityManager环境
SecurityUtils.setSecurityManager(defaultSecurityManager);
//通过SecurityUtils获取Subject主体
Subject subject = SecurityUtils.getSubject();
//通过UsernamePasswordToken组织提交认证所要传递的参数
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("qqq", "aaa");
//提交登录认证
subject.login(usernamePasswordToken);
//打印是否认证通过:subject.isAuthenticated()
System.err.println("isAuthenticated:" + subject.isAuthenticated());//账号密码匹配的情况下打印结果:isAuthenticated:true;否则控制台报错:org.apache.shiro.authc.UnknownAccountException: Realm [org.apache.shiro.realm.SimpleAccountRealm@2cbb3d47] was unable to find account data for the submitted AuthenticationToken [org.apache.shiro.authc.UsernamePasswordToken - qq, rememberMe=false].
//登出
subject.logout();
//登出之后认证返回false
System.err.println("isAuthenticated:" + subject.isAuthenticated());//isAuthenticated:false
} }

2、Shiro的认证的更多相关文章

  1. Apache shiro集群实现 (三)shiro身份认证(Shiro Authentication)

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  2. Shiro入门之一 -------- Shiro权限认证与授权

    一  将Shirojar包导入web项目 二 在web.xml中配置shiro代理过滤器 注意: 该过滤器需要配置在struts2过滤器之前 <!-- 配置Shiro的代理过滤器 -->  ...

  3. Shiro的认证和权限控制

    权限控制的方式 从类别上分,有两大类: - 认证:你是谁?–识别用户身份. - 授权:你能做什么?–限制用户使用的功能. 权限的控制级别 从控制级别(模型)上分: - URL级别-粗粒度 - 方法级别 ...

  4. spring boot(十四)shiro登录认证与权限管理

    这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...

  5. 学习Spring Boot:(十三)配置 Shiro 权限认证

    经过前面学习 Apache Shiro ,现在结合 Spring Boot 使用在项目里,进行相关配置. 正文 添加依赖 在 pom.xml 文件中添加 shiro-spring 的依赖: <d ...

  6. springboot+mybatis+shiro——登录认证和权限控制

    转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...

  7. Shiro 登录认证源码详解

    Shiro 登录认证源码详解 Apache Shiro 是一个强大且灵活的 Java 开源安全框架,拥有登录认证.授权管理.企业级会话管理和加密等功能,相比 Spring Security 来说要更加 ...

  8. Shiro身份认证---转

    目录 1. Shro的概念 2. Shiro的简单身份认证实现 3. Shiro与spring对身份认证的实现 前言: Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在 JavaSE 环境 ...

  9. Shiro身份认证、盐加密

    目的: Shiro认证 盐加密工具类 Shiro认证 1.导入pom依赖 <dependency> <groupId>org.apache.shiro</groupId& ...

随机推荐

  1. git 计算commit

    转自:https://www.cnblogs.com/jwentest/p/8186712.html#!/bin/bash f1="$1*" f2="$2*" ...

  2. 关于 i++ 和 ++ i

    先看一下代码,猜想一下输出值 @Testpublic void test() { int i =1; int a,b=0; i++; a=(i++); System.out.println(a); S ...

  3. 安装wordpress的过程

    1 首先安装lamp.在安装php时,由于ubuntu16.04源中自带的是php7,所以需要直接安装 apt-get install phpapt-get install libapache2-mo ...

  4. 【转】Linux iptables 详解

    转自:https://www.cnblogs.com/qwertwwwe/p/9452370.html 最近搭一个框架需要用到iptables做映射,学习了下iptables的原理,总结下方便以后查~ ...

  5. linux高性能服务器编程pdf免费下载

    百度云盘:链接: https://pan.baidu.com/s/1pLp4hHx 密码: wn4k

  6. git 与gitlab

    1.gitlab 创建project ,命名为test2 2.git push项目 git remote add ******* mkdir test1 cd test1 git init nano ...

  7. null 的应用

    它是 Oracle 中非常特殊的一种类型.它表示不确定,表示没有值.并且它能转化成所有的类型. 向数据库中插入空字符串时,oracle 会把它自动转化为 null 类型.所以,在查询空字符的时候: s ...

  8. 九、爬虫框架之Scrapy

    爬虫框架之Scrapy 一.介绍 二.安装 三.命令行工具 四.项目结构以及爬虫应用简介 五.Spiders 六.Selectors 七.Items 八.Item Pipelin 九. Dowload ...

  9. 第六篇:Scrapy框架

    爬虫框架之Scrapy 一.介绍 二.安装 三.命令行工具 四.项目结构以及爬虫应用简介 五.Spiders 六.Selectors 七.Items 八.Item Pipelin 九. Dowload ...

  10. python类库32[多进程通信Queue+Pipe+Value+Array]

    多进程通信 queue和pipe的区别: pipe用来在两个进程间通信.queue用来在多个进程间实现通信. 此两种方法为所有系统多进程通信的基本方法,几乎所有的语言都支持此两种方法. 1)Queue ...