shiro在thymeleaf中的常用标签

博客介绍了Shiro在Thymeleaf中的使用。包括在登录界面login.html加入rememberMe复选框,在pom.xml中引入依赖,在ShiroConfiguration.java中添加相关内容,在index.html引入,还给出了shiro标签简单描述及转载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

修改登录界面加入rememberMe复选框
在login.html中加入:
<P><input type="checkbox" name="rememberMe" />记住我</P>

在pom.xml中引入依赖

	<dependency>
			<groupId>com.github.theborakompanioni</groupId>
			<artifactId>thymeleaf-extras-shiro</artifactId>
			<version>2.0.0</version>
		</dependency>

ShiroConfiguration.java中添加

    /**
     * ShiroDialect,为了在thymeleaf里使用shiro的标签的bean
     * @return
     */
    @Bean
    public ShiroDialect shiroDialect(){
		return new ShiroDialect();
	}

index.html引入

<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

shiro标签的简单描述

<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>  
<meta charset="UTF-8" />  
<title>Insert title here</title>  
</head>  
<body>  
    <h3>index</h3> 
    <!-- 验证当前用户是否为“访客”,即未认证(包含未记住)的用户。 -->
    <p shiro:guest="">Please <a href="login.html">login</a></p>
    
    
    <!-- 认证通过或已记住的用户。 -->
    <p shiro:user="">
       Welcome back John! Not John? Click <a href="login.html">here</a> to login.
    </p>
    
    <!-- 已认证通过的用户。不包含已记住的用户,这是与user标签的区别所在。 -->
    <p shiro:authenticated="">
      Hello, <span shiro:principal=""></span>, how are you today?
    </p> 
    <a shiro:authenticated="" href="updateAccount.html">Update your contact information</a>
    
    <!-- 输出当前用户信息,通常为登录帐号信息。 -->
    <p>Hello, <shiro:principal/>, how are you today?</p>
    
    
    <!-- 未认证通过用户,与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户。 -->
    <p shiro:notAuthenticated="">
       Please <a href="login.html">login</a> in order to update your credit card information.
    </p>
     
    <!-- 验证当前用户是否属于该角色。 -->
    <a shiro:hasRole="admin" href="admin.html">Administer the system</a><!-- 拥有该角色 -->
    
    <!-- 与hasRole标签逻辑相反,当用户不属于该角色时验证通过。 -->
    <p shiro:lacksRole="developer"><!-- 没有该角色 -->
      Sorry, you are not allowed to developer the system.
    </p>
    
    <!-- 验证当前用户是否属于以下所有角色。 -->
    <p shiro:hasAllRoles="developer, admin"><!-- 角色与判断 -->
       You are a developer and a admin.
    </p>
    
    <!-- 验证当前用户是否属于以下任意一个角色。  -->
    <p shiro:hasAnyRoles="admin, vip, developer"><!-- 角色或判断 -->
         You are a admin, vip, or developer.
    </p>
    
    <!--验证当前用户是否拥有指定权限。  -->
    <a shiro:hasPermission="userInfo:add" href="createUser.html">添加用户</a><!-- 拥有权限 -->
    
    <!-- 与hasPermission标签逻辑相反,当前用户没有制定权限时,验证通过。 -->
    <p shiro:lacksPermission="userInfo:del"><!-- 没有权限 -->
         Sorry, you are not allowed to delete user accounts.
    </p>
    
    <!-- 验证当前用户是否拥有以下所有角色。 -->
    <p shiro:hasAllPermissions="userInfo:view, userInfo:add"><!-- 权限与判断 -->
           You can see or add users.
    </p>
    
    <!-- 验证当前用户是否拥有以下任意一个权限。  -->
    <p shiro:hasAnyPermissions="userInfo:view, userInfo:del"><!-- 权限或判断 -->
               You can see or delete users.
    </p>
    
</body>  
</html>

转载:https://blog.youkuaiyun.com/u014695188/article/details/52356158

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值