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. day5 python学习

    本日主要讲了列表的一些操作 1.列表的增 ret=['wang','taibai','alxe','ritian'] ret.append("wusir") print(ret) ...

  2. linux之 sed命令

    1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区 ...

  3. TypeScript 之 模块

    https://m.runoob.com/manual/gitbook/TypeScript/_book/doc/handbook/Modules.html 外部模块简写 外部模块简写:declare ...

  4. CentOS6.x 下安装Python pyyaml模块

    Python中使用yaml时候,需要安装下组件pyyaml,直接安装pyyaml时提示“ext/_yaml.c:4:20: error: Python.h: No such file or direc ...

  5. MATLAB:SMPD无法启动

    可以键入 distcomp.feature( 'LocalUseMpiexec', false )命令运行后,然后再启动程序.

  6. 关键字:this、ref、out

    Class1.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...

  7. Hanlp在java中文分词中的使用介绍

    项目结构 该项目中,.jar和data文件夹和.properties需要从官网/github下载,data文件夹下载 项目配置 修改hanlp.properties: 1 #/Test/src/han ...

  8. Vivado中ILA的使用

    Vivado中ILA的使用 1.编写RTL代码      其中需要说明的是(* keep = "TRUE" *)语句的意识是保持cnt信号不被综合掉,方便以后的调试,是否可以理解为 ...

  9. 关于java的一些小知识(课程作业01)

    1,在java源代码中加空格注释不会影响程序的编译速度. 2,一个java类文件中真的只能有一个公有类吗? 如果只在第一个public类里面定义实体,或者两个都不定义并没有报错且程序可以运行.每个编译 ...

  10. C# .NET 2.0 判断当前程序进程是否为64位运行时 (x64)

    public static bool Is64BitProcess() { ; }