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.FreeMarkerConfigurer
import com.jagregory.shiro.freemarker.ShiroTags
import freemarker.template.Configuration
import freemarker.template.TemplateException
import freemarker.template.TemplateModelException
@Component
class ShiroTagsFreeMarkerCfg {
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@PostConstruct
public void setSharedVariable() throws TemplateModelException {
println "设置freeMarker 的shiro 标签"
freeMarkerConfigurer.getConfiguration().setSharedVariable("shiro", new ShiroTags());
}
}
3.在模板文件中使用
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<#-- 注释部分 -->
<#-- 下面使用插值 -->
<p>We have these users: </p>
<ul>
首页
<@shiro.hasPermission name="权限添加">
User Account:${user.userAccount} <br/>
用户 [<@shiro.principal />]拥有权限添加权限
</@shiro.hasPermission>
<ul>
</body>
</html>