欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

什么是Shiro

這篇文章主要講解了“什么是Shiro”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“什么是Shiro”吧!

公司主營(yíng)業(yè)務(wù):做網(wǎng)站、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。成都創(chuàng)新互聯(lián)推出前進(jìn)免費(fèi)做網(wǎng)站回饋大家。

Shiro 介紹

Shiro項(xiàng)目的全名是Apache Shiro,后續(xù)簡(jiǎn)稱Shiro,是一個(gè)關(guān)于安全的框架,在Shiro官網(wǎng)具體介紹如下:

Apache Shiro?is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management. With Shiro’s easy-to-understand API, you can quickly and easily secure any application – from the smallest mobile applications to the largest web and enterprise applications.

from: https://shiro.apache.org/

在這段介紹中可以看出Shiro是一個(gè)功能強(qiáng)大且易用的Java安全框架,它包含了身份驗(yàn)證、授權(quán)、加密和會(huì)話管理等功能。并且Shiro具有易于理解的API,可以快速輕松的保護(hù)任何應(yīng)用程序。

為什么使用Shiro

本節(jié)將解釋為什么使用Shiro框架,在使用Shiro的主要原因中包含如下幾點(diǎn):

  1. 易于使用:在Shiro中因?yàn)榫邆湟子诶斫獾腁PI定義和優(yōu)秀的抽象從而使開發(fā)在在使用Shiro時(shí)十分方便易于上手。

  2. 全面:Shiro提供了關(guān)于權(quán)限認(rèn)證的各類基礎(chǔ)模型定義并對(duì)其進(jìn)行了抽象涵蓋的內(nèi)容相對(duì)全面。

  3. 靈活:Shiro幾乎可以在任何程序環(huán)境中執(zhí)行工作,包括且不限于Java開發(fā)中的Spring、EJB等。

Shiro 處理流程

本節(jié)將對(duì)Shiro的處理流程做簡(jiǎn)單說(shuō)明,在Shiro官方文檔中可以搜索到下圖:

什么是Shiro

from:https://shiro.apache.org/architecture.html

在上圖中可以發(fā)現(xiàn)Shiro中整體處理流程是通過(guò)Subject對(duì)象、SecurityManager對(duì)象和Realm對(duì)象組成。

  1. Subject對(duì)象可以理解為用戶。

  2. SecurityManager對(duì)象可以理解為安全管理器,主要目的是管理Subject對(duì)象。

  3. Realm對(duì)象可以理解為權(quán)限資源。

Shiro 架構(gòu)說(shuō)明

本節(jié)將對(duì)Shiro的架構(gòu)進(jìn)行說(shuō)明,在Shiro的官方文檔中可以搜索到下圖。

什么是Shiro

from:https://shiro.apache.org/architecture.html

在上圖中可以發(fā)現(xiàn)在Shiro架構(gòu)中分為如下幾個(gè)內(nèi)容:

  1. Subject:主題。一般情況下是用戶。

  2. Security Manager:安全管理器,它是整個(gè)Shiro的核心

  3. Authenticator:認(rèn)證器。

    1. Authentication Strategy:認(rèn)證策略。

  4. Authorizer:授權(quán)器。

  5. SessionManager:Session 管理器。

    1. SessionDAO:Session 持久操作

  6. CacheManager:緩存管理器

  7. Cryptography:密碼學(xué)相關(guān)內(nèi)容,主要負(fù)責(zé)加密解密。

  8. Realms:領(lǐng)域,權(quán)限。

在Shiro中主要的對(duì)象就是上面八個(gè),在后續(xù)的開發(fā)過(guò)程中會(huì)經(jīng)常使用到,它們組成了Shiro的各種功能

認(rèn)證順序

下面將對(duì)Shiro的基本認(rèn)證過(guò)程做說(shuō)明,在Shiro的官網(wǎng)可以看到如下圖片:

什么是Shiro

from: https://shiro.apache.org/authentication.html

