shiro thymeleaf整合使用

 ================================

©Copyright 蕃薯耀 2022-04-16

蕃薯耀的博客_CSDN博客

一、引入依赖

<!-- 使用见:https://github.com/theborakompanioni/thymeleaf-extras-shiro -->
<dependency>
	<groupId>com.github.theborakompanioni</groupId>
	<artifactId>thymeleaf-extras-shiro</artifactId>
	<version>${shiro-thymeleaf.version}</version>
</dependency>

二、配置ShiroDialect

/**
	 * thymeleaf集成shiro的标签,前缀是:shiro
	 * 
	 * 需要在pom文件引入依赖:thymeleaf-extras-shiro
	 * 
	 * <li>hasPermission:多个通过英文逗号分隔</li>
	 * <li>hasAllPermissions</li>
	 * <li>hasAnyPermissions</li>
	 * <li>lacksPermission</li>
	 * 
	 * <li>hasRole</li>
	 * <li>hasAllRoles</li>
	 * <li>hasAnyRoles</li>
	 * <li>lacksRole</li>
	 * 
	 * <li>authenticated</li>
	 * <li>user</li>
	 * 
	 * @see at.pollux.thymeleaf.shiro.dialect.ShiroDialect
	 * 
	 * @return
	 */
	@Bean
	public ShiroDialect shiroDialect(){
	    return new ShiroDialect();
	}

三、Html页面加入头xmlns:shiro

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" 
		xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

增加头xmlns:shiro,会有提示。

四、Html页面具体使用

1、方式一:作为属性

作为属性,部分属性会有提示(shiro:hasAnyRoles有提示,hasRole没有提示(因为版本比较旧),但不影响hasRole的使用),另外在页面要有提示,必须引入:xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"

<div shiro:hasRole="bbb">bb</div>

2、方式二:作为标签

作为标签,这种一般在 Jsp 页面使用,详细使用见:

https://shiro.apache.org/web.html#tag_library

经测试,在Thymeleaf使用也没问题,但没有自动完成的提示。

<shiro:hasRole name="aa">
    <div>aa</div>
</shiro:hasRole>

Shiro页面标签所有的属性可以看这个类:

at.pollux.thymeleaf.shiro.dialect.ShiroDialect

 (时间宝贵,分享不易,捐赠回馈,^_^)

================================

©Copyright 蕃薯耀 2022-04-16

蕃薯耀的博客_CSDN博客

猜你喜欢

转载自blog.csdn.net/w995223851/article/details/124215786
今日推荐