shiro java配置,InI 配置 - 跟我學 Apache Shiro_教程_Java開發社區

INI 配置

之前章節我們已經接觸過一些 INI 配置規則了,如果大家使用過如 Spring 之類的 IoC/DI 容器的話,Shiro 提供的 INI 配置也是非常類似的,即可以理解為是一個 IoC/DI 容器,但是區別在于它從一個根對象 securityManager 開始。

根對象 SecurityManager

從之前的 Shiro 架構圖可以看出,Shiro 是從根對象 SecurityManager 進行身份驗證和授權的;也就是所有操作都是自它開始的,這個對象是線程安全且真個應用只需要一個即可,因此 Shiro 提供了 SecurityUtils 讓我們綁定它為全局的,方便后續操作。

因為 Shiro 的類都是 POJO 的,因此都很容易放到任何 IoC 容器管理。但是和一般的 IoC 容器的區別在于,Shiro 從根對象 securityManager 開始導航;Shiro 支持的依賴注入:public 空參構造器對象的創建、setter 依賴注入。

1、純 Java 代碼寫法(com.github.zhangkaitao.shiro.chapter4.NonConfigurationCreateTest):

DefaultSecurityManager securityManager = new DefaultSecurityManager();

//設置authenticator

ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();

authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());

securityManager.setAuthenticator(authenticator);

//設置authorizer

ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer();

authorizer.setPermissionResolver(new WildcardPermissionResolver());

securityManager.setAuthorizer(authorizer);

//設置Realm

DruidDataSource ds = new DruidDataSource();

ds.setDriverClassName("com.mysql.jdbc.Driver");

ds.setUrl("jdbc:mysql://localhost:3306/shiro");

ds.setUsername("root");

ds.setPassword("");

JdbcRealm jdbcRealm = new JdbcRealm();

jdbcRealm.setDataSource(ds);

jdbcRealm.setPermissionsLookupEnabled(true);

securityManager.setRealms(Arrays.asList((Realm) jdbcRealm));

//將SecurityManager設置到SecurityUtils 方便全局使用

SecurityUtils.setSecurityManager(securityManager);

Subject subject = SecurityUtils.getSubject();

UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");

subject.login(token);

Assert.assertTrue(subject.isAuthenticated());

2、等價的 INI 配置(shiro-config.ini)

[main]

\#authenticator

authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator

authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy

authenticator.authenticationStrategy=$authenticationStrategy

securityManager.authenticator=$authenticator

\#authorizer

authorizer=org.apache.shiro.authz.ModularRealmAuthorizer

permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver

authorizer.permissionResolver=$permissionResolver

securityManager.authorizer=$authorizer

\#realm

dataSource=com.alibaba.druid.pool.DruidDataSource

dataSource.driverClassName=com.mysql.jdbc.Driver

dataSource.url=jdbc:mysql://localhost:3306/shiro

dataSource.username=root

\#dataSource.password=

jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm

jdbcRealm.dataSource=$dataSource

jdbcRealm.permissionsLookupEnabled=true

securityManager.realms=$jdbcRealm?

即使沒接觸過 IoC 容器的知識,如上配置也是很容易理解的:

對象名 = 全限定類名 相對于調用 public 無參構造器創建對象

對象名. 屬性名 = 值 相當于調用 setter 方法設置常量值

對象名. 屬性名 =$ 對象引用 相當于調用 setter 方法設置對象引用

3、Java 代碼(com.github.zhangkaitao.shiro.chapter4.ConfigurationCreateTest)

Factory factory =

new IniSecurityManagerFactory("classpath:shiro-config.ini");

org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();

//將SecurityManager設置到SecurityUtils 方便全局使用

SecurityUtils.setSecurityManager(securityManager);

Subject subject = SecurityUtils.getSubject();

UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");

subject.login(token);

Assert.assertTrue(subject.isAuthenticated());?

如上代碼是從 Shiro INI 配置中獲取相應的 securityManager 實例:

