最近在学习springboot结合shiro做权限管理时碰到一个问题。

问题如下:

我在userRealm中的doGetAuthorizationInfo方法中给用户添加了权限,然后在Controller中写了下面的代码。其中URL为/listArticle的方法必须要有article:over权限才能通过。我在doGetAuthorizationInfo方法中给该用户添加的权限并没有article:over,但是当前端向该URL发送请求时,@RequiresPermissions注解不起作用,listArticle方法被执行了。而且我在方法内部打印isPermitted("article:over")的输出结果为false。最终通过网上查博客,发现是没有开启Shiro的注解支持的原因。

@RequiresPermissions("article:over")
@RequestMapping(value = "/listArticle", method = RequestMethod.POST)
public JSONObject listArticle(@RequestBody JSONObject request){
System.out.println("---------isPermitted article:over--------" + SecurityUtils.getSubject().isPermitted("article:over"));
CommonTools.paginationConvert(request);
int ArticleNum = articleService.getArticleNum();
List<JSONObject> list = articleService.listArticle(request.getIntValue("offset"), request.getIntValue("pageRow"));
return Result.success(request, list, ArticleNum);
}

解决方法如下:

在ShiroConfig中添加如下一段代码。

//开启对shiro注解的支持
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
advisor.setSecurityManager(securityManager);
return advisor;
}
//开启aop注解支持
@Bean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
defaultAAP.setProxyTargetClass(true);
return defaultAAP;
}

添加完这段代码后,我再向该URL发送请求时就会报如下异常,表明该用户没有该权限:

org.apache.shiro.authz.AuthorizationException: Not authorized to invoke method

参考博客:

https://www.cnblogs.com/shiguotao-com/p/10617693.html

https://www.cnblogs.com/tuifeideyouran/p/7696055.html

SpringBoot Shiro 权限注解不起作用的更多相关文章

  1. Shiro 权限注解

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

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

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

  3. springboot shiro没有注解解决方案

    颓废的悠然   springboot shiro开启注释 shiroconfiguration中增加 1 2 3 4 5 6 7 @Bean     public AuthorizationAttri ...

  4. Shiro权限注解原理

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

  5. SpringBoot&Shiro实现权限管理

    SpringBoot&Shiro实现权限管理 引言 相信大家前来看这篇文章的时候,是有SpringBoot和Shiro基础的,所以本文只介绍整合的步骤,如果哪里写的不好,恳请大家能指出错误,谢 ...

  6. Shiro 权限校验不通过时,区分GET和POST请求正确响应对应的方式

    引入:https://blog.csdn.net/catoop/article/details/69210140 本文基于Shiro权限注解方式来控制Controller方法是否能够访问. 例如使用到 ...

  7. SpringBoot整合Shiro权限框架实战

    什么是ACL和RBAC ACL Access Control list:访问控制列表 优点:简单易用,开发便捷 缺点:用户和权限直接挂钩,导致在授予时的复杂性,比较分散,不便于管理 例子:常见的文件系 ...

  8. springboot shiro和freemarker集成之权限控制完全参考手册(跳过认证,登录由三方验证,全网首发)

    本文主要考虑单点登录场景,登录由其他系统负责,业务子系统只使用shiro进行菜单和功能权限校验,登录信息通过token从redis取得,这样登录验证和授权就相互解耦了. 用户.角色.权限进行集中式管理 ...

  9. springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3. springboot+shiro+redis(集群re ...

随机推荐

  1. Springboot 基本认识

    不管是 spring cloud alibaba 还是 spring cloud netflix,都 是基于 springboot 这个微框架来构建的,所以我希望花一 点时间来讲一下 springbo ...

  2. 101道Numpy、Pandas练习题

    无论是数据分析还是机器学习,数据的预处理必不可少. 其中最常用.最基础的Python库非numpy和pandas莫属,很多初学者可能看了很多教程,但是很快就把用法忘光了. 光看不练假把式,今天向大家推 ...

  3. Angular 2 for 2017 web full stack development

    1 1 1 Angular 2 for 2017 web full stack development 1 1 https://angular2.xgqfrms.xyz/ https://ng2-he ...

  4. API & YApi

    API & YApi 接口管理服务 YApi http://yapi.demo.qunar.com/ https://ued.qunar.com/ build bug https://gith ...

  5. macOS warning emoji render bug

    macOS warning emoji render bug ️ macOS render bug Apple Color Emoji fonts install old version fonts ...

  6. TCP 协议三次握手过程分析

    TCP 协议三次握手过程分析 TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: ...

  7. React Hooks in depth

    React Hooks in depth React Hooks https://reactjs.org/docs/hooks-rules.html https://www.npmjs.com/pac ...

  8. taro best practice

    taro best practice 最佳实践 https://taro-docs.jd.com/taro/docs/best-practice.html#关于-jsx-支持程度补充说明 https: ...

  9. git alias & zsh

    git alias & zsh VPN & git work tree # git pull === gp ➜ .git git:(feature/select-seat-system ...

  10. 下一代币王花落谁家?是否是BGV更胜一筹呢?

    BGV作为Baccarat的平台币横空出世,通过BGV来激励拥有NGK的用户.在共识算法上,Baccarat 在共识层采用了混合机制 DPOSS共识机制,这是维护 Baccarat 生态体系良性发展的 ...