您的位置:首页 > 编程语言 > Java开发

spring mvc + shiro +thymeleaf 扩展使用shiro tags页面标签

2017-03-17 13:20 423 查看
先看官方说明:
Apache Shiro provides a Subject-aware JSP/GSP tag library that allows you to control your JSP, JSTL or GSP page output based on the current Subject's state. This is quite useful for personalizing views
based on the identity and authorization state of the current user viewing the web page.

Tag Library Configuration

The Tag Library Descriptor (TLD) file is bundled in shiro-web.jar in the META-INF/shiro.tld file. To use any of the tags, add the following line to the top of your JSP page (or wherever you define page
directives):

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

We've used the shiro prefix to indicate the shiro tag library namespace, but you can assign whatever name you like.

Now we'll cover each tag and show how it might be used to render a page.

官网对于shiro标签说明 ,它支持jsp,jstl,gsp等前端技术,但是不支持themleaf。
但是在https://github.com/theborakompanioni/thymeleaf-extras-shiro中提供一个
thymeleaf-extras-shiro技术,相当于thymeleaf的扩展。

maven 配置如下:

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

spring mvc 配置文件中配置如下:

 <!--thymeleaf 视图处理器配置-->
    <bean id="templateResolver"
          class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/templates/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="characterEncoding" value="UTF-8"/>
    </bean>
    <bean id="templateEngine"
          class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
        <property name="additionalDialects">
            <set>
                <bean class="at.pollux.thymeleaf.shiro.dialect.ShiroDialect"/>
            </set>
        </property>
    </bean>
    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

页面则可以使用 shiro标签:

<ul class="nav nav-tabs nav-stacked main-menu" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
      <li
shiro:hasPermission ="SearchFile">
        <a href="fileanager.html" th:href="@{/test/adminUser/fileManager}"> 
        <i class="icon-bar-chart"></i><span class="hidden-tablet"> 文件管理</span>
        </a>
      </li>
</ul>

官方说明:https://github.com/theborakompanioni/thymeleaf-extras-shiro
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shiro spring mvc thymeleaf