创建测试工程

加入shiro-core的jar包及其依赖包

与其它java开源框架类似,将shiro的jar包加入项目就可以使用shiro提供的功能了。shiro-core是核心包必须选用,还提供了与web整合的shiro-web、与spring整合的shiro-spring、与任务调度quartz整合的shiro-quartz等,下边是shiro各jar包的maven坐标。

<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-quartz</artifactId>
<version>1.3.2</version>
</dependency> 也可以通过引入shiro-all包括shiro所有的包:
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>1.3.2</version>
</dependency>

shiro各jar包的maven坐标

创建工程目录

shiro.ini

通过Shiro.ini配置文件初始化SecurityManager环境。

配置 eclipse支持ini文件编辑:

在eclipse配置后,在classpath创建shiro-realm.ini配置文件,为了方便测试将用户名和密码配置的shiro.ini配置文件中:

[users]
zhang=111111

认证代码

public class TestAhentication {

    @Test
public void testLoginAndLogout() { // 构建SecurityManager工厂,IniSecurityManagerFactory可以从ini文件中初始化SecurityManager环境
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini"); // 通过工厂创建SecurityManager
SecurityManager securityManager = factory.getInstance(); // 将securityManager设置到运行环境中
SecurityUtils.setSecurityManager(securityManager); // 创建一个Subject实例,该实例认证要使用上边创建的securityManager进行
Subject subject = SecurityUtils.getSubject(); // AuthenticationToken arg0
UsernamePasswordToken token = new UsernamePasswordToken("zhang","111111"); try {
subject.login(token);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 用户认证状态
boolean isAuthenticated = subject.isAuthenticated();
System.out.println("用户登录认证结果:"+isAuthenticated); //密码错误异常 org.apache.shiro.authc.IncorrectCredentialsException
//账号错误异常 org.apache.shiro.authc.UnknownAccountException subject.logout();
System.out.println("用户退出认证结果"+subject.isAuthenticated());
}
}

常见的异常

UnknownAccountException

账号不存在异常如下:

org.apache.shiro.authc.UnknownAccountException: Realm [org.apache.shiro.realm.text.IniRealm@7c75222b] was unable to find account data for the submitted AuthenticationToken [org.apache.shiro.authc.UsernamePasswordToken - zhang, rememberMe=false].

IncorrectCredentialsException

当输入密码错误会抛此异常,如下:

org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - zhang, rememberMe=false] did not match the expected credentials.

更多异常

认证执行流程

1、 创建token令牌,token中有用户提交的认证信息即账号和密码

2、 执行subject.login(token),提交给securityManager (安全管理器),安全管理器在通过Authenticator进行认证,

3、 Authenticator 安排ModularRealmAuthenticator调用realm从ini配置文件取用户真实的账号和密码,这里使用的是IniRealm(shiro自带)

4、 IniRealm先根据token中的账号去ini中找该账号,如果找不到则给ModularRealmAuthenticator返回null,如果找到则匹配密码,匹配密码成功则认证通过。

通过token获取user的过程

密码匹配的过程源码

自定义Realm实现通过数据库查询数据

  上边的程序使用的是Shiro自带的IniRealm,IniRealm从ini配置文件中读取用户的信息,大部分情况下需要从系统的数据库中读取用户信息,所以需要自定义realm。

shiro提供的realm

最基础的是Realm接口,CachingRealm负责缓存处理,AuthenticationRealm负责认证,AuthorizingRealm负责授权,通常自定义的realm继承AuthorizingRealm。

创建自定义realm类

public class CustomRealm extends AuthorizingRealm

public class CustomRealm extends AuthorizingRealm{

    public void setName() {
super.setName("customRealm");
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token){ //获取认证凭证
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
String username = upToken.getUsername(); if (username == null) {
throw new AuthenticationException("账号为空");
}
//进行数据库查询账号信息 //模拟数据库查询信息
if (!username.equals("zhangsan1")) {
return null;
}
//用户存在 获取到数据库的密码 返回出去
//假设密码是1111
String password = "1111"; SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password, getName()); return info;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO Auto-generated method stub
return null;
}
}

shiro-customRealm.ini

[main]
#自定义 realm
customRealm=com.td.shiro.realm.CustomRealm
#将realm设置到securityManager
securityManager.realms=$customRealm

进行测试

