写在前面

在上一篇文章《shiro认证流程源码分析--练气初期》当中,我们简单分析了一下shiro的认证流程。不难发现,如果我们需要使用其他数据源的信息完成认证操作,我们需要自定义Realm继承AuthorizingRealm类,并实现两个方法,分别对应授权和认证。

在这一篇文章当中,我们将介绍如何自定义Realm对象,完成认证信息数据源的切换。

自定义Reaml

/**自定义Realm对象
* @author 赖柄沣 bingfengdev@aliyun.com
* @version 1.0
* @date 2020/10/4 11:00
*/
public class MySqlRealm extends AuthorizingRealm { /**授权,今天暂不实现
* @author 赖柄沣 bingfengdev@aliyun.com
* @date 2020-10-04 11:01:50
* @param principalCollection
* @return org.apache.shiro.authz.AuthorizationInfo
* @throws AuthenticationException
* @version 1.0
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { return null;
} /**认证
* @author 赖柄沣 bingfengdev@aliyun.com
* @date 2020-10-04 11:01:50
* @param authenticationToken
* @return org.apache.shiro.authz.AuthorizationInfo
* @throws AuthenticationException
* @version 1.0
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
// 1. 从token中获取用户名
String principal = (String) authenticationToken.getPrincipal(); //2. 根据用户名查询数据库(模拟)
if (principal == "xiangbei") {
AuthenticationInfo authInfo = new SimpleAuthenticationInfo("xiangbei","123",this.getName());
return authInfo;
}
return null;
}
}

在认证器中使用自定义Realm进行认证

/**认证管理器
* @author 赖柄沣 bingfengdev@aliyun.com
* @version 1.0
* @date 2020/10/4 11:11
*/
public class CurrentSystemAuthenticator {
private DefaultSecurityManager securityManager;
public CurrentSystemAuthenticator() {
//创建安全管理器
securityManager = new DefaultSecurityManager(); //设置自定义realm
this.securityManager.setRealm(new MySqlRealm()); //将安全管理器设置到安全工具类中
SecurityUtils.setSecurityManager(securityManager); } public void authenticate(String username,String password){
//获取当前登录主题
Subject subject = SecurityUtils.getSubject(); //生成toeken
UsernamePasswordToken token = new UsernamePasswordToken(username, password); //进行认证
try {
subject.login(token);
}catch (UnknownAccountException | IncorrectCredentialsException e) {
System.out.println("用户名或密码不正确");
} //打印认证状态
if (subject.isAuthenticated()){
System.out.println(token.getPrincipal()+" 认证通过!");
}else {
System.out.println(token.getPrincipal()+" 认证未通过!");
} }
}

进行测试

认证通过的情况

用例代码

/**测试认证
* @author 赖柄沣 bingfengdev@aliyun.com
* @version 1.0
* @date 2020/9/21 0:49
*/
public class TestAuthenticator {
private Authenticator authenticator=null; @Before
public void init() {
authenticator = new Authenticator();
} @Test
public void testAuth(){ authenticator.authenticate("xiangbei","123");
}
}

输出

xiangbei 认证通过!

认证不通过的情况

认证不通过的情况在shiro当中分为几种情况,具体可以查看我的上一篇文章《shiro认证流程源码分析--练气初期》 关于shiro认证异常的分析,常用的有如下几种:

  1. 账户不正确(不存在)
  2. 密码错误
  3. 账户被锁定
  4. 密码过期

在实际项目中为了安全起见,账户不正确和密码错误统一返回“用户名或密码不正确”类似的的提示,避免造成账户泄露。

下面针对这种情况给予演示

用例代码

/**
* @author 赖柄沣 bingfengdev@aliyun.com
* @version 1.0
* @date 2020/10/4 11:20
*/
public class AuthcTest {
private CurrentSystemAuthenticator authenticator;
@Before
public void init() {
this.authenticator = new CurrentSystemAuthenticator();
} @Test
public void testAuthc(){
this.authenticator.authenticate("xiangbei","13");
}
}

输出

用户名或密码不正确
xiangbei 认证未通过!

写在最后

这一篇文章主要是带领大家了解一下如何通过自定义Realm对象完成shiro认证数据源的切换。对于MySQL的集成,我们将在后面的文章当中集成SpringBoot时介绍。

下一篇文章将简单介绍shiro中的密码加密以及如何配置使用。

本文所涉及的代码下载地址:https://github.com/code81192/art-demo/tree/master/shiro-authc-mysql

Shiro入门学习---使用自定义Realm完成认证|练气中期的更多相关文章

