Shiro配置跳过权限验证
需求
因为在开发环境,测试环境,有时候需要跳过shiro的权限验证.所以想写个简单的配置跳过shiro的权限验证.
跳过权限验证的原理就是重写**@RequiresPermissions**的实现,然后在配置文件中写一个开关,最后通过Aop注入进去就大功告成.
@RequiresPermissions 处理类
在 org.apache.shiro.authz.aop.PermissionAnnotationHandler 中处理这个注解,这就是我们要
覆写的类.我准备将它替换成log日志.
/**
* 检查是否有@{@link org.apache.shiro.authz.annotation。注释是
*声明,如果是,执行权限检查,看是否允许调用Subject
继续
*访问。
*
* @since 0.9.0
*/
public class PermissionAnnotationHandler extends AuthorizingAnnotationHandler {
/**
*确保调用Subject
具有注释指定的权限,如果没有,则抛出
* AuthorizingException
表示拒绝访问。
*
*/
public void assertAuthorized(Annotation a) throws AuthorizationException {
if (!(a instanceof RequiresPermissions)) return;
RequiresPermissions rpAnnotation = (RequiresPermissions) a;
//获取注解的值
String[] perms = getAnnotationValue(a);
//获取主体
Subject subject = getSubject();
//如果只有一个需要权限
if (perms.length == 1) {
subject.checkPermission(perms[0]);
return;
}
//与的处理
if (Logical.AND.equals(rpAnnotation.logical())) {
getSubject().checkPermissions(perms);
return;
}
//或的处理
if (Logical.OR.equals(rpAnnotation.logical())) {
// Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
boolean hasAtLeastOnePermission = false;
for (String permission : perms) if (getSubject().isPermitted(permission)) hasAtLeastOnePermission = true;
// Cause the exception if none of the role match, note that the exception message will be a bit misleading
if (!hasAtLeastOnePermission) getSubject().checkPermission(perms[0]);
}
}
}
通过 AopAllianceAnnotationsAuthorizingMethodInterceptor 加入拦截处理,通过Shiro starter 的 Conguration配置到将AuthorizationAttributeSourceAdvisor注入到 Spring Bean中.AuthorizationAttributeSourceAdvisor 实现了 StaticMethodMatcherPointcutAdvisor
破解
既然找到了实现的方法,那么注入一个自己实现类就可以跳过shiro的权限了.
但是为了只在测试和开发环境破解,需要使用配置来实现
1.配置跳过shiro开关
首先在spring的配置中加入 spring.profiles.active ,同时配置 xfs.shiro.skipShiro为true.
破解时根据当前运行环境和skipShiro来判断是否要跳过shiro
spring:
# 环境 dev|test|prod
profiles:
active: dev
application:
name: system
xfs:
shiro:
skipShiro: true #危险配置,跳过shiro权限控制,用于开发和测试环境调试,慎用
2.重写自己的@RequiresPermissions处理方法
在日志上打log,防止严重的生产问题
package cn.lanhi.auth.starter.interceptor;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.aop.PermissionAnnotationHandler;
import java.lang.annotation.Annotation;
/**
*
*
* @author : snx cn.shennaixin@gmail.net
* @date : 2020-06-16 11:39
*/
@Slf4j
public class ShiroPermissionHandler extends PermissionAnnotationHandler {
public ShiroPermissionHandler() {
super();
log.warn("使用了自定义的PermissionHandler,如果是生产环境,使用这个类将会导致权限控制模块失效");
}
/**
* 重写权限认证方法,仅仅打印log,不做拦截处理
*
* @param a 注解
* @throws AuthorizationException 一个不可能抛出的异常
*/
@Override
public void assertAuthorized(Annotation a) throws AuthorizationException {
if (!(a instanceof RequiresPermissions)) return;
//如果是数组,打印效果不好,使用json序列化
log.warn("警告!! 跳过了权限:{}", JSON.toJSONString(getAnnotationValue(a)));
}
}
3.设置注解处理器
在 AnnotationsAuthorizingMethodInterceptor 这个抽象类的实现类中,添加了对注解的拦截器
package org.apache.shiro.spring.security.interceptor;
public class AopAllianceAnnotationsAuthorizingMethodInterceptor
extends AnnotationsAuthorizingMethodInterceptor implements MethodInterceptor {
//Shiro拦截器
public AopAllianceAnnotationsAuthorizingMethodInterceptor() {
List<AuthorizingAnnotationMethodInterceptor> interceptors =
new ArrayList<AuthorizingAnnotationMethodInterceptor>(5);
AnnotationResolver resolver = new SpringAnnotationResolver();
interceptors.add(new RoleAnnotationMethodInterceptor(resolver));
//注入了我们要破解的权限控制拦截器,
interceptors.add(new PermissionAnnotationMethodInterceptor(resolver));
interceptors.add(new AuthenticatedAnnotationMethodInterceptor(resolver));
interceptors.add(new UserAnnotationMethodInterceptor(resolver));
interceptors.add(new GuestAnnotationMethodInterceptor(resolver));
setMethodInterceptors(interceptors);
}
........
}
那么破解一下,自己继承一下AopAllianceAnnotationsAuthorizingMethodInterceptor,然后获取自身属性,修改值
package cn.lanhi.auth.starter.shiro;
import com.xfs.auth.starter.interceptor.ShiroPermissionHandler;
import org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor;
import org.apache.shiro.authz.aop.PermissionAnnotationMethodInterceptor;
import org.apache.shiro.spring.security.interceptor.AopAllianceAnnotationsAuthorizingMethodInterceptor;
import java.util.List;
import java.util.stream.Collectors;
/**
*
shiro 权限重定义
*
* @author : snx cn.shennaixin@gmail.net
* @date : 2020-06-16 11:34
*/
public class ShiroMethodInterceptor extends AopAllianceAnnotationsAuthorizingMethodInterceptor {
public ShiroMethodInterceptor() {
super();
}
/**
* 跳过shiro RequirePremissions 注解验证
*/
public ShiroMethodInterceptor skipPremissionHandler() {
List<AuthorizingAnnotationMethodInterceptor> interceptors = this.getMethodInterceptors().stream()
.filter(authorizingAnnotationMethodInterceptor ->
!(authorizingAnnotationMethodInterceptor instanceof PermissionAnnotationMethodInterceptor))
.collect(Collectors.toList());
PermissionAnnotationMethodInterceptor interceptor = new PermissionAnnotationMethodInterceptor();
//设置成自己的注解处理器!
interceptor.setHandler(new ShiroPermissionHandler());
interceptors.add(interceptor);
setMethodInterceptors(interceptors);
return this;
}
}
4.重写shiroAop
package org.apache.shiro.spring.config;
/**
* shiro AOP
* @since 1.4.0
*/
@Configuration
public class ShiroAnnotationProcessorConfiguration extends AbstractShiroAnnotationProcessorConfiguration{
@Bean
@DependsOn("lifecycleBeanPostProcessor")
protected DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
return super.defaultAdvisorAutoProxyCreator();
}
@Bean
protected AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
return super.authorizationAttributeSourceAdvisor(securityManager);
}
}
--------------------
--------------------
--------------------
这个 AuthorizationAttributeSourceAdvisor 提供了AOP的拦截器实现.接下来我们要覆盖他,
/**
* Create a new AuthorizationAttributeSourceAdvisor.
*/
public AuthorizationAttributeSourceAdvisor() {
setAdvice(new AopAllianceAnnotationsAuthorizingMethodInterceptor());
}
--------------------
--------------------
--------------------
5.覆盖Shiro Bean
判断了一下环境,跳过Shiro 权限验证,仅在测试和开发环境生效,且需要开启配置
注意bean要设置成@primary
/**
* 跳过Shiro 权限验证,仅在测试和开发环境生效
*
* @param securityManager
* @return
*/
@Bean("authorizationAttributeSourceAdvisor")
@Primary
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
SecurityManager securityManager,
Environment environment,
@Value("${xfs.shiro.skipShiro:false}") boolean skipShiro) {
//获取当前运行环境
String profile = environment.getRequiredProperty("spring.profiles.active");
//创建要生成的Bean
AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
advisor.setSecurityManager(securityManager);
//判断是否可以跳过shiro
if (skipShiro && ("dev".equals(profile) || "test".equals(profile))) {
//运行跳过shiro权限验证方法
advisor.setAdvice(new ShiroMethodInterceptor().skipPremissionHandler());
}
return advisor;
}
到此为止,就大功告成啦.
Shiro配置跳过权限验证的更多相关文章
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(二)基于SpringMVC+Shiro的用户登录权限验证
序: 在上一篇中,咱们已经对于项目已经做了基本的配置,这一篇文章开始学习Shiro如何对登录进行验证. 教学: 一.Shiro配置的简要说明. 有心人可能注意到了,在上一章的applicationCo ...
- JavaEE权限管理系统的搭建(六)--------使用拦截器实现菜单URL的跳转权限验证和页面的三级菜单权限按钮显示
本小结讲解,点击菜单进行页面跳转,看下图,点击管理员列表后会被认证拦截器首先拦截,验证用户是否登录,如果登录就放行,紧接着会被权限验证拦截器再次拦截,拦截的时候,会根据URL地址上找到对应的方法,然后 ...
- 将 Shiro 作为应用的权限基础 二:shiro 认证
认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的“实体/凭证”组合便是“用户名/密码”组合. 一. ...
- 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)
前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...
- shiro配置unauthorizedUrl,无权限抛出无权限异常,但是不跳转
在使用shiro配置无授权信息的url的时候,发现这样的一个scenario,配置好unauthorizedUrl后仍然无法跳转,然后就在网上开始找,找了原因以及解决方案 原因,先post一个源码: ...
- Shiro权限验证代码记录,正确找到shiro框架在什么地方做了权限识别
权限验证方式的验证代码: org.apache.shiro.web.servlet.AdviceFilter这个类是所有shiro框架提供的默认权限验证实例类的父类 验证代码: public void ...
- 自定义shiro实现权限验证方法isAccessAllowed
由于Shiro filterChainDefinitions中 roles默认是and, admin= user,roles[system,general] 比如:roles[system,gener ...
- 项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示
1 课程计划 1. 常见权限控制方式 2. 基于shiro提供url拦截方式验证权限 3. 在realm中授权 4. 基于shiro提供注解方式验证权限 5. 总结验证权限方式(四种) 6. 用户注销 ...
- Spring+shiro配置JSP权限标签+角色标签+缓存
Spring+shiro,让shiro管理所有权限,特别是实现jsp页面中的权限点标签,每次打开页面需要读取数据库看权限,这样的方式对数据库压力太大,使用缓存就能极大减少数据库访问量. 下面记录下sh ...
- shiro登陆权限验证
一>引入shirojar包 <!-- shiro登陆权限控制 --> <dependency> <groupId>org. ...
随机推荐
- 墨天轮最受DBA欢迎的数据库技术文档-监控篇
好久不见,<墨天轮最受欢迎的技术文档>系列文章回归啦!本期主题数据库监控篇,希望能够帮助到大家!此外,为感谢大家支持,原文文末也给大家带来了返场福利,欢迎大家进入原文参与~ 数据库监控是许 ...
- style 标签写在body 前后的区别?
知识储备:了解浏览器渲染页面的流程 a)首先 , 解析(parse)html 标签 , 获取DOM 树 b)解析html 的同时 , 解析css , 获得样式规则 (style rules) CSS ...
- idea创建搭建项目 maven eg
1. 创建一个空的项目 ps:作为 git 管理 ,父项目 2. 创建第一个微服务 先导入两个必要的组件 web spring web : spring cloud openfeign (用于微服务之 ...
- 4:Exchange安装后的任务
4:Exchange安装后的任务 安装后的任务: 第一:证书的申请安装,分配服务略 注意项:如果是通配符证书,不能直接分配pop的服务 第二:虚拟目录的配置 Exchang ...
- PCI-5565反射内存卡
PCI-5565反射内存卡是一种用于实时网络的硬件设备.它基于反射内存网的原理,通过光纤连接多台计算机,形成网络节点,并且每个节点上的网络内存卡存储着其他节点的共享数据拷贝.该反射内存卡可以插在多种总 ...
- 基于 CoreDNS 和 K8s 构建云原生场景下的企业级 DNS
容器作为近些年最火热的后端技术,加快了很多企业的数字化转型进程.目前的企业,不是在使用云原生技术,就是在转向云原生技术的过程中.在容器化进程中,如何保持业务的平稳迁移,如何将现有的一些服务设施一并进行 ...
- Vulnhub 靶机 THE PLANETS: EARTH
0x01信息收集 1.1.nmap扫描 IP段扫描,确定靶机地址 平扫描 nmap 192.168.1.0/24 扫描结果(部分) Nmap scan report for earth.local ( ...
- SQLServer数据库事务级别
EFCore自动创建的数据库在SQLSERVER时是READ_COMMITTED_SNAPSHOT,SQLSERVER创建数据库默认是READ_COMMITTED. 因此记录一下查看和修改的方法,以便 ...
- 配置和使用nvm免安装版本(nvm-noinstall.zip)
配置和使用nvm免安装版本(nvm-noinstall.zip) NVM(Node Version Manager)是一个用于管理多个Node.js版本的命令行工具一下分几个步骤说明如何配置和使用nv ...
- 一份阅读量30万+免费且全面的C#/.NET面试宝典
前言 C#/.NET/.NET Core相关技术常见面试题汇总,不仅仅为了面试而学习,更多的是查漏补缺.扩充知识面和大家共同学习进步.该知识库主要由自己平时学习实践总结.网上优秀文章资料收集(这一部分 ...