從上圖中可以發(fā)現(xiàn)關(guān)于Shiro的認(rèn)證過(guò)程包含五個(gè)步驟:

  1. 應(yīng)用程序調(diào)用Subject.login方法將token作為參數(shù)構(gòu)件Subject對(duì)象。

  2. 將步驟一中得到的Subject對(duì)象傳遞到SecurityManager中。

  3. 在SecurityManager中將認(rèn)證交給Authenticator對(duì)象處理。

  4. 在Authenticator中具體的處理是由AuthenticationStrategy認(rèn)證策略進(jìn)行實(shí)際處理。

  5. 在AuthenticationStrategy處理過(guò)程中回去尋找Realm數(shù)據(jù)信息對(duì)其進(jìn)行認(rèn)證。

授權(quán)順序

下面將對(duì)Shiro的基本授權(quán)過(guò)程做說(shuō)明,在Shiro的官網(wǎng)可以看到如下圖片:

什么是Shiro

from: https://shiro.apache.org/authorization.html

從上圖中可以發(fā)現(xiàn)關(guān)于Shiro的授權(quán)過(guò)程包含四個(gè)步驟:

  1. Subject對(duì)象調(diào)用isPermitted*hasRole*checkRole*checkPermission*方法。

  2. SecurityManager進(jìn)行授權(quán)判斷,具體處理將委托Authorizer類進(jìn)行。

  3. 在Authorizer處理過(guò)程中會(huì)依賴Realm相關(guān)操作。

  4. Realm檢查相關(guān)配置。

第一個(gè) Shiro 程序

在前文對(duì)Shiro做了一些基本概念的介紹,本節(jié)將使用Shiro搭建一個(gè)簡(jiǎn)單的用例,主要實(shí)現(xiàn)認(rèn)證相關(guān)處理操作。在確認(rèn)目標(biāo)后需要率先選擇Shiro的版本,在本文編寫期間Shiro的版本更新到了1.7.1,具體maven依賴如下:

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.7.1</version>
</dependency>

關(guān)于JDK的版本具體信息如下:

