shiro使用注解(@RequiresPermissions等)不无效及异常处理
1、注解不生效
在shiro配置类中加上如下代码:
/**
* Shiro生命周期处理器
*/
@Bean(name = "lifecycleBeanPostProcessor")
public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
} /**
* 开启Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证
*/
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
creator.setProxyTargetClass(true);
return creator;
} /**
* 开启shiro aop注解支持.
* 使用代理方式;所以需要开启代码支持;
*/
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
2、异常处理
过滤器必须要是AuthorizationFilter
过滤器才能生效,即只有perms,roles,ssl,rest,port才是属于AuthorizationFilter,而anon,authcBasic,auchc,user是AuthenticationFilter,所以unauthorizedUrl设置后页面不跳转。此处使用springmvc同意异常处理来解决:
package com.example.springbootshiro.controller; import com.example.springbootshiro.constants.CommonConstants;
import com.example.springbootshiro.domain.vo.ResponseVO;
import com.example.springbootshiro.enums.ResponseStatusEnum;
import com.example.springbootshiro.utils.ResultUtil;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import java.lang.reflect.UndeclaredThrowableException; /**
* 统一异常处理类<br>
* 捕获程序所有异常,针对不同异常,采取不同的处理方式
*
*/
@ControllerAdvice
public class ExceptionHandleController {
private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionHandleController.class); // @ResponseBody
@ExceptionHandler(UnauthorizedException.class)
public String handleShiroException(Exception ex) {
return "redirect:/error/403";
} // @ResponseBody
@ExceptionHandler(AuthorizationException.class)
public String AuthorizationException(Exception ex) {
return "redirect:/error/401";
} }
具体处理逻辑自己控制
shiro使用注解(@RequiresPermissions等)不无效及异常处理的更多相关文章
- shiro添加注解@RequiresPermissions无效
在学习和使用shiro中,需要整合shiro框架,然后可以在spring中中使用有三种方法,我用的是注解开发这种方式,但是,我加入注解后发现,没什么作用,然后想着肯定是没有注解成功,然后查找资料,发现 ...
- shiro添加注解@RequiresPermissions不起作用
这是因为没有开启spring拦截器,在spring-mvc.xml中加入以下代码就可以了(一定要写在最先加载的xml中,写在后面加载的xml中也不起作用) <bean class="o ...
- Shiro 权限注解
Shiro 权限注解: Shiro 提供了相应的注解用于权限控制,如果使用这些注解就需要使用AOP 的功能来进行 判断,如Spring AOP:Shiro 提供了Spring AOP 集成用于 ...
- Shiro启用注解方式
shiro验证权限方式一种是基于url配置文件: 例如: <bean id="shiroFilter" class="org.apache.shiro.spring ...
- 【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面
这几天粗浅的把shiro整合到spring中,并且注解控制shiro用户/角色/权限And/OR 步骤: 1.首先maven搭建web项目 2.创建数据库 user/role/authority 其中 ...
- Shiro权限注解原理
概述 前不久刚学会使用权限注解(),开始思索了一番.最开始猜测实现方式是注解@Aspect,具体实现方式类似如下所示(切面记录审计日志).后来发现并非如此,所以特地分析一下源码. @Component ...
- shiro授权+注解式开发
shiro授权和注解式开发 1.shiro授权角色.权限 2.Shiro的注解式开发 ShiroUserMapper.xml <select id="getRolesByUserId& ...
- SpringBoot Shiro 权限注解不起作用
最近在学习springboot结合shiro做权限管理时碰到一个问题. 问题如下: 我在userRealm中的doGetAuthorizationInfo方法中给用户添加了权限,然后在Controll ...
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(二)shiro权限注解 shiro注 ...
随机推荐
- CodeForces 939E Maximize
Maximize 题意:整个程序有2种操作,操作1将一个元素放入集合S中,且保证最新插入的元素不小于上一次的元素, 操作2 找到集合S中的某个子集合, 使得 集合中最大的元素减去平均数的值最大. 题解 ...
- poj2528 Mayor's posters(线段树区间修改+特殊离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- Wannafly挑战赛16---A 取石子
链接:https://www.nowcoder.com/acm/contest/113/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言52428 ...
- TypeScript + React + Redux 实战简单天气APP全套完整项目
下载链接:https://www.yinxiangit.com/171.html 目录: 从面向过程的js到面向对象的js,让web前端更加高大尚.让你的前端步步日上,紧跟技术发展的前沿.让你构建更加 ...
- 大型公司java架构师面试实战讲解高清视频教程 15课
目录: 01.面试必考之HashMap源码分析与实现02.探索JVM底层奥秘ClassLoader源码分析与案例讲解03.大型网站数据库瓶颈之数据库分库分表方案实践04.资料为图灵学院所有05.大型公 ...
- 解决subline安装插件被墙失败的方法
一.问题场景描述 当你完成subline和package control的安装后,准备使用install package安装各种各样的插件来丰富你的编辑器,却出现类似 “Unable to downl ...
- IDEA中输出syso的快捷键设置
1. 2. 3. 4. 5.上图中的第三步会出现警告,那个红色的字,点击Define,选择Java 6.之后点击Apply和OK即可
- sql 单表distinct/多表group by查询去除重复记录
单表distinct 多表group by group by 必须放在 order by 和 limit之前,不然会报错 下面先来看看例子: table id name 1 a 2 b ...
- windows平台:查看端口占用情况,请杀死端口对应进程PID
//查看 netstat -ano | findstr //杀死 taskkill /f /PID
- Winform中实现ZedGraph的多条Y轴(附源码下载)
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...