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. 如何在Ubuntu上在多个PHP版本之间切换 (for swoole)

    摘要: 之前一直用Php7.0,今天想用7.2试下一些特性,安装完之后,切换回7.0却不能再使用7.0的swoole了,原来是切换方式出现了问题 一 从PHP 7.0 切换到 PHP 7.2 Apac ...

  2. Python基础——函数进阶

    等待更新…………………… 后面再写

  3. 【Java】 ArrayList和LinkedList实现(简单手写)以及分析它们的区别

    一.手写ArrayList public class ArrayList { private Object[] elementData; //底层数组 private int size; //数组大小 ...

  4. redis常见问题和解决方案

    转载:https://www.cnblogs.com/aspirant/p/6820262.html [原创]那些年用过的Redis集群架构(含面试解析) redis常见问题和解决方案 持久化.主从问 ...

  5. C# wpf 使用资源文件 resx

    随意新建一个wpf应用 在cs代码编辑,增加 using System.Resources; 放在最上 在方案新建文件夹 名 文件 在 文件 新建资源文件 资源.resx 资源.resx 添加字符串 ...

  6. CCPC-Wannafly Winter Camp Day8 (Div2, onsite) 补题

    A Aqours 题解: https://www.cnblogs.com/qieqiemin/p/11251645.html D:吉良吉影的奇妙计划 (暴力打表) 题目描述 吉良吉影是一个平凡的上班族 ...

  7. robots.txt防止向黑客泄露网站的后台和隐私

    为了不让搜索引擎索引网站的后台页面或其它隐私页面,我们将这些路径在robots.txt文件中禁用了.但矛盾的是,robots.txt文件任何人都可以访问,包括黑客.为了禁止搜索引擎,我们把隐私泄露给了 ...

  8. 新建ext4分区及开机挂载

    1.查看新的20G硬盘是否已经挂在完毕. 2.使用fdisk命令创建主分区 3.再将分区设置完毕之后,查看磁盘分区是否创建完成. 2.使用mkfs.ext4命令将新创建的分区进行格式化为: 1)blo ...

  9. ListView的创建

    里面涉及到很多知识 包括3D样式的去除,重绘ListView控件,以及处理控件的边框颜色 // Test_listview_1.cpp : Defines the entry point for th ...

  10. Vue的css动画原理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...