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等)不无效及异常处理的更多相关文章

  1. shiro添加注解@RequiresPermissions无效

    在学习和使用shiro中,需要整合shiro框架,然后可以在spring中中使用有三种方法,我用的是注解开发这种方式,但是,我加入注解后发现,没什么作用,然后想着肯定是没有注解成功,然后查找资料,发现 ...

  2. shiro添加注解@RequiresPermissions不起作用

    这是因为没有开启spring拦截器,在spring-mvc.xml中加入以下代码就可以了(一定要写在最先加载的xml中,写在后面加载的xml中也不起作用) <bean class="o ...

  3. Shiro 权限注解

      Shiro 权限注解:   Shiro 提供了相应的注解用于权限控制,如果使用这些注解就需要使用AOP 的功能来进行 判断,如Spring AOP:Shiro 提供了Spring AOP 集成用于 ...

  4. Shiro启用注解方式

    shiro验证权限方式一种是基于url配置文件: 例如: <bean id="shiroFilter" class="org.apache.shiro.spring ...

  5. 【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面

    这几天粗浅的把shiro整合到spring中,并且注解控制shiro用户/角色/权限And/OR 步骤: 1.首先maven搭建web项目 2.创建数据库 user/role/authority 其中 ...

  6. Shiro权限注解原理

    概述 前不久刚学会使用权限注解(),开始思索了一番.最开始猜测实现方式是注解@Aspect,具体实现方式类似如下所示(切面记录审计日志).后来发现并非如此,所以特地分析一下源码. @Component ...

  7. shiro授权+注解式开发

    shiro授权和注解式开发 1.shiro授权角色.权限 2.Shiro的注解式开发 ShiroUserMapper.xml <select id="getRolesByUserId& ...

  8. SpringBoot Shiro 权限注解不起作用

    最近在学习springboot结合shiro做权限管理时碰到一个问题. 问题如下: 我在userRealm中的doGetAuthorizationInfo方法中给用户添加了权限,然后在Controll ...

  9. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(二)shiro权限注解 shiro注 ...

随机推荐

  1. Git 上传本地项目到Github

    前提: 安装Git 注册并拥有Github账号 目录: 初始化本地目录位Git仓库 Github上创建仓库 本地生成SSH key,并添加到Github上 本地项目管理Github上远程项目 详细步骤 ...

  2. 模板汇总——Tarjian

    1. 单向边  + 新图建边 int belong[N], dfn[N], low[N], now_time, scc_cnt; stack<int> s; void dfs(int u) ...

  3. CF996B World Cup 思维 第十四 *

    World Cup time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  4. MySQL 数据库出现导入xls数据出现1062主从错误错误问题解决方案

    今天把xls数据表导入MySQL数据库时发现出现1062错误    ,并且有20-700条数据一直导入不了所以开始找解决方案. 解决方案1:   数据库表设计问题导致相同字段的重复数据不能导入   解 ...

  5. CVE-2014-6271 Shellshock 破壳漏洞 复现

    补坑. 什么是shellshock ShellShock是一个BashShell漏洞(据说不仅仅是Bash,其他shell也可能有这个漏洞). 一般情况来说,系统里面的Shell是有严格的权限控制的, ...

  6. Runtime.getRuntime().exec()实现Java调用python程序

    使用Runtime.getRuntime().exec()来实现Java调用python,调用代码如下所示: import java.io.BufferedReader; import java.io ...

  7. WebGL简易教程(四):颜色

    目录 1. 概述 2. 示例:绘制三角形 1) 数据的组织 2) varying变量 3. 结果 4. 理解 1) 图形装配和光栅化 2) 内插过程 5. 参考 1. 概述 在上一篇教程<Web ...

  8. 【深入浅出-JVM】(76):classloader

    方法 public Class<?> loadClass(String name) throws ClassNotFoundException 通过类名发挥这个类的Class实例 prot ...

  9. Android 网络通信框架Volley(三)

    NetworkImageView 分析:public class NetworkImageView extends ImageView 他继承自ImageView,并且添加了一个新方法: public ...

  10. UGUI_冻结技能键盘点击触发

    1.在某一张image图上添加Button组件,使其具有点击触发事件的功能: 2.outline组件 3.SkillItem脚本 using System.Collections; using Sys ...