先在pom中配置依賴
org.apache.shiro
shiro-core
1.4.0
junit
junit
release
新建測試類authenticationtest
import org.apache.shiro.securityutils;
import org.apache.shiro.authc.usernamepasswordtoken;
import org.apache.shiro.mgt.defaultsecuritymanager;
import org.apache.shiro.realm.******accountrealm;
import org.apache.shiro.subject.subject;
import org.junit.before;
import org.junit.test;
public class authenticationtest
@test
public void testauthentication()
}
測試結果
從**中分析認證邏輯
在認證的**上修改
import org.apache.shiro.securityutils;
import org.apache.shiro.authc.usernamepasswordtoken;
import org.apache.shiro.mgt.defaultsecuritymanager;
import org.apache.shiro.realm.******accountrealm;
import org.apache.shiro.subject.subject;
import org.junit.before;
import org.junit.test;
public class authenticationtest
@test
public void testauthentication()
}
從**中分析授權邏輯
1.建立自定義的customrealm
customrealm繼承authorizingrealm類,該類有dogetauthorizationinfo()、dogetauthenticationinfo()方法。dogetauthorizationinfo()用來編寫授權邏輯,dogetauthenticationinfo()方法用來編寫認證邏輯。
public class customrealm extends authorizingrealm
//認證邏輯
protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception
}
2.編寫認證邏輯,因為是簡單的測試,所以沒有連線資料庫,資料庫用map來模擬
public class customrealm extends authorizingrealm
//授權邏輯
protected authorizationinfo dogetauthorizationinfo(principalcollection principalcollection)
//認證邏輯
protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception
******authenticationinfo authenticationinfo=new ******authenticationinfo
("mark",password,"customrealm");
return authenticationinfo;
}/**
* 模擬資料庫查詢憑證
* @param username
* @return
*/private string getpasswordbyusername(string username)
}
3.編寫測試類customrealmtest
}4.測試結果
5.編寫授權邏輯
//授權邏輯
protected authorizationinfo dogetauthorizationinfo(principalcollection principalcollection)
/** * 模擬資料庫查詢角色許可權
* @param username
* @return
*/private setgetpermissionsbyusername(string username)
/** * 模擬資料庫查詢角色
* @param username
* @return
*/private setgetrolesbyusername(string username)
6.測試
@test
public void testauthentication()
結果
把user:add改為user:update,結果為
shiro認證授權
1.shiro配置類 1.1需要配置什麼?建立乙個shirofilte ctorybean物件,檢測認證 向bean中傳入securitymanager物件 沒有認證的時候應該訪問哪個url位址 setloginurl 哪些資源是可以被匿名訪問的 setfilterchaindefinitionma...
Shiro 認證與授權分析
使用者訪問系統資源時的認證流程如下 具體流程分析如下 1 系統呼叫subject的login方法將使用者資訊提交給securitymanager 2 securitymanager將認證操作委託給認證器物件authenticator 3 authenticator將身份資訊傳遞給realm。4 re...
shiro 實現認證授權
一.認證和授權的概念 認證 authentication show it,you are you 授權 authoriziation through it,you can do sth 二.shiro中的認證與授權 authenticatingrealm shiro中的用於進行認證的領域,實現dog...