6、Shiro之自定义realm
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的更多相关文章
- shiro中自定义realm实现md5散列算法加密的模拟
shiro中自定义realm实现md5散列算法加密的模拟.首先:我这里是做了一下shiro 自定义realm散列模拟,并没有真正链接数据库,因为那样东西就更多了,相信学到shiro的人对连接数据库的一 ...
- shiro(二)自定义realm,模拟数据库查询验证
自定义一个realm类,实现realm接口 package com; import org.apache.shiro.authc.*; import org.apache.shiro.realm.Re ...
- shiro之自定义realm
Shiro认证过程 创建SecurityManager--->主体提交认证--->SecurityManager认证--->Authenticsto认证--->Realm验证 ...
- Shiro -- (三) 自定义Realm
简介: Realm:域,Shiro 从从 Realm 获取安全数据(如用户.角色.权限),就是说 SecurityManager 要验证用户身份,那么它需要从 Realm 获取相应的用户进行比较以确定 ...
- 使用Spring配置shiro时,自定义Realm中属性无法使用注解注入解决办法
先来看问题 纠结了几个小时终于找到了问题所在,因为shiro的realm属于Filter,简单说就是初始化realm时,spring还未加载相关业务Bean,那么解决办法就是将springmvc ...
- (十)shiro之自定义Realm以及自定义Realm在web的应用demo
数据库设计 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...
- 权限框架 - shiro 自定义realm
上篇文章中是使用的默认realm来实现的简单登录,这仅仅只是个demo,真正项目中使用肯定是需要连接数据库的 首先创建自定义realm文件,如下: 在shiro中注入自定义realm的完全限定类名: ...
- Shiro第二篇【介绍Shiro、认证流程、自定义realm、自定义realm支持md5】
什么是Shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. spring中有spring security (原名Acegi),是一个权限框架,它和sp ...
- shiro自定义Realm
1.1 自定义Realm 上边的程序使用的是shiro自带的IniRealm,IniRealm从ini配置文件中读取用户的信息,大部分情况下需要从系统的数据库中读取用户信息,所以需要自定义realm. ...
随机推荐
- js 学习三 Array
1.数组的长度 var sequence = [1, 1, 2, 3, 5, 8, 13]; sequence .length //7 2.字符串转换成数组 string.split() var my ...
- 万兴神剪手 Wondershare Filmora v9.2.11.6 简体中文版
目录 1. 介绍 2. 简体中文9.2.1.10汉化版下载 3. 安装和激活说明 1. 介绍 万兴神剪手 Filmora 是一款界面简洁时尚.功能强大的视频编辑软件,它是深圳万兴科技公司近年来的代表作 ...
- mariadb增删改查
数据库用户的操作 登录前需先启动3306端口. 首次启动需初始化数据库 mysql_secure_installation 增/改: 创建用户及赋予用户指定权限 grant 权限(分为create[创 ...
- 文件I/O编程 (fcntl)
Fcntl函数语法要点所需头文件:#include #include #include函数原型:int fcntl(int fd,cmd,struct flock ...
- 【转】rsa公钥和私钥的生成
转:https://www.cnblogs.com/zengsf/p/10136886.html 在liunx环境中 openssl 然后生成私钥: genrsa -out app_private_k ...
- JDK12的安装搭建
JDK12的安装搭建 一.JDK下载 1.JDK官网下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk12-down ...
- zencart 输出产品特价折扣百分比
通过调用zen_get_products_base_price($products_id)获取原价,zen_get_products_special_price($products_id)获取特价,进 ...
- Xunsearch入门
Xunsearch入门 简介: 开源免费.高性能.多功能.简单易用的专业全文检索技术方案. 1.Xunsearch安装: (1)官网(http://xunsearch.com)下载 wget http ...
- 【POJ2152】Fire
题目大意:给定一棵 N 个节点的无根树,点有点权,边有边权,现需要选出一个点集,满足树上任意一个点到该点集的距离不超过该点的给定值,求选出点集点权的最小值是多少. 题解:可以发现,对于以 i 为根的子 ...
- php基于websocket实现的在线聊天室
听说websocket技术可以实现 1.