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 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM