此文章仅仅说明在springboot整合shiro时的一些坑,并不是教程

增加依赖

<!-- 集成shiro依赖 -->
<dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring-boot-web-starter</artifactId><version>1.4.0-RC2</version>
</dependency>

配置三个必须的Bean

  1. Realm
    用于授权和登录
        @Beanpublic Realm realm() {//创建自己的Realm实例return new UserRealm();}
  1. ShiroFilterChainDefinition
    用于实现权限
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();// 三种方式实现定义权限路径// 第一种:使用角色名定义chainDefinition.addPathDefinition("/admin/**", "authc, roles[admin]");// 第二种:使用权限code定义chainDefinition.addPathDefinition("/docs/**", "authc, perms[document:read]");// 第三种:使用接口的自定义配置(此处配置之后需要在对应的接口使用@RequiresPermissions(""))chainDefinition.addPathDefinition("/**", "authc");return chainDefinition;
  1. CacheManager
    缓存管理
        @Beanprotected CacheManager cacheManager() {return new MemoryConstrainedCacheManager();}

还有一些配置,可以在配置文件中配置,具体配置项见 shiro配置


以下内容是为了实现前后端分离,配置shiro拦截器实现返回401状态码的需求

编写拦截器类

public class FormLoginFilter extends PathMatchingFilter {@Overrideprotected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {Subject subject = SecurityUtils.getSubject();boolean isAuthenticated = subject.isAuthenticated();if (!isAuthenticated) {HttpServletResponse resp = (HttpServletResponse) response;resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);resp.getWriter().print("NO AUTH!");return false;}return true;}
}

在之前配置的三个Bean的基础上多配置一个BeanShiroFilterFactoryBean

    @Beanpublic ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();// 必须设置SecuritManagershiroFilterFactoryBean.setSecurityManager(securityManager);Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();//配置拦截器,实现无权限返回401,而不是跳转到登录页filters.put("authc", new FormLoginFilter());// 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面shiroFilterFactoryBean.setLoginUrl("/login");// 登录成功后要跳转的链接shiroFilterFactoryBean.setSuccessUrl("/index");// 未授权界面;shiroFilterFactoryBean.setUnauthorizedUrl("/403");// 拦截器Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();// 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边// authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问filterChainDefinitionMap.put("/**", "authc");shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);return shiroFilterFactoryBean;}

此处配置了过滤链,上面三个必须的Bean中修改其中的ShiroFilterChainDefinition

    @Beanpublic ShiroFilterChainDefinition shiroFilterChainDefinition() {//不需要在此处配置权限页面,因为上面的ShiroFilterFactoryBean已经配置过,但是此处必须存在,因为shiro-spring-boot-web-starter或查找此Bean,没有会报错return new DefaultShiroFilterChainDefinition();;}

springboot整合shiro使用shiro-spring-boot-web-starter相关推荐

  1. springboot整合全文搜索引擎Elasticsearch | Spring Boot 28

  2. SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例(转)...

    SpringBoot整合mybatis.shiro.redis实现基于数据库的细粒度动态权限管理系统实例 shiro 目录(?)[+] 前言 表结构 maven配置 配置Druid 配置mybatis ...

  3. SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例...

    SpringBoot整合mybatis.shiro.redis实现基于数据库的细粒度动态权限管理系统实例 shiro 目录(?)[+] 1.前言 本文主要介绍使用SpringBoot与shiro实现基 ...

  4. Spring Boot Web

    一. 概述 下面我们将进入 SpringBoot 基础阶段的学习. 在没有正式的学习 SpringBoot 之前,我们要先来了解下什么是 Spring . 我们可以打开 Spring 的官网 ( ht ...

  5. PART 5: INTEGRATING SPRING SECURITY WITH SPRING BOOT WEB

    转自:http://justinrodenbostel.com/2014/05/30/part-5-integrating-spring-security-with-spring-boot-web/ ...

  6. Spring boot web(2):web综合开发

    1 web开发 Spring boot web 开发非常简单,其中包括常用的 json输出.filters.property.log等 1.1 json接口开发 在以前的Spring 开发我么提供js ...

  7. okta-spring_通过Okta的单点登录保护Spring Boot Web App的安全

    okta-spring "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多 ...

  8. 通过Okta的单点登录保护Spring Boot Web App的安全

    "我喜欢编写身份验证和授权代码." 〜从来没有Java开发人员. 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证. 您可以使 ...

  9. spring boot web 开发示例

    一.创建Maven工程 创建maven工程,packaging 类型选择jar. 二.配置相关maven依赖. 1,首先你需要在pom中最上方添加spring boot的父级依赖,这样当前的项目就是S ...

  10. Spring Boot Web应用程序中注册 Servlet 的方法实例

    Spring Boot Web应用程序中注册 Servlet 的方法实例 本文实例工程源代码:https://github.com/KotlinSpringBoot/demo1_add_servlet ...

最新文章

  1. 2021年大数据ELK(二):Elasticsearch简单介绍
  2. CMS垃圾收集器小实验之CMSInitiatingOccupancyFraction参数
  3. 用注解还是用xml 配置?
  4. php add 返回id,PHP curl_multi_close函数
  5. python实现选择文件_python3实现文件选择对话框
  6. 高斯滤波器是低通还是高通_经典模拟滤波器仍值得研究吗?
  7. HTML5移动Web开发(六)——定义一个内容策略
  8. IA64与x64的区别
  9. 利用Python进行数据分析(3) 使用IPython提高开发效率
  10. 没有安装gcc,导致提示configure cannot guess build type; you must specify one
  11. 如何设计 user 表?加入第三方登录呢?
  12. Python学习笔记--CSV模块读写数据(转)
  13. 系统分析师-论文准备
  14. hg8245设置无线打印服务器,小编教您设置华为HG8245光纤猫路由器
  15. window-批量创建文件夹
  16. 【百度AI图像识别】LOGO帝来袭~ 个体再小、LOGO不能少
  17. 2023年厦门大学应用统计专硕考研上岸经验分享
  18. IEEE 754二进制浮点数算术标准
  19. 实习生、应届生、三方协议、实习协议、劳动合同的区别
  20. LVGL lv_line线条(15)

热门文章

  1. .Net+SQL Server企业应用性能优化笔记3——SQL查询语句
  2. 演示:思科设备基于物理接口帧中继(fame-relay)的配置
  3. 摩根上调 思科股票评级至增持
  4. 职场小窍门:看穿同事性格的16个小动作
  5. echart 多柱图只显示部分数据标签_2分钟上手、3小时学会无代码软件开发---Echarts数据可视化...
  6. 编程入门:准备学Python入门编程 为什么前辈一直劝我不行?
  7. 为了上班摸鱼,我用Python开发“BOSS来了”
  8. 为什么获取crm服务器信息失败,为 Outlook 配置 Microsoft Dynamics CRM 客户端时出现 与 Microsoft Dynamics CRM 服务器通信时出现问题 错误...
  9. markdown 图片居中_Markdown更改字体、颜色、大小,插入表格等方法
  10. ASP.NET MVC – HTML 帮助器简介