2、Shiro的认证
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的认证的更多相关文章
- Apache shiro集群实现 (三)shiro身份认证(Shiro Authentication)
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- Shiro入门之一 -------- Shiro权限认证与授权
一 将Shirojar包导入web项目 二 在web.xml中配置shiro代理过滤器 注意: 该过滤器需要配置在struts2过滤器之前 <!-- 配置Shiro的代理过滤器 --> ...
- Shiro的认证和权限控制
权限控制的方式 从类别上分,有两大类: - 认证:你是谁?–识别用户身份. - 授权:你能做什么?–限制用户使用的功能. 权限的控制级别 从控制级别(模型)上分: - URL级别-粗粒度 - 方法级别 ...
- spring boot(十四)shiro登录认证与权限管理
这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...
- 学习Spring Boot:(十三)配置 Shiro 权限认证
经过前面学习 Apache Shiro ,现在结合 Spring Boot 使用在项目里,进行相关配置. 正文 添加依赖 在 pom.xml 文件中添加 shiro-spring 的依赖: <d ...
- springboot+mybatis+shiro——登录认证和权限控制
转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...
- Shiro 登录认证源码详解
Shiro 登录认证源码详解 Apache Shiro 是一个强大且灵活的 Java 开源安全框架,拥有登录认证.授权管理.企业级会话管理和加密等功能,相比 Spring Security 来说要更加 ...
- Shiro身份认证---转
目录 1. Shro的概念 2. Shiro的简单身份认证实现 3. Shiro与spring对身份认证的实现 前言: Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在 JavaSE 环境 ...
- Shiro身份认证、盐加密
目的: Shiro认证 盐加密工具类 Shiro认证 1.导入pom依赖 <dependency> <groupId>org.apache.shiro</groupId& ...
随机推荐
- Linux系统Tomcat进程使用shutdown无法关闭进程
问题场景: 若在应用中启动了用户线程,在Linux系统Tomcat进程使用shutdown无法关闭进程. 解决方案: #1.在catalina.sh文件中添加CATALINA_PID [root@lo ...
- linux yum错误
一. 错误:File contains no section headers. file: file:///etc/yum.repos.d/upgrade.repo, line: 1 'name=Ce ...
- SSH整合——登录模块
1.导包——参照我的GitHub Hibernate hibernate/lib/required hibernate/lib/jpa 数据库驱动 Struts2 struts-blank.war/W ...
- zencart后台修改首页meta_title、meta_keywords、meta_description
includes\languages\english\模板\meta_tags.php 首先,将meta_tags.php中常量HOME_PAGE_META_DESCRIPTION.HOME_PAGE ...
- php随机获取数组里面的值
srand() 函数播下随机数发生器种子,array_rand() 函数从数组中随机选出一个或多个元素,并返回.第二个参数用来确定要选出几个元素.如果选出的元素不止一个,则返回包含随机键名的数组,否则 ...
- LVM介绍及相关操作
一.逻辑卷管理器介绍 逻辑卷管理器(英语:Logical Volume Manager,缩写为LVM),又译为逻辑卷宗管理器.逻辑扇区管理器.逻辑磁盘管理器,是Linux核心所提供的逻辑卷管理(Log ...
- ini文件多了个dos的^M结尾符号,导致linux下脚本程序不能运行
[omcr@lnlte2dmr-tdl legacy]$ cat -A ums_del_mr_files_cfg.ini MrFileDiskMountPoint=/home^M$ MrFileDis ...
- Spring的新注解
1 @Configuration 1.1 作用 用于指定当前类是一个Spring的配置类,当创建容器的时候会从该类上加载注解.获取容器的时候需要使用AnnotationApplicationConte ...
- css标签样式 边框 盒子模型 定位 溢出 z-index 透明度
目录 一.CSS设置标签样式 1. 给标签设置长和宽 2.字体的颜色(3种表示方法) 3.文本样式 4. 语义 5. 背景样式 6. 边框 6.1 边框变圆 7.display 标签转换 二.盒子模型 ...
- day_06 猜年龄游戏,三级菜单 ,求1 - 2 + 3 - 4 + 5...99的所有数的和(课后作业)
1.猜年龄游戏: 要求: 允许用户最多尝试3次 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 如果猜对了,有三次选择奖励 ...