注意点有:
1. 不要启用 spring-boot-devtools, 如果启用 devtools 后, 不管是热启动还是手工重启, devtools总是试图重新恢复之前的session数据, 很有可能会导致session混乱.
2. 在配置 Shiro 的filterChainDefinitionMap时, 登陆的 url 也要使用 authc filter.
3. 自定义 Realm 类的 doGetAuthenticationInfo() 方法, 仅生成 AuthenticationInfo 对象, 不需要做密码验证.  密码验证步骤是在 Realm 父类的 assertCredentialsMatch() 方法完成.

这篇仅仅列了一些有关shiro不错的文章

============================
生产环境代码参考

============================
补习系列(6)- springboot 整合 shiro 一指禅
https://www.cnblogs.com/littleatp/p/9471950.html
SpringBoot 整合 Redis 实现 Shiro 权限控制的集群 Session 共享
http://itechor.top/solo/articles/2018/11/09/1541749859183.html
SpringBoot系列 - 集成Shiro权限管理
https://www.xncoding.com/2017/07/07/spring/sb-shiro.html

============================
了解原理
============================
Shiro - 关于session
http://www.cnblogs.com/Kavlez/p/4135857.html

电子书
https://waylau.gitbooks.io/apache-shiro-1-2-x-reference/content/II.%20Core%20%E6%A0%B8%E5%BF%83/6.%20Authorization%20%E6%8E%88%E6%9D%83.html

============================
其他文章
============================
Spring Boot 集成 Shiro 权限管理与密码加盐加密存储
https://xlui.me/t/spring-boot-shiro/
SpringBoot系列 - 集成Shiro权限管理
https://www.xncoding.com/2017/07/07/spring/sb-shiro.html
Spring Boot 整合Shiro和Redis缓存Session
https://blog.csdn.net/cckevincyh/article/details/79633661
Spring Boot Shiro权限控制
https://mrbird.cc/Spring-Boot-Shiro%20Authorization.html
Apache shiro集群实现 系列文章
https://blog.csdn.net/lishehe/article/details/45218251

============================
shiro-redis 插件
============================
shiro-redis 为 shiro 框架提供了基于 Redis 的SessionDAO实现, 名为: RedisSessionDAO; 同时也提供了基于Redis 的CacheManager实现, 名为: RedisCacheManager. 使用该插件就可以使用 redis 共享 session.

shiro-redis 插件使用了 DefaultWebSessionManager, 而不是默认的ServletContainerSessionManager, 目的是可以自己在 redis 中维护 Session.

官网 http://alexxiyang.github.io/shiro-redis/
源码: https://github.com/alexxiyang/shiro-redis

==========================
shiro starter 的相关注解
==========================

<dependency>
   <groupId>org.apache.shiro</groupId>
   <artifactId>shiro-spring-boot-starter</artifactId>
   <version>1.4.0</version>
</dependency>

application.properties 的两个属性:
   shiro.enabled : 用来控制是否启用shiro
   shiro.annotations.enabled, 用来控制是否启用shiro的注解功能.

-----------------------------
启用 shiro 注解
-----------------------------
要想启用shiro注解功能, 必须注入下面两个bean,  shiro-spring-boot-starter 1.4 之后已经自动配置了, 要想关闭注解功能, 可设置application.properties 参数 shiro.annotations.enabled ,   1.4版本之前需要手动增加下面两个bean.

//开启shiro aop注解支持, 启用权限注解
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
} //开启shiro aop注解支持, 启用认证注解
@Bean
@ConditionalOnMissingBean
public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
defaultAAP.setProxyTargetClass(true);
return defaultAAP;
}

-----------------------------
shiro 注解清单
-----------------------------
Shiro 注解既可以放在 Controller 中, 也可以放在 Service 中, 建议放到 Controller 中, 因为如果 Service 层使用了 Spring 事务注解, Shiro 注解将无效.
@RequiresAuthentication , 该注解可以加到类和方法上, 要求当前的 subject 已经登录

@RequiresGuest , 该注解要求当前的 subject 未登录, 或处于 RememberMe

@RequiresUser, 该注解要求当前的 subject 已经登录, 或处于 RememberMe

