Shiro入门学习---使用自定义Realm完成认证|练气中期
写在前面
在上一篇文章《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认证异常的分析,常用的有如下几种:
- 账户不正确(不存在)
- 密码错误
- 账户被锁定
- 密码过期
在实际项目中为了安全起见,账户不正确和密码错误统一返回“用户名或密码不正确”类似的的提示,避免造成账户泄露。
下面针对这种情况给予演示
用例代码
/**
* @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完成认证|练气中期的更多相关文章
- Shiro入门学习之自定义Realm实现认证(四)
一.概述 Shirom默认使用自带的IniRealm,IniRealm从ini配置文件中读取用户的信息,而大部分情况下需要从系统数据库中读取用户信息,所以需要实现自定义Realm,Realm接口如下: ...
- Shiro入门学习之自定义Realm实现授权(五)
一.自定义Realm授权 前提:认证通过,查看Realm接口的继承关系结构图如下,要想通过自定义的Realm实现授权,只需继承AuthorizingRealm并重写方法即可 二.实现过程 1.新建mo ...
- Shiro入门学习之shi.ini实现认证及源码分析(二)
一.Shiro.ini文件 1.文件说明 ①ini(InitializationFile)初始文件:Window系统文件扩展名 ②Shiro使用时可以连接数据库,也可以不连接数据库(可以使用shiro ...
- shiro入门学习--使用MD5和salt进行加密|练气后期
写在前面 在上一篇文章<Shiro入门学习---使用自定义Realm完成认证|练气中期>当中,我们学会了使用自定义Realm实现shiro数据源的切换,我们可以切换成从关系数据库如MySQ ...
- Shiro入门学习之shi.ini实现授权(三)
一.Shiro授权 前提:需要认证通过才会有授权一说 1.授权过程 2.相关方法说明 ①subject.hasRole("role1"):判断是否有该角色 ②subject.has ...
- Shiro入门学习与实战(一)
一.概述 1.Shiro是什么? Apache Shiro是java 的一个安全框架,主要提供:认证.授权.加密.会话管理.与Web集成.缓存等功能,其不依赖于Spring即可使用: Spring S ...
- Shiro入门学习之散列算法与凭证配置(六)
一.散列算法概述 散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如MD5.SHA等,一般进行散列时最好提供一个salt(“盐”),什么意思?举个栗子 ...
- shiro入门学习--授权(Authorization)|筑基初期
写在前面 经过前面的学习,我们了解了shiro中的认证流程,并且学会了如何通过自定义Realm实现应用程序的用户认证.在这篇文章当中,我们将学习shiro中的授权流程. 授权概述 这里的授权指的是授予 ...
- shiro框架学习-5-自定义Realm
1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...
随机推荐
- angular实现地区三级联动
<!DOCTYPE html><html ng-app="myapp"> <head> <meta charset="UTF-8 ...
- android studio 如何进行格式化代码 快捷键必备
在Eclipse中,我们一般使用Ctrl+Shift+F来格式化代码,Android Studio中需要换成: Reformat code CTRL + ALT + L (Win) OPTION + ...
- CA定义以及功能说明
当您访问以HTTPS开头的网站时,即表示正在使用CA.CA是Internet的重要组成部分.如果不存在CA,那么将无法安全在线购物以及使用网银在线业务等.什么是CA?CA具体是做什么的,又是如何确保您 ...
- 教你用OpenCV 和 Python给证件照换底色(蓝底 <->红底->白底)
在我们的生活中常常要用到各种底色要求的证件电子照,红底.蓝底.或者白底,而假如你手上只有一种底色的证件照,你又不想再去拍又不会PS怎么办?今天教你们用OpenCV和Python给你的证件照换底色. P ...
- basicInterpreter1.02 增加对for循环的支持
源码下载:https://files.cnblogs.com/files/heyang78/basicInterpreter102-20200531-2.rar 输入: for x= to print ...
- Cassandra-Java(增删查改)
驱动下载 创建maven工程,让maven来维护我们的jar,maven最重要的pom文件内容如下: <project xmlns="http://maven.apache.org/P ...
- Bypass windous/mac 登陆密码
前言 如题,在52破解里看到一个非常好用的工具 Kon-Boot 2.7 功能 不会去擦除windows密码 不会修改windows文件 此外,Kon-Boot的最新版是目前世界上唯一的一个能够绕过W ...
- SpringBoot整合MinIO
今天因为公司的需求接触到这个东西,我们先来看下MinIO的官网简介 MinIO 是一个基于Apache License v2.0开源协议的对象存储服务.它兼容亚马逊S3云存储服务接口,非常适合于存储大 ...
- jmeter连接websocket wss
注:域名的默认端口号是443 1.先添加 WebSocket Open Connection:连接请求 2.再添加 WebSocket request-response Sampler:发送数据请求 ...
- pytest测试框架 -- 简介
一.pytest测试框架简介: (1)pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效. (2)pytest框架可以兼容unittest用 ...