SpringBoot系列: SpringBoot Web项目中使用Shiro

注意点有:
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 NameClassDescription
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

原文地址:https://www.cnblogs.com/harrychinese/p/SpringBoot_shiro_web.html