  1. Shiro入门学习之自定义Realm实现认证(四)

    一.概述 Shirom默认使用自带的IniRealm,IniRealm从ini配置文件中读取用户的信息,而大部分情况下需要从系统数据库中读取用户信息,所以需要实现自定义Realm,Realm接口如下: ...

  2. Shiro入门学习之自定义Realm实现授权(五)

    一.自定义Realm授权 前提:认证通过,查看Realm接口的继承关系结构图如下,要想通过自定义的Realm实现授权,只需继承AuthorizingRealm并重写方法即可 二.实现过程 1.新建mo ...

  3. Shiro入门学习之shi.ini实现认证及源码分析(二)

    一.Shiro.ini文件 1.文件说明 ①ini(InitializationFile)初始文件:Window系统文件扩展名 ②Shiro使用时可以连接数据库,也可以不连接数据库(可以使用shiro ...

  4. shiro入门学习--使用MD5和salt进行加密|练气后期

    写在前面 在上一篇文章<Shiro入门学习---使用自定义Realm完成认证|练气中期>当中,我们学会了使用自定义Realm实现shiro数据源的切换,我们可以切换成从关系数据库如MySQ ...

  5. Shiro入门学习之shi.ini实现授权(三)

    一.Shiro授权 前提:需要认证通过才会有授权一说 1.授权过程 2.相关方法说明 ①subject.hasRole("role1"):判断是否有该角色 ②subject.has ...

  6. Shiro入门学习与实战(一)

    一.概述 1.Shiro是什么? Apache Shiro是java 的一个安全框架,主要提供:认证.授权.加密.会话管理.与Web集成.缓存等功能,其不依赖于Spring即可使用: Spring S ...

  7. Shiro入门学习之散列算法与凭证配置(六)

    一.散列算法概述 散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如MD5.SHA等,一般进行散列时最好提供一个salt(“盐”),什么意思?举个栗子 ...

  8. shiro入门学习--授权(Authorization)|筑基初期

    写在前面 经过前面的学习,我们了解了shiro中的认证流程,并且学会了如何通过自定义Realm实现应用程序的用户认证.在这篇文章当中,我们将学习shiro中的授权流程. 授权概述 这里的授权指的是授予 ...

  9. shiro框架学习-5-自定义Realm

    1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...

随机推荐

  1. 2020,6招玩转 Appium 自动化测试

    Appium是个什么鬼 Appium是一个移动端的自动化框架,可用于测试原生应用,移动网页应用和混合型应用,且是跨平台的.可用于IOS和Android以及firefox的操作系统.原生的应用是指用an ...

  2. 力扣Leetcode 33. 搜索旋转排序数组

    33. 搜索旋转排序数组 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值, ...

  3. JS - 日期时间比较函数

    JS日期比较(yyyy-mm-dd) function duibi(a, b) { var arr = a.split("-"); var starttime = new Date ...

  4. python笔记-标准库unittest

    unittest核心工作原理 unittest中最核心的四个概念是:test case, test suite, test runner, test fixture. 一个TestCase的实例就是一 ...

  5. [PyTorch 学习笔记] 5.1 TensorBoard 介绍

    本章代码: https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson5/tensorboard_methods.py http ...

  6. P1082 同余方程(拓展欧几里德)

    题目描述 求关于xx的同余方程 a x \equiv 1 \pmod {b}ax≡1(modb) 的最小正整数解. 输入输出格式 输入格式: 一行,包含两个正整数 a,ba,b,用一个空格隔开. 输出 ...

  7. Inscribed Figures(思维)

    The math faculty of Berland State University has suffered the sudden drop in the math skills of enro ...

  8. 从String中移除空白字符的多种方式!?

    字符串,是Java中最常用的一个数据类型了.我们在日常开发时候会经常使用字符串做很多的操作.比如字符串的拼接.截断.替换等. 这一篇文章,我们介绍一个比较常见又容易被忽略的一个操作,那就是移除字符串中 ...

  9. 一文搞懂WordPress建站

    文章首发于:https://zouwang.vip/ 日日夜夜的等待,WordPress建站教程终于来了.本篇文章适用于第一次建站的小白,帮助你从零搭建起一个属于自己的网站,既然是从零,那么我就会带着 ...

  10. html基础:js

    js是一种脚本语言.在html中起到操控行为的作用.在html中,html代码如果是一个人的话,那么js就是这个人的行为 js在html的head中被引用,也可以在body中被引用.引用方式用< ...