Spring中shiro的簡單使用

2021-10-06 03:24:34 字數 3972 閱讀 1750

先將一下shiro工作的乙個流程,**示例在後面,想要直接看**的朋友可以直接下滑。

shrio認證流程

1、通過配置檔案建立 securitymanager類。

2、使用者登入時 建立 usernamepasswordtoken 類的乙個物件(token)。

3、通過securityutils.getsubject().login(token) 方法 提交認證。

4、securitymanager 由 realmauthentication 進行認證。

5、realmauthentication 呼叫自定義的realm物件(傳入token)。

6、real物件根據傳入的token 根據賬號查詢使用者資訊。

​ 1> 查詢到 -> 將使用者資訊返回。

​ 2> 否則返回null

7、realmauthentication 接收到realm 返回的資訊

​ 1> null 丟擲 unknownaccountexception

​ 2> 找到使用者(1、realm返回的密碼與token對比 ,若不相同則返回incorrectcredential***ception)

shiro授權流程

1、構建securitymanager。

2、subject.ispermitted()授權。

3、securitymanager.ispermitted()、執行授權。

4、authorizer 執行授權。

5、根據身份獲取資源許可權資訊。

1、對subject授權 呼叫ispermitted(「permission串」)方法。

2、securitymanager執行授權–> 通過modularrealmauthorizer 執行。

3、modularrealmauthorizer 執行 自定義realm從資料庫查詢許可權資料。呼叫realm的授權方法:dogetauthorizationinfo()。

4、realm 將許可權資料返回給realmauthorizer

5、realmauthorizer呼叫permissionreslover 進行許可權串對比。

6、對吧ispermitted中「permission串」 是否在realm查詢的許可權資料中。

**示例

1、shiro配置類

@configuration

public

class

shiroconfig

// 2. 配置securitymanager

@bean

public defaultwebsecuritymanager securitymanager()

//3. 配置自定義的realm

@bean

public userrealm userrealm()

//3.1 配置salt加密

@bean

public hashedcredentialsmatcher hashedcredentialsmatcher()

//4.初始化shiro的生命週期

@bean

public lifecyclebeanpostprocessor lifecyclebeanpostprocessor()

/** * setuseprefix(false)用於解決乙個奇怪的bug。在引入spring aop的情況下。

* 在@controller註解的類的方法中加入@requiresrole等shiro註解,會導致該方法無法對映請求,

* 導致返回404。加入這項配置能解決這個bug

*/@bean

public authorizationattributesourceadvisor authorizationattributesourceadvisor()

@bean

@dependson

("lifecyclebeanpostprocessor"

)public

static defaultadvisorautoproxycreator getdefaultadvisorautoproxycreator()

}

2、建立自定義的realm類
public

class

userrealm

extends

authorizingrealm

******authorizationinfo ******authorizationinfo =

new******authorizationinfo()

; ******authorizationinfo.

addroles

(rolenames)

;//同時,還和獲取使用者對應的許可權集合

arraylist<>());

logger.

info

("------------授權成功!!-------------");

return ******authorizationinfo;

}@override

protected authenticationinfo dogetauthenticationinfo

(authenticationtoken authenticationtoken)

throws authenticationexception

else

}return

new******authenticationinfo

(xxuser, xxuser.

getpassword()

, bytesource.util.

bytes

(xxuser.

getusername()

),getname()

);}//得到密碼

private

static string ge***5password

(string password,string saltstr)

public

static

void

main

(string[

] args)

}

3、登入時檢驗

usernamepasswordtoken token =

newusernamepasswordtoken

((string)parammap.

get(

"username"),

(string)parammap.

get(

"password"))

;/*下面這段**進行異常處理,若產生異常則表示登入失敗。

*/securityutils.

getsubject()

.login

(token)

;//這一步操作成功,則表示使用者登入成功

4、介面許可權管理

上面提到已經將當前使用者的角色和許可權獲取到。

在介面上使用註解

//對角色控制

@requiresroles

(value =

,logical = logical.or)

//對許可權控制

@requirespermissions

(value =

,logical = logical.and)

如果有多個許可權/角色驗證的時候中間用「,」隔開,預設是所有列出的許可權/角色必須同時滿足才生效。但是在註解中有logical = logical.or這塊。這裡可以讓許可權控制更靈活些。

如果將這裡設定成or,表示所列出的條件只要滿足其中乙個就可以,如果不寫或者設定成logical = logical.and,表示所有列出的都必須滿足才能進入方法。

//需要登入

@requiresauthentication

//無需登入

@requiresguest

這有一篇博文,作者已經詳細講解了shiro關於許可權控制的5個註解

shiro與spring的整合

1.除了spring本身專案的jar包,還要加入四個jar包 1 shiro all 1.2.5.jar 2 slf4j api 1.7.7.jar 3 slf4j log4j12 1.7.7.jar 4 log4j 1.2.17.jar 2.首先進行web.xml的配置 參考官方的文件 需要在we...

基於Spring框架的Shiro配置方法

一 在web.xml中新增shiro過濾器 shirofilter org.springframework.web.filter.delegatingfilterproxy shirofilter 二 在spring的applicationcontext.xml中新增shiro配置 1 新增shir...

Shiro概述,簡單使用

subject 主體,代表了當前 使用者 通過subject與shiro框架進行互動。securitymanager 安全管理器,shiro框架的核心,負責排程整個框架的執行 realm 域,shiro從realm獲取安全資料 如使用者 角色 許可權 就是說securitymanager要驗證使用者...