搜尋
首頁Javajava教程shiro源碼的詳細介紹

shiro源碼的詳細介紹

Jul 26, 2017 pm 04:36 PM
shiro分析原始碼

(一)//1、取得SecurityManager工廠,此處使用Ini設定檔初始化SecurityManager
#Factory factory = new IniSecurityManagerFactory(" classpath:shiro.ini")
2.factory類別的類別結構為:
 

(3)abstractFactory類別主要設定是否為單一範例

(4)iniFactorySupport是支援ini設定所建立的對象

(5)iniSecuritymanagerFactory是ini方式建立securityManager的實作類別

(二)//2、得到SecurityManager實例並綁定給SecurityUtils
org.apache.shiro.mgt.SecurityManagersecurityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
(1)透過建立的Ini物件建立Securitymanager物件
1  IniSecurityManagerFactory类:2      creatSecuritymanager(ini){3      SecurityManager securityManager = createSecurityManager(ini);4      return securityManager;5  }

 

(2)透過建立的Ini物件建立Securitymanager物件
 1 private SecurityManager createSecurityManager(Ini ini) { 2 //null 3 Ini.Section mainSection = ini.getSection(MAIN_SECTION_NAME); 4 if (CollectionUtils.isEmpty(mainSection)) { 5  6 //try the default: null 7 mainSection = ini.getSection(Ini.DEFAULT_SECTION_NAME); 8 } 9  return createSecurityManager(ini, mainSection);10  }

 (3)透過ini物件和主模組mainSession建立Securitymanager

 1 private SecurityManager createSecurityManager(Ini ini, Ini.Section mainSection) { 2 //{securityManager=DefaultSecurityManager,iniRealm=IniRealm}   3 Map defaults = createDefaults(ini, mainSection); 4 Map objects = buildInstances(mainSection, defaults); 5  6 SecurityManager securityManager = getSecurityManagerBean(); 7 boolean autoApplyRealms = isAutoApplyRealms(securityManager); 8 if (autoApplyRealms) { 9 //realms and realm factory might have been created - pull them out first  so we can initialize the securityManager:10  Collection realms = getRealms(objects);11  //set them on the SecurityManager12  if (!CollectionUtils.isEmpty(realms)) {13          applyRealmsToSecurityManager(realms, securityManager);14            }15         }16        return securityManager;17     }
(4)透過mainSession和預設object物件設定物件之間的關聯
 1 private Map buildInstances(Ini.Section section, Map defaults) { 2      this.builder = new ReflectionBuilder(defaults); 3      return this.builder.buildObjects(section); 4      } 5 //类ReflectionBuilder 6 //通过mainSection创建对象并关联 7  public Map buildObjects(Map kvPairs) { 8   ..... 9  LifecycleUtils.init(objects.values());10 }
 (5)因為IniRealm實作了Initializable,則初始化IniRealm物件
 1 //类IniRealm 2 private void processDefinitions(Ini ini) { 3  Ini.Section usersSection = ini.getSection(USERS_SECTION_NAME); 4  processUserDefinitions(usersSection); 5 } 6 //通过userSection解析user模块 7  protected void processUserDefinitions(Map userDefs) { 8       for (String username : userDefs.keySet()) { 9          ........10       account = new SimpleAccount(username, password, getName());11       add(account);12         ........13        }14  }15 protected void add(SimpleAccount account) {16    String username = getUsername(account);17    USERS_LOCK.writeLock().lock();18    try {19     this.users.put(username, account);20    }finally {21     USERS_LOCK.writeLock().unlock();22 }
#IniRealm的類別結構為:

simpleAccount的結構為:
至此,建立物件關聯, IniRealm的初始化完成!接下來看DefaultSecurityManager的結構圖:

#(7)設定DefaultSecurityManager的realm屬性:
#
1  applyRealmsToSecurityManager(realms, securityManager){2     ((RealmSecurityManager) securityManager).setRealms(realms);3 }4 //在类RealmSecurityManager中5 public void setRealms(Collection realms) {6    this.realms = realms;7    afterRealmsSet();8 }

 注意:afterRealmsSet();主要用來設定authenticator、authorizer的realm屬性:

          至此Default
          至此Default
          完成、並回傳DefaultSecurityManager物件
//3、取得Subject及建立使用者名稱/密碼驗證Token(即使用者身分/憑證)
#Subject subject = SecurityUtils.getSubject();
 

#

1 //获取主题对象 2 public static Subject getSubject() {3     Subject subject = ThreadContext.getSubject();//第一次null4     if (subject == null) {5        subject = (new Subject.Builder()).buildSubject();6        ThreadContext.bind(subject);7         }8       return subject;9 }
 

(1)程式碼分析:使用建造者模式建立物件:

 1 public static class Builder{ 2        SubjectContext subjectContext; 3        SecurityManager securityManager; 4         public Builder(SecurityManager securityManager) { 5             if (securityManager == null) { 6                 throw new NullPointerException("SecurityManager method argument cannot be null."); 7             } 8             this.securityManager = securityManager; 9             this.subjectContext = newSubjectContextInstance();//DefaultSubjectContext(初始化一个backMap集合)10             if (this.subjectContext == null) {11                 throw new IllegalStateException("Subject instance returned from 'newSubjectContextInstance' " +12                         "cannot be null.");13             }14             this.subjectContext.setSecurityManager(securityManager);15         }16        public Subject buildSubject() {17             return this.securityManager.createSubject(this.subjectContext);18         }19 }

 (2)使用主題上下文建立主題

#
 1   public Subject createSubject(SubjectContext subjectContext) { 2         //create a copy so we don't modify the argument's backing map: 3         SubjectContext context = copy(subjectContext); 4  5         //ensure that the context has a SecurityManager instance, and if not, add one: 6         context = ensureSecurityManager(context);//DefaultSubjectContext.backMap.put(SecurityManage) 7  8         //Resolve an associated Session (usually based on a referenced session ID), and place it in the context before 9         //sending to the SubjectFactory.  The SubjectFactory should not need to know how to acquire sessions as the10         //process is often environment specific - better to shield the SF from these details:11         context = resolveSession(context);12 13         //Similarly, the SubjectFactory should not require any concept of RememberMe - translate that here first14         //if possible before handing off to the SubjectFactory:15         context = resolvePrincipals(context);16 17         Subject subject = doCreateSubject(context);18 19         //save this subject for future reference if necessary:20         //(this is needed here in case rememberMe principals were resolved and they need to be stored in the21         //session, so we don't constantly rehydrate the rememberMe PrincipalCollection on every operation).22         //Added in 1.2:23         save(subject);24 25         return subject;26     }
### (3)透過主題建立subject物件######
 protected Subject doCreateSubject(SubjectContext context) {return getSubjectFactory().createSubject(context);
    }
###

 

(4)DefaultSubjectFactory创建主题对象:
 1    public Subject createSubject(SubjectContext context) { 2         SecurityManager securityManager = context.resolveSecurityManager(); 3         Session session = context.resolveSession(); 4         boolean sessionCreationEnabled = context.isSessionCreationEnabled(); 5         PrincipalCollection principals = context.resolvePrincipals(); 6         boolean authenticated = context.resolveAuthenticated(); 7         String host = context.resolveHost(); 8 9         return new DelegatingSubject(principals, authenticated, host, session, sessionCreationEnabled, securityManager);10     }

以上是shiro源碼的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在平台獨立性的平台獨立性上使用字節碼優於本機代碼的優點是什麼?在平台獨立性的平台獨立性上使用字節碼優於本機代碼的優點是什麼?Apr 30, 2025 am 12:24 AM

ByteCodeachievesPlatFormIndenceByByByByByByExecutedBoviratualMachine(VM),允許CodetorunonanyplatformwithTheApprepreprepvm.Forexample,Javabytecodecodecodecodecanrunonanydevicewithajvm

Java真的100%獨立於平台嗎?為什麼或為什麼不呢?Java真的100%獨立於平台嗎?為什麼或為什麼不呢?Apr 30, 2025 am 12:18 AM

Java不能做到100%的平台獨立性,但其平台獨立性通過JVM和字節碼實現,確保代碼在不同平台上運行。具體實現包括:1.編譯成字節碼;2.JVM的解釋執行;3.標準庫的一致性。然而,JVM實現差異、操作系統和硬件差異以及第三方庫的兼容性可能影響其平台獨立性。

Java的平台獨立性如何支持代碼可維護性?Java的平台獨立性如何支持代碼可維護性?Apr 30, 2025 am 12:15 AM

Java通過“一次編寫,到處運行”實現平台獨立性,提升代碼可維護性:1.代碼重用性高,減少重複開發;2.維護成本低,只需一處修改;3.團隊協作效率高,方便知識共享。

為新平台創建JVM面臨哪些挑戰?為新平台創建JVM面臨哪些挑戰?Apr 30, 2025 am 12:15 AM

在新平台上創建JVM面臨的主要挑戰包括硬件兼容性、操作系統兼容性和性能優化。 1.硬件兼容性:需要確保JVM能正確使用新平台的處理器指令集,如RISC-V。 2.操作系統兼容性:JVM需正確調用新平台的系統API,如Linux。 3.性能優化:需進行性能測試和調優,調整垃圾回收策略以適應新平台的內存特性。

Javafx庫如何試圖解決GUI開發中的平台不一致?Javafx庫如何試圖解決GUI開發中的平台不一致?Apr 30, 2025 am 12:01 AM

javafxeffectife addressemanddressEndressencissencies uningusement insuplatform-agnosticsCenegraphandCsSsStyling.1)itabstractsplactsplatsplatsplatsplatsplatformsthroughascenegraph,確保consistentertrenderingrenderingrenderingacrosswindows,macoswindwind,Macos,MacOs.2)

說明JVM如何充當Java代碼和基礎操作系統之間的中介。說明JVM如何充當Java代碼和基礎操作系統之間的中介。Apr 29, 2025 am 12:23 AM

JVM的工作原理是將Java代碼轉換為機器碼並管理資源。 1)類加載:加載.class文件到內存。 2)運行時數據區:管理內存區域。 3)執行引擎:解釋或編譯執行字節碼。 4)本地方法接口:通過JNI與操作系統交互。

解釋Java虛擬機(JVM)在Java平台獨立性中的作用。解釋Java虛擬機(JVM)在Java平台獨立性中的作用。Apr 29, 2025 am 12:21 AM

JVM使Java實現跨平台運行。 1)JVM加載、驗證和執行字節碼。 2)JVM的工作包括類加載、字節碼驗證、解釋執行和內存管理。 3)JVM支持高級功能如動態類加載和反射。

您將採取哪些步驟來確保Java應用程序在不同的操作系統上正確運行?您將採取哪些步驟來確保Java應用程序在不同的操作系統上正確運行?Apr 29, 2025 am 12:11 AM

Java應用可通過以下步驟在不同操作系統上運行:1)使用File或Paths類處理文件路徑;2)通過System.getenv()設置和獲取環境變量;3)利用Maven或Gradle管理依賴並測試。 Java的跨平台能力依賴於JVM的抽象層,但仍需手動處理某些操作系統特定的功能。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境