默認情況先創建一個名字為 securityManager,類型為 org.apache.shiro.mgt.DefaultSecurityManager 的默認的 SecurityManager,如果想自定義,只需要在 ini 配置文件中指定 “securityManager=SecurityManager 實現類” 即可,名字必須為 securityManager,它是起始的根;

IniSecurityManagerFactory 是創建 securityManager 的工廠,其需要一個 ini 配置文件路徑,其支持 “classpath:”(類路徑)、“file:”(文件系統)、“url:”(網絡)三種路徑格式,默認是文件系統;

接著獲取 SecuriyManager 實例,后續步驟和之前的一樣。

從如上可以看出 Shiro INI 配置方式本身提供了一個簡單的 IoC/DI 機制方便在配置文件配置,但是是從 securityManager 這個根對象開始導航。

INI 配置

ini 配置文件類似于 Java 中的 properties(key=value),不過提供了將 key/value 分類的特性,key 是每個部分不重復即可,而不是整個配置文件。如下是 INI 配置分類:

[main]

\#提供了對根對象securityManager及其依賴的配置

securityManager=org.apache.shiro.mgt.DefaultSecurityManager

…………

securityManager.realms=$jdbcRealm

[users]

\#提供了對用戶/密碼及其角色的配置,用戶名=密碼,角色1,角色2

username=password,role1,role2

[roles]

\#提供了角色及權限之間關系的配置,角色=權限1,權限2

role1=permission1,permission2

[urls]

\#用于web,提供了對web url攔截相關的配置,url=攔截器[參數],攔截器

/index.html = anon

