前言
上一篇博客,我們說(shuō)到如何在用戶請(qǐng)求的時(shí)候,攔截用戶請(qǐng)求進(jìn)行操作,這一篇博客我們來(lái)聊一下如何使用Shiro標(biāo)簽在網(wǎng)頁(yè)之中進(jìn)行用戶角色和權(quán)限控制.那么,我們就看一下具體的場(chǎng)景,假設(shè)頁(yè)面當(dāng)中有一個(gè)新增用戶按鈕,我們需要當(dāng)擁有admin角色的用戶登錄認(rèn)證完成之后顯示,普通用戶不顯示,那么這時(shí)候我們就需要使用Shiro標(biāo)簽來(lái)做處理了.例如這個(gè)例子,我們就可以用一下代碼來(lái)實(shí)現(xiàn).如果含有admin就會(huì)顯示,反之,則不會(huì)顯示.
<shiro:hashRole name = "admin">
<button >新增用戶</button>
</shiro:hashRole>
JSP的Shiro標(biāo)簽
在JSP中使用Shiro標(biāo)簽比較簡(jiǎn)單,我們只需要注意用法即可.下面我就把所以Shiro標(biāo)簽放在下面了,各位看官自行查考.
<shiro:guest>
游客訪問 <a href = "login.jsp"></a>
</shiro:guest>
user 標(biāo)簽:用戶已經(jīng)通過認(rèn)證\記住我 登錄后顯示響應(yīng)的內(nèi)容
<shiro:user>
歡迎[<shiro:principal/>]登錄 <a href = "logout">退出</a>
</shiro:user>
authenticated標(biāo)簽:用戶身份驗(yàn)證通過沙热,即 Subjec.login 登錄成功 不是記住我登錄的
<shiro:authenticted>
用戶[<shiro:principal/>] 已身份驗(yàn)證通過
</shiro:authenticted>
notAuthenticated標(biāo)簽:用戶未進(jìn)行身份驗(yàn)證,即沒有調(diào)用Subject.login進(jìn)行登錄,包括"記住我"也屬于未進(jìn)行身份驗(yàn)證
<shiro:notAuthenticated>
未身份驗(yàn)證(包括"記住我")
</shiro:notAuthenticated>
principal 標(biāo)簽:顯示用戶身份信息,默認(rèn)調(diào)用
Subjec.getPrincipal()獲取,即Primary Principal
<shiro:principal property = "username"/>
hasRole標(biāo)簽:如果當(dāng)前Subject有角色將顯示body體內(nèi)的內(nèi)容
<shiro:hashRole name = "admin">
用戶[<shiro:principal/>]擁有角色admin
</shiro:hashRole>
hasAnyRoles標(biāo)簽:如果Subject有任意一個(gè)角色(或的關(guān)系)將顯示body體里的內(nèi)容
<shiro:hasAnyRoles name = "admin,user">
用戶[<shiro:pricipal/>]擁有角色admin 或者 user
</shiro:hasAnyRoles>
lacksRole:如果當(dāng)前 Subjec沒有角色將顯示body體內(nèi)的內(nèi)容
<shiro:lacksRole name = "admin">
用戶[<shiro:pricipal/>]沒有角色admin
</shiro:lacksRole>
hashPermission:如果當(dāng)前Subject有權(quán)限將顯示body體內(nèi)容
<shiro:hashPermission name = "user:create">
用戶[<shiro:pricipal/>] 擁有權(quán)限user:create
</shiro:hashPermission>
lacksPermission:如果當(dāng)前Subject沒有權(quán)限將顯示body體內(nèi)容
<shiro:lacksPermission name = "org:create">
用戶[<shiro:pricipal/>] 沒有權(quán)限org:create
</shiro:lacksPermission>
Freemark的Shiro標(biāo)簽
在SpringBoot里面并不是直接支持JSP文件的,然后我就在項(xiàng)目中使用了Freemark,但是Shiro標(biāo)簽并不能直接支持.ftl文件,所以我們需要先引入一個(gè)Maven依賴.
<dependency>
<groupId>net.mingsoft</groupId>
<artifactId>shiro-freemarker-tags</artifactId>
<version>0.1</version>
</dependency>
然后,我們寫一個(gè)名為ShiroTagsFreeMarkerCfg配置類來(lái)對(duì)Freemark使用Shiro標(biāo)簽進(jìn)行配置.當(dāng)然了,要確定配置類能被正確注入到Bean中,代碼如下所示.
import com.jagregory.shiro.freemarker.ShiroTags;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import javax.annotation.PostConstruct;
@Component
public class ShiroTagsFreeMarkerCfg {
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@PostConstruct
public void setSharedVariable() throws TemplateModelException {
freeMarkerConfigurer.getConfiguration().setSharedVariable("shiro", new ShiroTags());
}
}
配置類搞好之后,我們就可以在.ftl文件中使用Shiro的權(quán)限標(biāo)簽了,標(biāo)簽格式如下所示.
<@shiro.guest>
游客訪問 <a href = "login.jsp"></a>
</@shiro.guest>
user 標(biāo)簽:用戶已經(jīng)通過認(rèn)證\記住我 登錄后顯示響應(yīng)的內(nèi)容
<@shiro.user>
歡迎[<@shiro.principal/>]登錄唯竹,<a href="/logout.html">退出</a>
</@shiro.user>
authenticated標(biāo)簽:用戶身份驗(yàn)證通過呜魄,即 Subjec.login 登錄成功 不是記住我登錄的
<@shiro.authenticated>
用戶[<@shiro.principal/>]已身份驗(yàn)證通過
</@shiro.authenticated>
notAuthenticated標(biāo)簽:用戶未進(jìn)行身份驗(yàn)證力图,即沒有調(diào)用Subject.login進(jìn)行登錄,包括"記住我"也屬于未進(jìn)行身份驗(yàn)證
<@shiro.notAuthenticated>
當(dāng)前身份未認(rèn)證(包括記住我登錄的)
</@shiro.notAuthenticated>
principal 標(biāo)簽:顯示用戶身份信息洞斯,默認(rèn)調(diào)用
Subjec.getPrincipal()獲取,即Primary Principal
<@shiro.principal property="username"/>
hasRole標(biāo)簽:如果當(dāng)前Subject有角色將顯示body體內(nèi)的內(nèi)容
<@shiro.hasRole name="admin">
用戶[<@shiro.principal/>]擁有角色admin<br/>
</@shiro.hasRole>
hasAnyRoles標(biāo)簽:如果Subject有任意一個(gè)角色(或的關(guān)系)將顯示body體里的內(nèi)容
<@shiro.hasAnyRoles name="admin,user,member">
用戶[<@shiro.principal/>]擁有角色admin或user或member<br/>
</@shiro.hasAnyRoles>
lacksRole:如果當(dāng)前 Subjec沒有角色將顯示body體內(nèi)的內(nèi)容
<@shiro.lacksRole name="admin">
用戶[<@shiro.principal/>]不擁有admin角色
</@shiro.lacksRole>
hashPermission:如果當(dāng)前Subject有權(quán)限將顯示body體內(nèi)容
<@shiro.hasPermission name="user:add">
用戶[<@shiro.principal/>]擁有user:add權(quán)限
</@shiro.hasPermission>
lacksPermission:如果當(dāng)前Subject沒有權(quán)限將顯示body體內(nèi)容
<@shiro.lacksPermission name="user:add">
用戶[<@shiro.principal/>]不擁有user:add權(quán)限
</@shiro.lacksPermission>
結(jié)語(yǔ)
寫到這里,前前后后寫了八篇Shiro集成博客,這個(gè)系列的博客到此就結(jié)束了,后期如果遇到新的問題會(huì)繼續(xù)補(bǔ)充,歡迎繼續(xù)關(guān)注騷棟.內(nèi)心騷棟,未忘初心.