Shiro 如何对Jsp页面标签授权呢?

2022/7/30 6:23:53

本文主要是介绍Shiro 如何对Jsp页面标签授权呢?,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

转自:

http://www.java265.com/JavaCourse/202201/2170.html

下文笔者讲述jsp页面标签授权的方法分享,如下所示:

shiro中使用Jsp页面标签授权首先需要导入标签库<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

常见的Shiro标签

<shiro:authenticated>        登录之后
<shiro:notAuthenticated>        不在登录状态时
<shiro:guest>            用户在没有RememberMe时
<shiro:user>            用户在RememberMe时
<shiro:hasAnyRoles name="abc,123" >    在有abc或者123角色时
<shiro:hasRole name="abc">        拥有角色abc
<shiro:lacksRole name="abc">        没有角色abc
<shiro:hasPermission name="abc">    拥有权限资源abc
<shiro:lacksPermission name="abc">    没有abc权限资源
<shiro:principal>        显示用户身份名称
<shiro:principal property="username"/>         显示用户身份中的属性值

guest

guest标签(与@RequiresGuest对应),验证用户没有登录认证或被记住过,验证是否是一个guest的请求

<shiro:guest>  
    欢迎游客访问,<a href="${pageContext.request.contextPath}/login.jsp">登录</a>  
</shiro:guest>

user

user标签(与@RequiresUser对应),登录后(记住我)看到的内容

<shiro:user>  
    欢迎您:<shiro:principal/>
</shiro:user>

authenticated

authenticated标签,用户登录(不包括记住我功能登录)

<shiro:authenticated>
    你已经认证通过啦,欢迎您:<shiro:principal/>
</shiro:authenticated>

notAuthenticated

用户未登录(记住我功能也算未登录)

principal

显示用户身份信息,默认调用Subject.getPrincipal()获取,即Primary Principal

<shiro:principal type="java.lang.String"/>	相当于Subject.getPrincipals().oneByType(String.class)
<shiro:principal property="username"/>          相当于((User)Subject.getPrincipals()).getUsername()

hasRole

与@RequiresRoles对应,校验是否有权限

<shiro:hasRole name="update">  
    <a href="/modify.jsp">修改</a>
</shiro:hasRole> 

lacksRole

当前用户没有任何角色则显示标签体中的内容

hasAnyRoles

当前用户拥有任何一个角色则显示标签体中的内容

hasAllRoles

当前用户拥有指定的所有角色则显示标签体中的内容

hasPermission

与@RequiresPermissions对应

lacksPermission

当前用户拥有任何一个权限则显示标签体中的内容

hasAnyPermissions

当前用户拥有任何一个权限则显示标签体中的内容

hasAllPermissions

当前用户拥有指定的所有权限则显示标签体中的内容,一个或多个角色和权限的在项目中会经常使用

<shiro:hasAllPermissions name="user:create,user:update">  
    用户[<shiro:principal/>]拥有权限user:create和user:update<br/>  
</shiro:hasAllPermissions>  

<shiro:authenticated>
    你已经认证通过啦
 
    欢迎您:<shiro:principal/>
    <hr>
 
    <shiro:hasRole name="admin">
    所拥有的角色:admin
    </shiro:hasRole>
    <br>
    <shiro:hasAnyRoles name="admin,abc">
        拥有其中一个角色:admin or abc
    </shiro:hasAnyRoles>
    <br>
    <shiro:lacksRole name="admin2">
        未拥有的角色:admin2
    </shiro:lacksRole>
    <hr>
    <shiro:hasPermission name="role:create">
        拥有的权限:role:create
    </shiro:hasPermission>
    <br>
    <shiro:lacksPermission name="menu:create">
        未拥有的权限:menu:create
    </shiro:lacksPermission>
</shiro:authenticated>


这篇关于Shiro 如何对Jsp页面标签授权呢?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程