天天看點

Shiro thymeleaf整合使用

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

©Copyright 蕃薯耀 2022-04-16

https://www.cnblogs.com/fanshuyao/

一、引入依賴

<!-- 使用見: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

https://www.cnblogs.com/fanshuyao/

今天越懶,明天要做的事越多。

繼續閱讀