通过shiro教程1我们发现仅仅将数据源信息定义在ini文件中与我们实际开发环境有很大不兼容,所以我们希望能够自定义Realm。

自定义Realm的实现

创建自定义Realmjava类

创建一个java文件继承AuthorizingRealm类,重写两个抽象方法

/**
* 自定义的Realm
* @author dengp
*
*/
public class MyRealm extends AuthorizingRealm{ /**
* 认证方法
* @param token
* 就是我们在测试代码中 定义的UsernamePasswordToken对象
* 有我们保存的需要验证的账号密码信息
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// 获取账号信息
String principal = (String) token.getPrincipal();
// 正常逻辑此处应该根据账号去数据库中查询,此处我们默认账号为 root 密码123456
// 验证账号
if(!"root".equals(principal)){
// 账号错误
return null;
}
String pwd = "123456";
// 验证密码
AuthenticationInfo info = new SimpleAuthenticationInfo(principal, pwd,"myrealm");
return info;
} /**
* 授权方法
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO Auto-generated method stub
return null;
}
}
方法名 说明
doGetAuthenticationInfo 完成账号认证的方法
doGetAuthorizationInfo 完成用户授权的方法

配置ini.xml文件

[main]
#自定义 realm
customRealm=com.dpb.realm.MyRealm
#将realm设置到securityManager
securityManager.realms=$customRealm

测试

测试代码和上个案例一模一样

@Test
public void test() {
// 1.获取SecurityManager工厂对象
Factory<SecurityManager> factory =
new IniSecurityManagerFactory("classpath:shiro.ini");
// 2.通过Factory对象获取SecurityManager对象
SecurityManager securityManager = factory.getInstance();
// 3.将SecurityManager对象添加到当前运行环境中
SecurityUtils.setSecurityManager(securityManager); // 4.获取Subject对象
Subject subject = SecurityUtils.getSubject();
AuthenticationToken token = new UsernamePasswordToken("root1", "12345");
// 登录操作
try {
subject.login(token);
} catch (UnknownAccountException e) {
System.out.println("账号出错...");
} catch(IncorrectCredentialsException e){
System.out.println("密码出错...");
}
// 获取登录的状态
System.out.println(subject.isAuthenticated());
}

原理分析

为什么要继承AuthorizingRealm?

上个教程中我们完整的分析了认证的流程

我们发现在认证的过程核心代码是此:



核心方法是doGetAuthenticationInfo(token)

在Realm的结构中

AuthorizingRealm和AuthenticatingRealm都提供的有doGetAuthenticationInfo(token)的抽象方法。

但是AuthenticatingRealm中要重写的抽象方法太多

而AuthorizingRealm只需要重写两个方法,且这两个方法都是我们需要使用的。故选择继承AuthorizingRealm

自定义的Realm什么时候被调用的?

密码验证什么时候执行的?

注意:自定义Realm中只完成了账号的认证。密码认证还是在AuthenticatingRealm中完成的,只是我们在自定义Realm中完成了密码的设置。



shiro教程2(自定义Realm)的更多相关文章

  1. shiro授权及自定义realm授权(七)

    1.授权流程

  2. Shiro入门 - 通过自定义Realm连数数据库进行授权

    shiro-realm.ini [main] #自定义Realm myRealm=test.shiro.MyRealm #将myRealm设置到securityManager,相当于Spring中的注 ...

  3. Shiro入门 - 通过自定义Realm连数数据库进行认证(md5+salt形式)

    shiro-realm-md5.ini [main] #定义凭证匹配器 credentialsMatcher=org.apache.shiro.authc.credential.HashedCrede ...

  4. Shiro入门 - 通过自定义Realm连数数据库进行认证

    添加shiro-realm.ini文件 [main] #自定义Realm myRealm=test.shiro.MyRealm #将myRealm设置到securityManager,相当于Sprin ...

  5. 【Shiro】Apache Shiro架构之自定义realm

    [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shiro]Apache S ...

  6. 当shiro不进入自定义realm的权限认证方法时

    需要加入下面的一个bean @Bean public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator(){ DefaultAdvisorA ...

  7. 权限框架 - shiro 自定义realm

    上篇文章中是使用的默认realm来实现的简单登录,这仅仅只是个demo,真正项目中使用肯定是需要连接数据库的 首先创建自定义realm文件,如下: 在shiro中注入自定义realm的完全限定类名: ...

  8. shiro自定义realm支持MD5算法认证(六)

    1.1     散列算法 通常需要对密码 进行散列,常用的有md5.sha, 对md5密码,如果知道散列后的值可以通过穷举算法,得到md5密码对应的明文. 建议对md5进行散列时加salt(盐),进行 ...

  9. shiro自定义realm认证(五)

    上一节介绍了realm的作用: realm:需要根据token中的身份信息去查询数据库(入门程序使用ini配置文件),如果查到用户返回认证信息,如果查询不到返回null.token就相当于是对用户输入 ...

随机推荐

  1. python基础之Day10

    一.函数的返回值 1.什么是返回值返回值是一个函数的处理结果, 2.为什么要有返回值如果我们需要在程序中拿到函数的处理结果做进一步的处理,则需要函数必须有返回值 3.函数的返回值的应用函数的返回值用r ...

  2. fine安装教程

    先关掉lmgrd 相关的两个进程 -关闭当前license server 1. Login root 2. 执行 FineTurbo11.1_linux 文件夹内 ./install_numeca 3 ...

  3. unbuntu 安装 teamviewer

    下载 teamviewer 安装包 使用 dpkg 安装 deb 安装包 使用 sudo apt-get install -f 解决依赖问题

  4. IJ配置项目的TOMCAT

    参考文档: IJ里配置TOMCAT http://jingyan.baidu.com/album/0a52e3f43d9f69bf62ed72f9.html?picindex=11 源发行版1.8 需 ...

  5. 339. Nested List Weight Sum

    https://leetcode.com/problems/nested-list-weight-sum/description/ Given a nested list of integers, r ...

  6. mui.init()和mui.plusReady()

    1.每个用到mui的页面都调用下mui.init.2.如果需要使用大H5+对象,就写到plusReady中,如plus对象. HTML5+扩展api是针对手机APP的,只有才手机应用(比如手机浏览器) ...

  7. jQuery的事件,动画效果等

    一.事件 click(function(){}) 点击事件 hover(function(){})   悬浮事件,这是jQuery封装的,js没有不能绑定事件 focus(function(){}) ...

  8. vue报错TypeError: Cannot read property '$createElement' of undefined

    报错截图: 这个错误就是路由上的component写成了components

  9. scala字符串前加s使用$

    https://my.oschina.net/u/2000675/blog/1592140 字符串中的变量替换,Scala中基础的字符串插值就是在字符串前加字幕‘s’,然后在字符串中放入变量,每个变量 ...

  10. 普通java程序,maven打包

    pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...