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. PHP之面试题总结

    总结一些面试题,有备无患,走起... 1.熟悉的 nosql 和 sql 有什么区别(优势,劣势) Memcache,Redis 都是内存数据库 redis是一个开源的支持多种数据类型的key=> ...

  2. vue组件如何引入外部.js/.css/.scss文件

    可在相应的单vue组件引入相应文件. 1.引入外部.js文件. 2.引入外部.css文件. 使用@import引入外部css,作用域是全局的,也可在相应的单vue组件引入,import并不是引入代码到 ...

  3. 依据系统语言、设备、url 重定向对应页面

    1. 思路 获取浏览器语言.页面名称.区分手机端与电脑 根据特定方式命名 html 文件,然后独立文件,重定向 eg: - root -  gap.html     gap -    index.ht ...

  4. docker inspect命令查看镜像详细信息

    使用 inspect 命令查看镜像详细信息,包括制作者.适应架构.各层的数字摘要等. # docker inspect --help Usage: docker inspect [OPTIONS] N ...

  5. Linux Shell交互式自动化运维程序

    Expect是Linux操作系统下的一个用来处理交互操作,系统默认是没有安装expect库,所以执行expect会提示找不到命令,需要手动安装,其它安装也比较简单,可以通过二进制源码包编译配置进行安装 ...

  6. [转载]一个支持Verilog的Vim插件——前言

    原文地址:一个支持Verilog的Vim插件--前言作者:hover 随着设计复杂度的增加,在书写代码中枯燥的重复性的劳动会越来越多.例如,例化若干个有上百个端口的子模块,这个工作没有任何创造性可言, ...

  7. linux 走三层内网添加静态路由

    /etc/sysconfig/network-scripts/ifcfg-eth1 #机器1ip route add 10.24.4.0/24 via 10.90.203.1 dev ens33/et ...

  8. dedecms织梦无法保存栏目内容的解决方法

    最近使用DedeCms5.3和DedeCms5.5遇到了一个不可思议的问题:在添加栏目时IE内核的浏览器无法保存栏目内容.到网上搜索了半天没找到解决方法,查看DedeCms官方搜索到的结果是“栏目内容 ...

  9. UVa 10047 自行车 状态记录广搜

    每个格子(x,y,drection,color) #include<iostream> #include<cstdio> #include<cstring> #in ...

  10. python openpyxl 简单使用

    1. 加载excel import openpyxl from openpyxl.utils import get_column_letter,column_index_from_string fro ...