---
title: "在Spring+FreeMarker框架中使用Shiro的Tag标签"
author: "ware"
date: "2013-07-17T17:53:58Z"
tags: []
categories: ["编程语言"]
slug: "spring-freemarker-shiro-tag"
draft: false
lastmod: "2013-07-17T17:56:10Z"
id: 1908569
url: "https://ware.iteye.com/blog/1908569"
---

目前Freemarker对Shrio的标签还不支持，不过已经有人贡献出来第三方面解决方案，如下：

1、下载shiro-freemarker-tags  
GitHub地址：https://github.com/jagregory/shiro-freemarker-tags  
可以先看一下他的说明文档，有一个初步的了解。

2、Spring MVC配置  
自定义一个ShiroTagFreeMarkerConfigurer继承Spring本身提供的FreeMarkerConfigurer,目的是在FreeMarker的Configuration中添加shiro的配置

 

```
public class ShiroTagFreeMarkerConfigurer extends FreeMarkerConfigurer {
 
    @Override
    public void afterPropertiesSet() throws IOException, TemplateException {
        super.afterPropertiesSet();
        this.getConfiguration().setSharedVariable("shiro", new ShiroTags());
    }
     
}
```

 下面的Spring mvc配置

 

 

```
<bean id="freemakerCongfig"
    class="com.xxx.web.freemarker.ShiroTagFreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/views/" />
    <property name="freemarkerSettings">
        <props>
            <prop key="defaultEncoding">UTF-8</prop>
            <prop key="classic_compatible">true</prop>
        </props>
    </property>
</bean>
```

 

#### 3、使用Shiro Tag

&lt;@shiro.guest&gt;Hello guest!&lt;/@shiro.guest&gt;

 

&lt;@shiro.hasPermission name="auth:role:add"&gt;hello world&lt;/@shiro.hasPermission&gt;