/admin/** = authc, roles[admin], perms["permission1"]

[main] 部分

提供了對根對象 securityManager 及其依賴對象的配置。

創建對象

securityManager=org.apache.shiro.mgt.DefaultSecurityManager

其構造器必須是 public 空參構造器,通過反射創建相應的實例。

常量值 setter 注入

dataSource.driverClassName=com.mysql.jdbc.Driver

jdbcRealm.permissionsLookupEnabled=true?

會自動調用 jdbcRealm.setPermissionsLookupEnabled(true),對于這種常量值會自動類型轉換。

對象引用 setter 注入

authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator

authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy

authenticator.authenticationStrategy=$authenticationStrategy

securityManager.authenticator=$authenticator?

會自動通過 securityManager.setAuthenticator(authenticator) 注入引用依賴。

嵌套屬性 setter 注入

securityManager.authenticator.authenticationStrategy=$authenticationStrategy

也支持這種嵌套方式的 setter 注入。

byte 數組 setter 注入

\#base64 byte[]

authenticator.bytes=aGVsbG8=

\#hex byte[]

authenticator.bytes=0x68656c6c6f?

默認需要使用 Base64 進行編碼,也可以使用 0x 十六進制。

Array/Set/List setter 注入

authenticator.array=1,2,3

authenticator.set=$jdbcRealm,$jdbcRealm?

多個之間通過 “,” 分割。

Map setter 注入

authenticator.map=$jdbcRealm:$jdbcRealm,1:1,key:abc

即格式是:map=key:value,key:value,可以注入常量及引用值,常量的話都看作字符串(即使有泛型也不會自動造型)。

實例化 / 注入順序

realm=Realm1

realm=Realm12

authenticator.bytes=aGVsbG8=

authenticator.bytes=0x68656c6c6f?

后邊的覆蓋前邊的注入。

測試用例請參考配置文件 shiro-config-main.ini。

[users] 部分

配置用戶名 / 密碼及其角色,格式:“用戶名 = 密碼,角色 1,角色 2”,角色部分可省略。如:

[users]

zhang=123,role1,role2

wang=123?

密碼一般生成其摘要 / 加密存儲,后續章節介紹。

[roles] 部分

配置角色及權限之間的關系,格式:“角色 = 權限 1,權限 2”;如:

[roles]

role1=user:create,user:update

role2=*?

如果只有角色沒有對應的權限,可以不配 roles,具體規則請參考授權章節。

[urls] 部分

配置 url 及相應的攔截器之間的關系,格式:“url = 攔截器 [參數],攔截器 [參數],如:

[urls]

/admin/** = authc, roles[admin], perms["permission1"]?

具體規則參見 web 相關章節。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/371630.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371630.shtml
英文地址,請注明出處:http://en.pswp.cn/news/371630.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

在Spring容器外部連接對象依賴項

有幾種有趣的方法可以設置在Spring容器外部實例化的對象的屬性和依賴關系。 用例首先,為什么我們需要在Spring容器之外進行依賴注入–我知道三個用例,其中我實例化了Spring容器之外的對象并需要注入依賴。 首先考慮使用Spring TaskExecutor執行一系列任…

前端學習路線匯總

https://123.w3cschool.cn/plk2fihttps://www.w3cschool.cn/nodejs/nodejs-global-object.htmlnode.js轉載于:https://www.cnblogs.com/sxz2008/p/7238773.html

MediaInfo源代碼分析 1:整體結構

博客地址:http://blog.csdn.net/leixiaohua1020/article/details/12016231 MediaInfo源代碼分析系列文章列表: MediaInfo源代碼分析 1:整體結構MediaInfo源代碼分析 2:API函數MediaInfo源代碼分析 3:Open()函數MediaI…

測試RESTful服務的客戶端

開發使用RESTful Web API的應用程序可能意味著開發服務器和客戶端。 為服務器端編寫集成測試可以像使用Arquillian來啟動服務器一樣容易,并且可以通過REST確保測試服務是否按預期工作。 問題是如何測試客戶端。 在本文中,我們將了解如何使用模擬來測試客…

linux mysql誤刪,linux下MySQL安裝與刪除?(Ubuntu)

1、MySQL安裝A)MySQL安裝: sudo apt-get install mysql-server mysql-clientB)啟動/停止MySQL服務:MySQL 在安裝以后,MySQL 就已經啟動;如果需要手動啟動或停止則如下操作:手動啟動服務: sudo start mysql手…

對多態的理解例子

public class A {public String show(D obj) {return ("A and D");}public String show(A obj) {return ("A and A");} }class B extends A{public String show(B obj){ //重載return ("B and B");}public String show(A obj){ //重寫show(A obj…

【Java每日一題】20170120

20170119問題解析請點擊今日問題下方的“【Java每日一題】20170120”查看(問題解析在公眾號首發,公眾號ID:weknow619) package Jan2017; import java.util.Arrays; import java.util.List; public class Ques0120 { publ…

使用NoSQL實現實體服務–第2部分:合同優先

現在該開始使用NoSQL項目對SOA實體服務進行編碼了,并且正如我所承諾的,我將從Web服務的合同開始。 看一下本系列的第1部分 。 這種從Web服務合同定義開始的技術是面向服務的體系結構實現的“合同優先”方法的核心,并具有許多技術優勢&#xf…

php hugepage,【原創】解決Redis啟動報錯:Transparent Huge Pages (THP) support enabled in your kernel...

問題背景Redis啟動報錯:WARNING you have Transparent Huge Pages (THP) support enabled in your kernel.This will create latency and memory usage issues with Redis.To fix this issue run the command echo never > /sys/kernel/mm/transparent_hugepage/…

hibernate cascade的真正含義

hibernate cascade 是 OneToOne OneToMany ManyToOne ManyToMany等注解的屬性&#xff0c;表示級聯操作。 /*** (Optional) The operations that must be cascaded to* the target of the association.** <p> By default no operations are cascaded.*/CascadeType[] cas…

射線碰撞檢測

