(二)shiro認證

認證是用戶來證明他們的身份。

subject認證主體,它包含有兩個重要信息:

principals:身份,可以是用戶名,郵件,手機號碼等等,用來表示一個登陸主體身份

Creadentials:憑證,常見有密碼,數字證書等。


shiro處理認證流程


第一步:調用Subject.login 方法

第二步:Security Manager負責驗證,這是認證起始的地方。

第三步:Authenticator是真正的驗證步驟

第四步:Authenticator可能會委託給相應的AuthenticationStrategy進行多Realm身份驗證,默認ModularRealmAuthenticator會調用AuthenticationStrategy進行多Realm身份驗證

第五步:Authenticator會把相應的token傳入Realm,從Realm獲取身份驗證信息,如果沒有返回/拋出異常表示身份驗證失敗了。此處可以配置多個Realm,將按照相應的順序及策略進行訪問。


Realm:意思是域,ShiroRealm 中獲取驗證數據;
Realm 有很多種類,例如常見的jdbc realmjndi realmtext realm 


實例演示

爲了實現真實數據庫驗證,在數據庫中創建一個數據庫命名爲db_shiro,創建表名字爲users,固定寫法否則會報錯。

字段如下


1.創建一個maven項目,並在pom.xml文件中,並配置需要用到的jar包

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.liy.shiro</groupId>
  <artifactId>shiro01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>shiro</name>
  <description>shiro</description>
  
  <dependencies>
  <!-- shiro-core -->
	<dependency>
    	<groupId>org.apache.shiro</groupId>
    	<artifactId>shiro-core</artifactId>
    	<version>1.3.2</version>
	</dependency>
  
  <!-- slf4j-log4j12 -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.21</version>
</dependency>
  
  <!-- c3p0 -->
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>
  
  <!-- commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

<!-- mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.40</version>
</dependency>  
  </dependencies>
</project>

這裏需要數據源c3p0、MySQL的jdbc驅動、commons-logging以及shiro所需要的jar包


2.配置ini文件

命名爲jdbc_realm.ini

[main]
#聲明一個realm
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
#獲取數據源
dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
#配置數據源的基本信息
dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/db_shiro
dataSource.user=root
#密碼爲空用""代替,否則不能構成一個鍵值對
dataSource.password=""
#jdbcRealm的數據源設置信息
jdbcRealm.dataSource=$dataSource
#把jdbcRealm賦給securityManager
securityManager.realms=$jdbcRealm
格式:

1.變量名  = 全類名

2.變量名.屬性 = 爲屬性賦值,會調用set方法

3.$變量名 引用之前的一個對象實例 

3.java代碼

package com.liy.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

public class JdbcRealmTest {

	public static void main(String[] args) {
		//讀取配置文件,初始化SecurityManager工廠
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:jdbc_realm.ini");
		//獲取SecurityManager實例
		SecurityManager securityManager=factory.getInstance();
		//把securityManager實例幫定到SecurityUtils中
		SecurityUtils.setSecurityManager(securityManager);
		//得到當前執行的用戶
		Subject currentUser=SecurityUtils.getSubject();
		//創建token用戶令牌
		UsernamePasswordToken token = new UsernamePasswordToken("liy", "123");
		try {
			//身份認證
			currentUser.login(token);
		
			System.out.println("用戶驗證成功");
		} catch (AuthenticationException e) {
			e.printStackTrace();
			System.out.println("用戶驗證失敗");
		}
		
		currentUser.logout();
	}
}


重點:引jar包,配置ini文件,數據庫表users,java代碼的編寫




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章