Shiro thymeleaf整合使用


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

?Copyright 蕃薯耀 2022-04-16

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

一、引入依赖


<dependency>
    <groupId>com.github.theborakompanionigroupId>
    <artifactId>thymeleaf-extras-shiroartifactId>
    <version>${shiro-thymeleaf.version}version>
dependency>

二、配置ShiroDialect

/**
     * thymeleaf集成shiro的标签,前缀是:shiro
     * 
     * 需要在pom文件引入依赖:thymeleaf-extras-shiro
     * 
     * 
  • hasPermission:多个通过英文逗号分隔
  • *
  • hasAllPermissions
  • *
  • hasAnyPermissions
  • *
  • lacksPermission
  • * *
  • hasRole
  • *
  • hasAllRoles
  • *
  • hasAnyRoles
  • *
  • lacksRole
  • * *
  • authenticated
  • *
  • user
  • * *
    @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">bbdiv>

    2、方式二:作为标签

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

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

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

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

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

    at.pollux.thymeleaf.shiro.dialect.ShiroDialect

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

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

    ?Copyright 蕃薯耀 2022-04-16

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