在我們的游戲開發過程中&#xff0c;有一個很重要的工作就是進行碰撞檢測。例如在射擊游戲中子彈是否擊中敵人&#xff0c;在RPG游戲中是否撿到裝備等等。在進行碰撞檢測時&#xff0c;我們最常用的工具就是射線&#xff0c;Unity 3D的物理引擎也為我們提供了射線類以及相關的函…

高級ZK:異步UI更新和后臺處理–第1部分

異步UI更新非常有用&#xff0c;因為它們通常可以提高響應性&#xff0c;可用性和用戶界面的總體感覺。 我將在這里重點介紹ZK框架&#xff0c;但是通常&#xff0c;相同的原理也適用于桌面UI&#xff08;Swing&#xff0c;SWT&#xff09;。 長時間運行的處理 有時&#xff0…

php注冊登錄遍寫入 遍驗證,自動注冊登錄驗證機制的php代碼

在phpwind站點后臺添加“廣告管家”(CNZZ的一款廣告投放的應用)的應用&#xff0c;整個“廣告管家”通過iframe載入&#xff0c;載入的具體內容根據不同站點顯示針對該站點的具體內容。出于意用性方面的考慮&#xff0c;需要以下二點&#xff1a;1、首次進入“廣告管家”頁面自…

轉載:tensorflow保存訓練后的模型

訓練完一個模型后&#xff0c;為了以后重復使用&#xff0c;通常我們需要對模型的結果進行保存。如果用Tensorflow去實現神經網絡&#xff0c;所要保存的就是神經網絡中的各項權重值。建議可以使用Saver類保存和加載模型的結果。 1、使用tf.train.Saver.save()方法保存模型 tf.…

php url傳遞變量,php – 在laravel中通過url傳遞變量

我是laravel的新手,我正在努力讓我的網址格式正確.格式為http://mysite/blog?category1 instead of http://mysite/blog/category1這些是我正在使用的文件,有沒有辦法將路由放入BlogControllerRoute.phpRoute::get(blog/{category}, function($category null){// get all the…

Apache Wicket:記住我的功能

在Web應用程序中&#xff0c;具有“記住我”功能非常普遍&#xff0c;該功能使用戶每次訪問我們的網站時都能自動登錄。 可以使用Spring Security來實現這種功能&#xff0c;但我認為將基于請求的身份驗證框架與基于組件的Web框架結合使用并不是最好的主意。 這兩個世界不能很好…

Ubuntu 安裝中文

系統環境&#xff1a; 1. 官網 http://pinyin.sogou.com/linux/ 下載安裝包。 2. 先運行 apt-get update 。 3. 再運行 apt-get -f install 。 4. 再運行 可能有的UBuntu系統自帶了。 5. 如果下載的搜狐輸入法安裝包的格式為 .deb 的&#xff0c; 運行 &#xff1a; dpk…

Eigen教程(10)

整理下Eigen庫的教程&#xff0c;參考&#xff1a;http://eigen.tuxfamily.org/dox/index.html 混淆 在Eigen中&#xff0c;當變量同時出現在左值和右值&#xff0c;賦值操作可能會帶來混淆問題。這一篇將解釋什么是混淆&#xff0c;什么時候是有害的&#xff0c;怎么使用做。 …

matlab把符號數,Matlab?符號與數值之間的轉換

符號運算得到的是精確的解析解&#xff0c;但是有時需要進行數值轉換&#xff0c;主要通過以下幾個函數實現。1.digits 函數調用方法&#xff1a;digits(D)函數設置有效數字個數為D的近似解精度。2.vpa 函數vpaVariable-precision arithmeticSyntaxR vpa(A)R vpa(A,d)Descrip…

JSF組件庫–質量不只是零缺陷

自從我上次研究三個主要JSF組件庫的質量以來&#xff0c;已經有一段時間了。 2009年12月&#xff0c;我開始比較RichFaces&#xff0c;Primefaces和ICEfaces的整體軟件質量 。 從那時起&#xff0c;事情發生了變化&#xff0c;從現在開始&#xff0c;我想重新評估和更新它。 我…