【Shiro】06 自定义Realm授权实现
创建一个激活的用户类:
public class ActiverUser {
private User user;
private List<String> roleList;
private List<String> permitList;
} // 省略SETTER & GETTER & CONSTRUCTOR ...
对应的创建业务层:
用户业务:
public interface UserService {
User queryUserByUserName(String username);
}
实现类:
这个Switch迷一样不支持String,不管了
public class UserServiceImpl implements UserService {
public User queryUserByUserName(String username) {
User user = null;
switch (username) {
case "zhangsan":
user=new User(1, "zhangsan", "123456", new Date());
break;
case "lisi":
user=new User(2, "lisi", "123456", new Date());
break;
case "wangwu":
user=new User(3, "wangwu", "123456", new Date());
break;
}
return user;
}
}
角色业务:
根据用户名查询对应的所有角色:
public interface RoleService {
List<String> queryRoleByUsername(String username);
}
实现类:
public class RoleServiceImpl implements RoleService {
public List<String> queryRoleByUsername(String username) {
return Arrays.asList("role1", "role2", "role3");
}
}
权限业务:
public interface PermissionService {
List<String> queryPermissionByUsername(String username);
}
实现类:
public class PermissionServiceImpl implements PermissionService{
public List<String> queryPermissionByUsername(String username) {
return Arrays.asList("user:query", "user:insert", "user:update", "user:delete");
}
}
UserRealm的修改:
package cn.echo42.shiro; import cn.echo42.pojo.User;
import cn.echo42.service.*;
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.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; import java.util.Collection;
import java.util.List; /**
* @author DaiZhiZhou
* @file Shiro
* @create 2020-08-01 18:38
*/
public class UserRealm extends AuthorizingRealm { private UserService userService = new UserServiceImpl();
private RoleService roleService = new RoleServiceImpl();
private PermissionService permissionService = new PermissionServiceImpl(); protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
ActiverUser activerUser = (ActiverUser) principalCollection.getPrimaryPrincipal();
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); //添加角色
Collection<String> roles = activerUser.getRoleList();
// 角色存在
if(null != roles && roles.size() > 0) {
info.addRoles(roles);
} Collection<String> permissions = activerUser.getPermitList();
//添加权限
if(null != permissions && permissions.size() > 0) {
info.addStringPermissions(permissions);
}
// if(activerUser.getUser().getType()==0) {
// info.addStringPermission("*:*");
// }
return info;
} protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { // 通过提交的令牌获取信息(用户名称?)
String principal = authenticationToken.getPrincipal().toString(); // 查询数据库得到记录
User user = userService.queryUserByUserName(principal); // user非空
if(null != user) {
// 装载用户到权限的所有信息
List<String> roles = roleService.queryRoleByUsername(user.getUsername());
List<String> permissions = permissionService.queryPermissionByUsername(user.getUsername());
ActiverUser activerUser = new ActiverUser(user, roles, permissions); // 返回认证信息出去
return new SimpleAuthenticationInfo(activerUser, authenticationToken.getCredentials(), this.getName());
}
return null;
}
}
【Shiro】06 自定义Realm授权实现的更多相关文章
- shiro授权及自定义realm授权(七)
1.授权流程
- shiro中自定义realm实现md5散列算法加密的模拟
shiro中自定义realm实现md5散列算法加密的模拟.首先:我这里是做了一下shiro 自定义realm散列模拟,并没有真正链接数据库,因为那样东西就更多了,相信学到shiro的人对连接数据库的一 ...
- shiro之自定义realm
Shiro认证过程 创建SecurityManager--->主体提交认证--->SecurityManager认证--->Authenticsto认证--->Realm验证 ...
- Shiro -- (三) 自定义Realm
简介: Realm:域,Shiro 从从 Realm 获取安全数据(如用户.角色.权限),就是说 SecurityManager 要验证用户身份,那么它需要从 Realm 获取相应的用户进行比较以确定 ...
- shiro(二)自定义realm,模拟数据库查询验证
自定义一个realm类,实现realm接口 package com; import org.apache.shiro.authc.*; import org.apache.shiro.realm.Re ...
- 6、Shiro之自定义realm
1.创建一个包存放我们自定义的realm文件: 创建一个类名为CustomRealm继承AuthorizingRealm并实现父类AuthorizingRealm的方法,最后重写: CustomRea ...
- 使用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授权 前提:认证通过,查看Realm接口的继承关系结构图如下,要想通过自定义的Realm实现授权,只需继承AuthorizingRealm并重写方法即可 二.实现过程 1.新建mo ...
- Shiro入门 - 通过自定义Realm连数数据库进行授权
shiro-realm.ini [main] #自定义Realm myRealm=test.shiro.MyRealm #将myRealm设置到securityManager,相当于Spring中的注 ...
随机推荐
- INFINI Labs 产品更新 | Easysearch 1.8.2 发布优化 CCR 性能
INFINI Labs 产品又更新啦~,包括 Easysearch v1.8.0.Gateway.Console.Agent.Loadgen v1.25.0.本次各产品更新了很多亮点功能,如 Easy ...
- Future集合会等线程池执行完才开始遍历吗?
先说结论:Future集合并不是等线程池执行完才开始遍历,而是线程池内的线程执行完一条Future集合就立即遍历一条 在使用线程池的业务场景下,我们经常需要获取线程执行的返回值,此时我们需要Calla ...
- elasticSearch RangeQuery范围查询from to的理解
elasticSearch RangeQuery范围查询from to的理解 Elasticsearch Guide 选择版本号来查询对应的文档内容:https://www.elastic.co/gu ...
- org.springframework.beans.BeanUtils.copyProperties对象赋值的常见问题
org.springframework.beans.BeanUtils.copyProperties对象赋值的常见问题 //codepublic static void copyProperties( ...
- JS常用的工具方法
记录一些经常使用的JS通用工具方法,代码来自互联网,佛性更新 空字符串校验 /** * 判断字符串是不是NULL或空串或空格组成 * @param str 被判断的字符串 * @return {boo ...
- 记一次 MatPlotLib 报错及解决方案:AttributeError: module 'matplotlib.cbook' has no attribute '_Stack'
记一次 MatPlotLib 报错及解决方案 目录 记一次 MatPlotLib 报错及解决方案 解决方法 具体发生了什么 报错 log 1 内容 报错 log 2 内容 最近出现 MatPlotLi ...
- Linux下挂载NTFS格式的U盘
NTFS是Windows下的格式,在Linux下是识别不了的,要想在Linux上挂载NTFS格式的U盘需要安装软件以提供支持.软件名为ntfs-3g. 1.下载安装包 https://tuxera.c ...
- 记录EF 排序配上自定义的比较器
记录EF 排序配上自定义的比较器 前言 要求页面文件显示的时候能够按照序号去排序要求如下: 数据库有一个列存放文件名,如: 1.1文件 1.2文件 1.1.1文件 1.1.11文件1.0.txt 1. ...
- dot net core使用BackgroundService运行一个后台服务
不管是在控制台程序还是asp.net core程序中,我们经常会有用到一个需要长时间运行的后台任务的需求.通常最直觉的方式是使用Thread实例来新建一个线程,但是这样需要自行管理线程的启动和停止. ...
- BootStrap Table 添加序列号
js $('#table').bootstrapTable({ striped: true,//隔行换色 columns: [ { field: '', title: '序号', sortable: ...