1.添加 shiro-freemarker-tags 库:
compile("net.mingsoft:shiro-freemarker-tags:0.1")
2.创建 ShiroTagsFreeMarkerCfg
import javax.annotation.PostConstruct;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;import org.springframework.stereotype.Component;import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurerimport com.jagregory.shiro.freemarker.ShiroTagsimport freemarker.template.Configurationimport freemarker.template.TemplateExceptionimport freemarker.template.TemplateModelException@Componentclass ShiroTagsFreeMarkerCfg { @Autowired private FreeMarkerConfigurer freeMarkerConfigurer; @PostConstruct public void setSharedVariable() throws TemplateModelException { println "设置freeMarker 的shiro 标签" freeMarkerConfigurer.getConfiguration().setSharedVariable("shiro", new ShiroTags()); } }
3.在模板文件中使用
Welcome! <#-- 注释部分 --><#-- 下面使用插值 -->We have these users:
- 首页 <@shiro.hasPermission name="权限添加"> User Account:${user.userAccount} 用户 [<@shiro.principal />]拥有权限添加权限