html 使用shiro标签 thymeleaf

这是spring.xml的配置文件,

<!--使用thyme leaf-->
    <!--视图解析器-->
    <bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".html"></property>
        <property name="templateMode" value="HTML"></property>
        <property name="cacheable" value="false"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"></property>
        <property name="additionalDialects">
            <set>
                <!--方言解释器-->
                <bean class="at.pollux.thymeleaf.shiro.dialect.ShiroDialect"/>
            </set>
        </property>
    </bean>

    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine"/>
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

这是pom.xml

 <!--Shiro-->
        <!--集成spring-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.7.1</version>
        </dependency>
        <!--web项目-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>1.7.1</version>
        </dependency>
        <!--shiro日志-->


        <!-- shiro  -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-all</artifactId>
            <version>1.7.1</version>
        </dependency>

        <!-- 引入ehcache的依赖,给shiro做缓存权限用的 -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.6</version>
        </dependency>



        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.4.1</version>
        </dependency>

        <!-- Shiro uses SLF4J for logging.  We'll use the 'simple' binding
             in this example app.  See http://www.slf4j.org for more info. -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.21</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.10.RELEASE</version>
        </dependency>

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

        <dependency>
            <groupId>org.attoparser</groupId>
            <artifactId>attoparser</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

以下是html

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

<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>

<h1>index</h1>
<a  href="/logout">logout</a>
<br><br>

<a href="common.html">一般用户</a>
<a href="admin.html">管理员</a>

<p shiro:hasRole="admin">
你好 管理员!
</p>
<a shiro:hasRole="user">你好user</a>
<a href="/shiroTest">测试时间</a>


</body>
</html>

普通用户登录

在这里插入图片描述
在这里插入图片描述

显示你好user

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

管理员登录、

在这里插入图片描述
显示你好管理员,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shiro整合Thymeleaf是为了在Thymeleaf模板引擎中使用Shiro的功能。首先需要在页面中添加Shiro的命名空间声明,即xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"。然后在项目的pom.xml文件中引入thymeleaf-extras-shiro的依赖,例如:<dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>2.0.0</version></dependency>。在Shiro的配置类中配置ShiroDialect(Shiro方言)对象,例如:@Bean public ShiroDialect shiroDialect() { return new ShiroDialect(); }。最后,在Thymeleaf的页面中,可以使用Shiro的相关标签和表达式来实现动态检测和授权控制。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【Shiro笔记三】Shiro整合Thymeleaf](https://blog.csdn.net/m0_67393827/article/details/124099555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Shiro整合thymeleaf](https://blog.csdn.net/qq_44116526/article/details/122115756)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值