@RequiresPermissions, 该注解要求当前的subject指定的权限.
@RequiresPermissions("account:create")
@RequiresPermissions (value={"account:create", "account:delete}) 默认的 logical 参数 取值为 Logical.OR
@RequiresPermissions (value={"account:create", "account:delete}, logical= Logical.AND)

@RequiresRoles, 该注解要求当前的subject具有所有的指定角色
@RequiresRoles("administrator")
@RequiresRoles(value={"admin", "user"}) 默认的 logical 参数 取值为 Logical.AND
@RequiresRoles(value={"admin", "user"}, logical= Logical.OR)

参考: https://www.cnblogs.com/roxy/p/7688076.html

-----------------------------
shiro 内置的filter
-----------------------------

Filter Name Class Description
anon org.apache.shiro.web.filter.authc.AnonymousFilter 匿名拦截器,即不需要登录即可访问;一般用于静态资源过滤;示例/static/**=anon
authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter 基于表单的拦截器;如/**=authc,如果没有登录会跳到相应的登录页面登录
authcBasic org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter Basic HTTP身份验证拦截器
logout org.apache.shiro.web.filter.authc.LogoutFilter 退出拦截器,主要属性:redirectUrl:退出成功后重定向的地址(/),示例/logout=logout
noSessionCreation org.apache.shiro.web.filter.session.NoSessionCreationFilter 不创建会话拦截器,调用subject.getSession(false)不会有什么问题,但是如果subject.getSession(true)将抛出DisabledSessionException异常
perms org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter 权限授权拦截器,验证用户是否拥有所有权限;属性和roles一样;示例/user/**=perms["user:create"]
port org.apache.shiro.web.filter.authz.PortFilter 端口拦截器,主要属性port(80):可以通过的端口;示例/test= port[80],如果用户访问该页面是非80,将自动将请求端口改为80并重定向到该80端口,其他路径/参数等都一样
rest org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter rest风格拦截器,自动根据请求方法构建权限字符串;示例/users=rest[user],会自动拼出user:read,user:create,user:update,user:delete权限字符串进行权限匹配(所有都得匹配,isPermittedAll)
roles org.apache.shiro.web.filter.authz.RolesAuthorizationFilter 角色授权拦截器,验证用户是否拥有所有角色;示例/admin/**=roles[admin]
ssl org.apache.shiro.web.filter.authz.SslFilter SSL拦截器,只有请求协议是https才能通过;否则自动跳转会https端口443;其他和port拦截器一样;
user org.apache.shiro.web.filter.authc.UserFilter 用户拦截器,用户已经身份验证/记住我登录的都可;示例/**=user

==========================
Demo 代码
==========================
littleatp 博客代码复现(使用内存Realm):
https://files.cnblogs.com/files/harrychinese/ShiroMemRealmDemo.7z
使用MySqL数据库扩展 littleatp 博客代码(使用 Jdbc Realm):
https://files.cnblogs.com/files/harrychinese/ShiroJdbcRealmDemo.7z

SpringBoot系列: SpringBoot Web项目中使用Shiro的更多相关文章

  1. Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

  2. 在web项目中使用shiro(认证、授权)

    一.在web项目中实现认证 第一步,在web项目中导入shiro依赖的包 第二步,在web.xml中声明shiro拦截权限的过滤器 <filter> <filter-name> ...

  3. SpringBoot系列: SpringBoot Web项目中使用Shiro 之二

    ==================================Shiro 的加深理解:==================================1. Shiro 和 Spring 系组 ...

  4. Java Web学习系列——Maven Web项目中集成使用Spring

    参考Java Web学习系列——创建基于Maven的Web项目一文,创建一个名为LockMIS的Maven Web项目. 添加依赖Jar包 推荐在http://mvnrepository.com/.h ...

  5. 在web项目中使用shiro(记住我功能)

    第一步,添加“记住我”复选框,rememberMe要设置参数 第二步,配置shiro的主配置文件 注意 rememberMeCookie对应的bean中要声明 <constructor-arg ...

  6. SpringBoot Web项目中中如何使用Junit

    Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...

  7. 在前后端分离的SpringBoot项目中集成Shiro权限框架

    参考[1].在前后端分离的SpringBoot项目中集成Shiro权限框架 参考[2]. Springboot + Vue + shiro 实现前后端分离.权限控制   以及跨域的问题也有涉及

  8. SpringBoot系列教程web篇之过滤器Filter使用指南

    web三大组件之一Filter,可以说是很多小伙伴学习java web时最早接触的知识点了,然而学得早不代表就用得多.基本上,如果不是让你从0到1写一个web应用(或者说即便从0到1写一个web应用) ...

  9. SpringBoot系列教程web篇之自定义异常处理HandlerExceptionResolver

    关于Web应用的全局异常处理,上一篇介绍了ControllerAdvice结合@ExceptionHandler的方式来实现web应用的全局异常管理: 本篇博文则带来另外一种并不常见的使用方式,通过实 ...

随机推荐

  1. Python编码规范(PEP8)及奇技淫巧(不断更新)

    https://blog.csdn.net/MrLevo520/article/details/69155636

  2. python之三元表达式、列表推导式、生成器表达式、递归、匿名函数、内置函数

    一 三元表达式.列表推导式.生成器表达式 一 三元表达式 name=input('姓名>>: ') res='SB' if name == 'alex' else 'NB' print(r ...

  3. Win10系统如何在防火墙里开放端口

    Win10系统如何在防火墙里开放端口(下面傻瓜式教学) 然后怎么做呢?????? 下一步.下一步.下一步.下一步.下一步.下一步.下一步.下一步.下一步.下一步......... 随便起个名字 KO

  4. scipy.stats.multivariate_normal的使用

    参考:https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.multivariate_normal.html ...

  5. 多数据库有序GUID

    背景 常见的一种数据库设计是使用连续的整数为做主键,当新的数据插入到数据库时,由数据库自动生成.但这种设计不一定适合所有场景. 随着越来越多的使用Nhibernate.EntityFramework等 ...

  6. Linux笔记-ps -aux的结果解析

    参考: https://blog.csdn.net/flyingleo1981/article/details/7739490 ps 的参数说明ps 提供了很多的选项参数,常用的有以下几个: l 长格 ...

  7. python关于二分查找

    楔子 如果有这样一个列表,让你从这个列表中找到66的位置,你要怎么做? l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72 ...

  8. MYSQL 两表 排除 重复记录

    MySQL之——查询重复记录.删除重复记录方法大全 - 冰河的专栏 - CSDN博客https://blog.csdn.net/l1028386804/article/details/51733585 ...

  9. 基本环境安装: Centos7+Java+Hadoop+Spark+HBase+ES+Azkaban

    1.  安装VM14的方法在 人工智能标签中的<跨平台踩的大坑有提到> 2. CentOS分区设置: /boot:1024M,标准分区格式创建. swap:4096M,标准分区格式创建. ...

  10. params.success && params.success(res.data)

    params.success && params.success(res.data)   只有success 为真,才执行后边的代码