@Test
public void testLoginAndLogout1() {
// 构建SecurityManager工厂,IniSecurityManagerFactory可以从ini文件中初始化SecurityManager环境
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-customRealm.ini");
// 通过工厂创建SecurityManager
SecurityManager securityManager = factory.getInstance();
// 将securityManager设置到运行环境中
SecurityUtils.setSecurityManager(securityManager);
// 创建一个Subject实例,该实例认证要使用上边创建的securityManager进行
Subject subject = SecurityUtils.getSubject();
// AuthenticationToken arg0
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","1111");
try {
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
} // 用户认证状态
boolean isAuthenticated = subject.isAuthenticated();
System.out.println("用户登录认证结果:"+isAuthenticated);
//密码错误异常 org.apache.shiro.authc.IncorrectCredentialsException
//账号错误异常 org.apache.shiro.authc.UnknownAccountException
subject.logout();
System.out.println("用户退出认证结果"+subject.isAuthenticated());
}

shiro认证流程的更多相关文章

  1. 菜鸟手把手学Shiro之shiro认证流程

    一.使用的spring boot +mybatis-plus+shiro+maven来搭建项目框架 <!--shiro--> <dependency> <groupId& ...

  2. Shiro第二篇【介绍Shiro、认证流程、自定义realm、自定义realm支持md5】

    什么是Shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和sp ...

  3. Shiro learning - 认证流程(3)

    Shiro认证流程 在学习认证流程之前,你应该先了解Shiro的基本使用流程 认证 身份认证: 证明用户是谁.用户需要提供相关的凭证principals(身份标识)和Credentials (凭证,证 ...

  4. shiro框架学习-2-springboot整合shiro及Shiro认证授权流程

    1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  5. frame shiro 认证示例及原理简述

    shiro 认证流程 1.创建一个 javaSE 的maven项目(quickstart),并添加依赖 <dependency> <groupId>junit</grou ...

  6. Shiro——认证

    引入shiro依赖 <!-- shiro --> <dependency> <!-- shiro-core Required in all environments. - ...

  7. cas 3.5.3服务器搭建+spring boot集成+shiro模拟登录(不修改现有shiro认证架构)

    因为现有系统外部接入需要,需要支持三方单点登录.由于系统本身已经是微服务架构,由多个业务独立的子系统组成,所以有自己的用户认证微服务(不是cas,我们基础设施已经够多了,现在能不增加就不增加).但是因 ...

  8. shiro基础学习(二)—shiro认证

    一.shiro简介      shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证.权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 以下 ...

  9. shiro认证登录实现

    准备工作: 在web.xml中配置shiro核心过滤器 在spring配置文件中提供核心过滤器运行所需要的辅助bean对象,在对象内注入安全管理器 拦截认证 配置三个url 拦截除了登录页面以及认证a ...

随机推荐

  1. pandas 中有关isin()函数的介绍,python中del解释

  2. CentOS7更改运行级别

    Step 1:查看系统默认运行级别 [root@node-1 html]# systemctl get-default       //图形界面graphical.target [root@node- ...

  3. 从零开始学spring cloud(七) -------- Spring Cloud OpenFegin

    一.OpenFegin 介绍 Feign是一个声明性的Web服务客户端. 它使编写Web服务客户端变得更容易. 要使用Feign,请创建一个界面并对其进行注释. 它具有可插入的注释支持,包括Feign ...

  4. 将python文件打包成exe可运行文件

    https://blog.csdn.net/douzhenwen/article/details/78886244

  5. Workerman创建聊天室实例

    // 标记是全局启动 define('GLOBAL_START', 1); require_once __DIR__ . '/Workerman/Connection.php'; require_on ...

  6. AD 10使用技巧---新学习

    1.修改设计(D)-规则(R) (1)布线线宽(routing)8mil (2)线间距Clearance 8mil (3)过孔大小RoutingVias 2.布局摆放---按主控芯片的管脚要连接的电路 ...

  7. NC 部署问题

    1.was环境部署日志  IBM/WEBSPHERE/APPSERVER/PRORFILES/APPSRV01/LOGS/SERVER1/ 

  8. 刷shipid 简便方法

    将表中的数据手动更改: select * from cmpps025 where  pino = ''; insert into cmpps025 select ncmp, pino, pono, i ...

  9. Web性能和负载测试工具补充

    压力测试文档:https://yq.aliyun.com/articles/377543https://www.cnblogs.com/ahjxxy/archive/2012/09/17/268899 ...

  10. 描点的改进:运用chart画图。

    主要是利用Chart画图: 通过选中一部分曲线进行图像的放大和缩小,最小值为1格. 先计算最大值和最小差值.然后赋值给AxisY.Minimum 和AxisY.Maximum.x轴初始显示数目:Axi ...