openjdk version "1.8.0_282"
OpenJDK Runtime Environment Corretto-8.282.08.1 (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM Corretto-8.282.08.1 (build 25.282-b08, mixed mode)

在完成這基本的技術(shù)選型后需要進(jìn)行工程創(chuàng)建,本例采用的是maven進(jìn)行管理,具體的POM文件內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>shiro-book</artifactId>
        <groupId>com.github.huifer</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>just-shiro</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.7.1</version>
        </dependency>

    </dependencies>
    <build>
       <resources>
           <resource>
               <directory>src/main/resources</directory>
               <includes>
                   <include>**/*.ini</include>
               </includes>
           </resource>
       </resources>
    </build>
</project>

注意: 這里采用的是父子工程,如果不采用父子工程請(qǐng)直接使用下面兩個(gè)依賴

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-core</artifactId>
        <version>1.7.1</version>
    </dependency>

</dependencies>
<build>
   <resources>
       <resource>
           <directory>src/main/resources</directory>
           <includes>
               <include>**/*.ini</include>
           </includes>
       </resource>
   </resources>
</build>

在工程創(chuàng)建完成后需要先編寫一個(gè)配置文件,該配置文件用于存放賬號(hào)密碼,文件名可以任意設(shè)置,在本例中使用的文件名為shiro.ini,該文件需要放在resources文件夾下,具體內(nèi)容如下:

[users]
admin=admin
userAdd=userAdd

在該文件中定義了兩個(gè)用戶,等號(hào)左側(cè)是用戶名,等號(hào)右側(cè)是密碼,在后續(xù)的測(cè)試用例中將會(huì)使用。下面編寫一個(gè)測(cè)試方法,具體代碼如下:

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

public class TestAuthenticator {
  public static void main(String[] args) {
    // 1. 創(chuàng)建安全管理器對(duì)象
    DefaultSecurityManager securityManager = new DefaultSecurityManager();
    // 2. 設(shè)置 realm 數(shù)據(jù)
    securityManager.setRealm(new IniRealm("classpath:shiro.ini"));
    // 3. 設(shè)置安全工具類相關(guān)數(shù)據(jù)
    SecurityUtils.setSecurityManager(securityManager);
    // 4. 從安全工具類中獲取 subject 對(duì)象
    Subject subject = SecurityUtils.getSubject();
    // 5. 創(chuàng)建令牌
    UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("admin", "admin");
    // 6. 登陸
    // 認(rèn)證狀態(tài)
    boolean authenticated = subject.isAuthenticated();
    System.out.println("登錄前的認(rèn)證狀態(tài)" + authenticated);
    subject.login(usernamePasswordToken);
    authenticated = subject.isAuthenticated();
    System.out.println("登錄后的認(rèn)證狀態(tài)" + authenticated);
  }
}

在邊編寫完成上輸代碼后可以嘗試執(zhí)行改代碼,執(zhí)行結(jié)果如下:

登錄前的認(rèn)證狀態(tài)false
登錄后的認(rèn)證狀態(tài)true

在這個(gè)簡(jiǎn)單的Shiro用例中主要執(zhí)行的操作有如下六個(gè)步驟:

  1. 創(chuàng)建默認(rèn)的安全管理器對(duì)象,其本質(zhì)實(shí)際上是SecurityManager。

  2. 設(shè)置Realm數(shù)據(jù),本例采用的是靜態(tài)配置。

  3. 安全工具類中設(shè)置安全管理器。

  4. 從安全工具類中提取Subject對(duì)象。

  5. 創(chuàng)建令牌,此時(shí)的令牌采用的是賬號(hào)密碼的形式。

  6. 進(jìn)行登陸操作,主要執(zhí)行subject.login方法,參數(shù)是令牌。

通過(guò)上述六個(gè)操作可以制作一個(gè)簡(jiǎn)單的用戶登陸驗(yàn)證,下面將模擬用戶名錯(cuò)誤,密碼錯(cuò)誤從而了解Shiro中的兩個(gè)基本異常。首先將UsernamePasswordToken構(gòu)造器中的第一個(gè)參數(shù)用戶名任意修改,修改后內(nèi)容如下:

UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("admin1", "admin");

在修改后執(zhí)行程序控制臺(tái)輸出內(nèi)容如下:

Exception in thread "main" org.apache.shiro.authc.UnknownAccountException: Realm [org.apache.shiro.realm.text.IniRealm@5b80350b] was unable to find account data for the submitted AuthenticationToken [org.apache.shiro.authc.UsernamePasswordToken - admin1, rememberMe=false].
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:184)
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:273)
	at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
	at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
	at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:275)
	at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:260)
	at com.github.huifer.shiro.TestAuthenticator.main(TestAuthenticator.java:31)

此時(shí)可以發(fā)現(xiàn)出現(xiàn)了異常,該異常表示的就是用戶名錯(cuò)誤,具體異常對(duì)象是UnknownAccountException。下面將模擬密碼錯(cuò)誤的異常,將UsernamePasswordToken構(gòu)造器中的第二個(gè)參數(shù)密碼任意修改,修改后內(nèi)容如下:

UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("admin", "admin1");

在修改后執(zhí)行程序控制臺(tái)輸出內(nèi)容如下:

Exception in thread "main" org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - admin, rememberMe=false] did not match the expected credentials.
	at org.apache.shiro.realm.AuthenticatingRealm.assertCredentialsMatch(AuthenticatingRealm.java:603)
	at org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo(AuthenticatingRealm.java:581)
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180)
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:273)
	at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
	at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
	at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:275)
	at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:260)
	at com.github.huifer.shiro.TestAuthenticator.main(TestAuthenticator.java:31)

此時(shí)可以發(fā)現(xiàn)出現(xiàn)了異常,該異常表示的就是密碼驗(yàn)證錯(cuò)誤,具體異常對(duì)象是IncorrectCredentialsException。

感謝各位的閱讀,以上就是“什么是Shiro”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)什么是Shiro這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

網(wǎng)頁(yè)題目:什么是Shiro
文章分享:http://chinadenli.net/article42/jhggec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序標(biāo)簽優(yōu)化網(wǎng)站營(yíng)銷靜態(tài)網(wǎng)站電子商務(wù)網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)