1、创建一个包存放我们自定义的realm文件:

创建一个类名为CustomRealm继承AuthorizingRealm并实现父类AuthorizingRealm的方法,最后重写:

CustomRealm代码:

package com.shiro.myrealm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set; public class CustomRealm extends AuthorizingRealm {
//认证方法
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
/**
* 重写认证方法
*/
//1、从主体传过来的认证信息中获取用户名
String username = (String) authenticationToken.getPrincipal();
//2、通过用户名到数据库获取凭证
String password = getPassWordByUsername(username);
if (password == null) {
return null;
}
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("miyue", password, "test");
return simpleAuthenticationInfo;
} //授权方法
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
/**
* 重新授权方法
*/
String username = (String) principalCollection.getPrimaryPrincipal();
//从角色和缓存中获取角色数据
Set<String> roles = getRolesByUsername(username);
//从角色和缓存中获取权限数据
Set<String> permission = getPermissionsByUsername(username);
SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo();
simpleAuthorizationInfo.setRoles(roles);
simpleAuthorizationInfo.setStringPermissions(permission);
return simpleAuthorizationInfo;
} //下面使用map,set模拟数据库数据返回
Map<String, String> map = new HashMap<String, String>(); {
map.put("miyue", "houru");
} private String getPassWordByUsername(String username) {
return map.get(username) == null ? null : map.get(username);
} private Set<String> getRolesByUsername(String username) {
Set<String> set = new HashSet<>();
set.add("admin");
set.add("user");
return set;
} private Set<String> getPermissionsByUsername(String username) {
Set<String> set = new HashSet<>();
set.add("user:delete");
set.add("user:add");
return set;
} }

新建一个测试类MyrealmTest,测试我们自定义的realm:

package com.shiro.shiroframe;

import com.shiro.myrealm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.jupiter.api.Test; public class MyrealmTest {
//引入我们自定义的realm
CustomRealm customRealm = new CustomRealm(); @Test
public void MyrealmTest() { DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(customRealm);
SecurityUtils.setSecurityManager(defaultSecurityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("miyue", "houru");
subject.login(usernamePasswordToken);
System.err.println(subject.isAuthenticated());
subject.checkRoles("admin");
subject.checkPermission("user:add");
} }

上面测试类,验证通过,则控制台不报错,否则报错;

6、Shiro之自定义realm的更多相关文章

  1. shiro中自定义realm实现md5散列算法加密的模拟

    shiro中自定义realm实现md5散列算法加密的模拟.首先:我这里是做了一下shiro 自定义realm散列模拟,并没有真正链接数据库,因为那样东西就更多了,相信学到shiro的人对连接数据库的一 ...

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

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

  3. shiro之自定义realm

    Shiro认证过程 创建SecurityManager--->主体提交认证--->SecurityManager认证--->Authenticsto认证--->Realm验证 ...

  4. Shiro -- (三) 自定义Realm

    简介: Realm:域,Shiro 从从 Realm 获取安全数据(如用户.角色.权限),就是说 SecurityManager 要验证用户身份,那么它需要从 Realm 获取相应的用户进行比较以确定 ...

  5. 使用Spring配置shiro时,自定义Realm中属性无法使用注解注入解决办法

    先来看问题    纠结了几个小时终于找到了问题所在,因为shiro的realm属于Filter,简单说就是初始化realm时,spring还未加载相关业务Bean,那么解决办法就是将springmvc ...

  6. (十)shiro之自定义Realm以及自定义Realm在web的应用demo

    数据库设计 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...

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

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

  8. Shiro第二篇【介绍Shiro、认证流程、自定义realm、自定义realm支持md5】

    什么是Shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和sp ...

  9. shiro自定义Realm

    1.1 自定义Realm 上边的程序使用的是shiro自带的IniRealm,IniRealm从ini配置文件中读取用户的信息,大部分情况下需要从系统的数据库中读取用户信息,所以需要自定义realm. ...

随机推荐

  1. N4复习考试总结

    一つ(ひとつ) 半分(はんぶん) 煙草(たばこ)を吸う(すう) 玄関(げんかん) ナイフ(刀)     財布(さいふ) 浅い(あさい) 薄い(うすい) 牛乳(ぎゅうにゅう) 皿(さら) 七日(なのか) ...

  2. mariadb读写分离

    mycat maraidb主从架构,是主负责写,从负责读,但前端如果没有调度器的话,是无法实现读写分离的.这就涉及到了中间站,它就是mycat.一定要在主从架构的基础之上实现读写分离. 配置三台的主从 ...

  3. 新手的Linux zcat命令示例

    Zcat是一个命令行实用程序,用于查看压缩文件的内容.它将压缩文件扩展为标准输出,允许您查看内容. 分类:Linux命令操作系统 2018-08-13 00:00:00 通常,使用gzip压缩的文件可 ...

  4. Activity安装与使用

    详细安装步骤:http://blog.csdn.net/oyzl68/article/details/16817963

  5. zlib的压缩与解压

    http://zlibnet.codeplex.com/releases/view/629717 using ZLibNet; string str = "ccc"; byte [ ...

  6. JAVA并发编程的艺术 Java并发容器和框架

    ConcurrentHashMap ConcurrentHashMap是由Segment数组结构和HashEntry数组结构组成. 一个ConcurrentHashMap里包含一个Segment数组, ...

  7. Kendo UI使用教程:入门指南

    [Kendo UI最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support for React和 ...

  8. springboot返回html和jsp

    一.返回html (1)添加maven依赖 <dependency>    <groupId>org.springframework.boot</groupId>  ...

  9. 《SaltStack技术入门与实践》—— Grains

    Grains 本章节参考<SaltStack技术入门与实践>,感谢该书作者: 刘继伟.沈灿.赵舜东 前几章我们已经了解SaltStack各个组件以及通过一个案例去熟悉它的各种应用,从这章开 ...

  10. php7魔术方法

    抽空把php7的魔术方法复习一下,大致如下 __constract:在实例化一个类时,触发 __destruct:在一个实例对象被销毁的时候触发 __set(string $name , mixed ...