Shiro 許可權框架的認識和使用

2021-09-10 21:51:08 字數 3252 閱讀 7521

許可權框架(提供的易用的api,功能強大)

框架

shiro

spring security

易用性√x粒度

粗細(強大)

身份驗證、授權、密碼學和會話管理

securitymanager:核心物件 realm:獲取資料介面

//一.建立我們自己的realm

myrealm myrealm = new myrealm();

//二.搞乙個核心物件:

defaultsecuritymanager securitymanager = new defaultsecuritymanager();

securitymanager.setrealm(myrealm);

//三.把securitymanager放到上下文中

securityutils.setsecuritymanager(securitymanager);

//1.拿到當前使用者

subject currentuser = securityutils.getsubject();

//2.判斷是否登入

currentuser.isauthenticated();

//3.登入(需要令牌的)

/** unknownaccountexception:使用者名稱不存在

incorrectcredential***ception:密碼錯誤

authenticationexception:其它錯誤

*/ usernamepasswordtoken token = new usernamepasswordtoken("admin", "123456");

currentuser.login(token);

//4.判斷是否是這個角色/許可權

currentuser.hasrole("角色名")

currentuser.ispermitted("許可權名")

/**

* string algorithmname, object source, object salt, int ha****erations)

* 第乙個引數algorithmname:加密演算法名稱

* 第二個引數source:加密原密碼

* 第三個引數salt:鹽值

* 第四個引數ha****erations:加密次數

*/******hash hash = new ******hash("md5","123456","itsource",10);

system.out.println(hash.tohex());

繼承authorizingrealm

實現兩個方法:dogetauthorizationinfo(授權) /dogetauthenticationinfo(登入認證)

//身份認證

@override

protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception

//返回認證資訊

//準備鹽值

bytesource salt = bytesource.util.bytes("itsource");

//密碼是shiro自己進行判斷

******authenticationinfo authenticationinfo = new ******authenticationinfo(username,password,salt,getname());

return authenticationinfo;

}

//授權

@override

protected authorizationinfo dogetauthorizationinfo(principalcollection principalcollection)

注意:如果我們的密碼加密,應該怎麼判斷(匹配器)

//一.建立我們自己的realm

myrealm myrealm = new myrealm();

//建立乙個憑證匹配器(無法設定鹽值)

hashedcredentialsmatcher matcher = new hashedcredentialsmatcher();

// 使用md5的方式比較密碼

matcher.sethashalgorithmname("md5");

// 設定編碼的迭代次數

matcher.setha****erations(10);

//設定憑證匹配器(加密方式匹配)

myrealm.setcredentialsmatcher(matcher);

去找:shiro-root-1.4.0-rc2\samples\spring

org.apache.shiro

shiro-all

1.4.0

pom

org.apache.shiro

shiro-spring

1.4.0

這個過濾器是乙個**(只關注它的名稱)

shirofilter

org.springframework.web.filter.delegatingfilterproxy

targetfilterlifecycle

true

/*

是從案例中拷備過來,進行了相應的修改

<?xml version="1.0" encoding="utf-8"?>

5.4 獲取map過濾

注意,返回的map必需是有序的(linkedhashmap)

public class filterchaindefinitionmapfactory 

}

許可權框架 shiro 授權demo

之前說了許可權認證,其實也就是登入驗證身份 這次來說說shiro的授權 shiro可以針對角色授權,或者訪問資源授權 兩者都行,但是在如今的複雜系統中,當然使用後者,如果你是小系統或者私活的話,前者即可,甚至可以不用,我懂的 好吧,上 首先新建乙個ini,登陸資訊以及許可權配置好 1 使用者2 us...

shiro框架許可權模組細節

以前登入成功,傳的是username,現在傳employee物件 身份認證 override protected authenticationinfo dogetauthenticationinfo authenticationtoken authenticationtoken throws aut...

許可權框架 shiro 授權demo

之前說了許可權認證,其實也就是登入驗證身份 這次來說說shiro的授權 shiro可以針對角色授權,或者訪問資源授權 兩者都行,但是在如今的複雜系統中,當然使用後者,如果你是小系統或者私活的話,前者即可,甚至可以不用,我懂的 好吧,上 首先新建乙個ini,登陸資訊以及許可權配置好 1 使用者2 us...