Realm:

  在实际应用中,shiro从数据库中获取安全数据(如用户、角色、权限),而不是从ini中,可作为安全数据源

  即SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法

  也需要从Realm中得到用户相应的角色/权限以确定用户是否能进行操作

org.apache.shiro.realm.Realm:   
  
   String getName(); //返回一个唯一的Realm名字
boolean supports(AuthenticationToken token); //判断此Realm是否支持此Token
AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException; //根据Token获取认证信息

  1 自定义Realm:

    一般实现AuthorizingRealm(授权)接口即可,此接口继承了AuthenticatingRealm(身份验证),也简介集成了CachingRealm(缓存)接口

    ini配置指定自定义Realm

[main]
#自定义realm
customRealm=com.roxy.shiro.realm.CustomRealm
#指定secrityManager的Realm实现
securityManager.realm=$customRealm

         realm:

public class CustomRealm extends AuthenticatingRealm{

    @Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 从数据库获取用户名和密码
String username = "draco";
String password = "615"; //从用户的输入中生成token,拿到用户名密码
String inputUsername = (String)token.getPrincipal();
if(!inputUsername.equals(username)){
throw new UnknownAccountException("用户不存在");
} /* if(status == 0){
throw new LockedAccountException("用户被锁定");
}*/ String inputPassword = (String)token.getCredentials();
if(!inputPassword.equals(password)){
throw new IncorrectCredentialsException("密码不正确");
} System.out.println(this.getName()); String realName = this.getName();
//拿到授权信息
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(inputUsername, inputPassword, realName); return info;
} }

    测试出现错误:

      错误信息提示类型转换错误,猜测username或者password的类型转换出错,但是日志已经将username打印出来,所以password转换出错

2017-10-14 20:24:34,680 WARN [org.apache.shiro.authc.AbstractAuthenticator] - 
  Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - draco, rememberMe=false].
  Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException).
java.lang.ClassCastException: [C cannot be cast to java.lang.String

      将代码改为:

        String inputPassword = new String((char[])token.getCredentials());

      再次测试:

2017-10-14 20:28:41,793 DEBUG [com.roxy.shiro.quickstart.Quickstart] - 密码错误
2017-10-14 20:28:41,794 DEBUG [com.roxy.shiro.quickstart.Quickstart] - 是否登陆成功:false

  2 多Realm配置:

[main]
#自定义realm
customRealm=com.roxy.shiro.realm.CustomRealm
customRealm2=com.roxy.shiro.realm.CustomRealm2
#指定secrityManager的Realm实现 (可选,若不指定,按照声明的顺序进行使用)
securityManager.realm=$customRealm,$customRealm2

   SecurityManager会按照Realms指定的顺序进行身份验证

   如果Realm没有被显示的指定,则会被忽略

Shiro Realm的更多相关文章

  1. 解决自定义Shiro.Realm扩展类不能用注解(@Resource或@Autowire)自动装配的问题

    问题产生原因:加载Realm时其他Spring配置文件(xml)尚未加载,导致注入失败. 解决方法:编写一个设置类把注入工作提前完成. package com.xkt.shiro import org ...

  2. org.apache.shiro.realm.AuthorizingRealm - No cache or cacheManager properties have been set. Authorization cache cannot be obtained.

    项目中用spring shiro来处理权限的问题,但是启动的时候会打印如下日志 org.apache.shiro.realm.AuthorizingRealm - No cache or cacheM ...

  3. Java-Shiro(五):Shiro Realm讲解(二)IniRealm的用法、JdbcRelam的用法、自定义Realm

    引入 上一篇在讲解Realm简介时,介绍过Realm包含大概4类缺省的Realm,本章主要讲解: 1)IniRealm的用法: 2)JdbcRealm基于mysql   默认表及查询语句实现认证.授权 ...

  4. shiro realm 注解失败问题解决过程

    做为一名在.net混了八九年的老兵油子,转战java时间并不长,刚开始做项目完全是凭借对C#的认识来做,虽然遇到一些问题,但实际结果显示C#在语言上和java还是有很大相似度,而且微软的MVC与Spr ...

  5. 第六章:shiro Realm相关对象

    Shiro 中的 AuthenticationToken AuthenticationToken 用于收集用户提交的身份(如用户名)及凭据(如密码).Shiro会调用CredentialsMatche ...

  6. 30、shiro框架入门2,关于Realm

    1.Jdbc的Realm链接,并且获取权限 首先创建shiro-jdbc.ini的配置文件,主要配置链接数据库的信息 配置文件中的内容如下所示 1.变量名=全限定类名会自动创建一个类实例 2.变量名. ...

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

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

  8. Shiro源码分析-初始化-Realm

    在上一篇介绍SecurityManager的初始化过程中,也有realm的粗略介绍. realm的概念在安全领域随处可见: 各种中间件的realm.spring security的realm.shir ...

  9. shiro(二)自定义realm,模拟数据库查询验证

    自定义一个realm类,实现realm接口 package com; import org.apache.shiro.authc.*; import org.apache.shiro.realm.Re ...

随机推荐

  1. prime docker-compose 环境运行试用

    prime 是一款基于graphql的开源cms,功能点很不错,但是出来不久,还是有好多bug的 官方暂时没有提供docker 的运行方式,为了方便测试,搞了以及docker-compose 测试的 ...

  2. 初等数论及其应用 (第6版) (Kenneth H.Rosen 著)

    第1章 整数 1.1 数和序列 1.2 和与积 1.3 数学归纳法 1.4 斐波那契数 1.5 整除性 第2章 整数的表示法和运算 2.1 整数的表示法 2.2 整数的计算机运算 2.3 整数运算的复 ...

  3. C# HttpClient请求Webapi帮助类

    引用 Newtonsoft.Json // Post请求 public string PostResponse(string url,string postData,out string status ...

  4. PowerDesigner ---- 数据库设计(物理模型CDM和概念模型PDM)

    前言 上一篇介绍了个PowerDesigner工具的安装和汉化,现在我就说一下怎么用这个PowerDesigner建数据库吧.   内容 第一种方法:概念模型转物理模型 1.首先新建模型--选择概念模 ...

  5. window.onload与$(document).ready()

    window.onload是原生JS事件,$(document).ready()是Jquery实现的与其作用类似的事件. 二者区别如下: 1.执行时间不同 $(document).ready()是DO ...

  6. Spring Cloud 与 Dubbo、Spring Cloud 与 Docker、Spring Cloud 与 Kubernetes 比较

    出处:http://dockone.io/article/4142

  7. docker 学习资料

    docker 学习资料 学习资料 网址 Docker 教程(菜鸟教程) http://www.runoob.com/docker/docker-tutorial.html

  8. MongoDB从3.0.6升级到MongoDB3.4.9

    最初进入公司的时候,有些爬虫数据需要存储在mongo数据库里面,当时看到最新的数据库版本是3.0.6,现在公司开展了新的项目需要使用到Mongo,使用到了Mongo的一些表关联查询的方法,但是只有在最 ...

  9. win7重装系统笔记

    制作U盘启动盘:链接 原文链接:链接 开机进入bios界面(华硕:F2) 按方向左右键进入:Boot选项,将“Lunch CSM”设置为“Enabled” 方向键选择“Security”,将“Secu ...

  10. Linux中chown和chmod的区别和用法

    转载自:http://www.cnblogs.com/EasonJim/p/6525242.html chmod修改第一列内容,chown修改第3.4列内容: chown用法: 用来更改某个目录或文件 ...