Shiro入門視頻課程——筆記(一)
阿新 • • 發佈:2018-08-06
安全管理 count 退出 classpath 文件 技術分享 size 三種 tro 視頻課程鏈接:http://edu.51cto.com/course/14122.html
Shiro權限框架,主講:湯小洋
一、Shiro簡介
1. 什麽是Shiro
? Apache Shiro是一個強大且易用的Java安全框架,執行身份驗證、授權、密碼學和會話管理。
2. 基本功能
? 身份認證、授權、加密、會話管理
3. 單詞
? authentication 身份認證
? authorization 授權
? strategy 策略
二、身份認證
1. 認證流程圖
2. 執行過程
? 分為五步:
-
Subject
用戶主體:請求的發起者,即訪問應用的用戶
-
SecurityManager
安全管理器:Shiro的核心,用來分發請求
-
Authenticator
認證器:用來進行認證操作
-
Authentication Strategy
認證策略,針對多個Realm
-
Realm
安全數據源:用來進行數據匹配的,可以通過多種數據源進行匹配認證,如文件、數據庫、QQ、微信、手機號等
三、第一個Shiro程序
? 登陸和退出
1. 添加jar包
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro-core.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency>
2. 核心配置文件
? Shiro使用的是INI格式的配置文件,鍵值對的配置,可以分類進行配置
#配置用戶信息
[users]
admin=123
tom=456
3. 用法
public static void main(String[] args) { //1.獲取SecurityManager,指定配置文件初始化 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro01/shiro.ini"); SecurityManager securityManager = factory.getInstance(); //2.對securityManager進行封裝,綁定給SecurityUtils SecurityUtils.setSecurityManager(securityManager); //3.獲取用戶主體Subject Subject subject = SecurityUtils.getSubject(); //4.登陸 System.out.println("是否認證:"+subject.isAuthenticated()); UsernamePasswordToken token = new UsernamePasswordToken("admin", "456"); try { subject.login(token); } catch(UnknownAccountException e){ System.out.println("未知的賬戶!e:"+e.getMessage()); } catch (IncorrectCredentialsException e){ System.out.println("錯誤的密碼!e:"+e.getMessage()); } catch (AuthenticationException e) { System.out.println("認證異常!e:"+e.getMessage()); } System.out.println("是否認證:"+subject.isAuthenticated()); //5.退出 subject.logout(); System.out.println("是否認證:"+subject.isAuthenticated()); }
4. 默認的配置
#默認的SecurityManager
securityManager=org.apache.shiro.mgt.DefaultSecurityManager
#默認的認證器
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
securityManager.authenticator=$authenticator
#默認的認證策略
authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy
authenticator.authenticationStrategy=$authenticationStrategy
#默認的Realm
iniRealm=org.apache.shiro.realm.text.IniRealm
securityManager.realms=$iniRealm
四、Realm
? 默認有三種Realm:IniReaml、JdbcRealm、PropertiesRealm
1. 使用JdbcRealm
#配置數據源
dataSource=com.alibaba.druid.pool.DruidDataSource
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf8
dataSource.username=root
#使用JdbcRealm
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.dataSource=$dataSource
#重寫認證的查詢語句
jdbcRealm.authenticationQuery=select password from t_user where login_name=?
securityManager.realms=$jdbcRealm
2. 自定義Realm
? 步驟:
- 自定義一個類,實現Realm接口,或繼承AuthorizingRealm父類
- 配置shiro.ini
五、認證策略
1. 簡介
? 針對多個Realm,可以對認證Realm的個數進行配置
? 三種認證策略:
-
AtLeastOneSuccessfulStrategy 默認
只要有一個Realm驗證成功即可,返回所有Realm身份驗證成功的認證信息
-
FirstSuccessfulStrategy
只要有一個Realm驗證成功即可,返回第一個Realm身份認證成功的認證信息
-
AllSuccessfulStrategy
所有Realm都驗證成功才算成功
? 註:三種認證策略都會對所有Realm進行匹配
2. 用法
#認證策略
#authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy
#authenticationStrategy=org.apache.shiro.authc.pam.FirstSuccessfulStrategy
authenticationStrategy=org.apache.shiro.authc.pam.AllSuccessfulStrategy
#認證器
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
authenticator.authenticationStrategy=$authenticationStrategy
securityManager.authenticator=$authenticator
securityManager.realms=$propertiesRealm,$jdbcRealm
Shiro入門視頻課程——筆記(一)