前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shiro教程10(freemaker使用shiro标签)

shiro教程10(freemaker使用shiro标签)

作者头像
用户4919348
发布2019-04-02 11:42:05
1K0
发布2019-04-02 11:42:05
举报
文章被收录于专栏:波波烤鸭

  当我们需要将页面中一些没有权限的标签给隐藏掉的时候,shiro默认提供了对jsp的支持,但是我们有些时候前端模板引擎使用的是freemarker,这时我们就需要专门来设置下了。

引入相关依赖

代码语言:javascript
复制
<dependency>
	<groupId>net.mingsoft</groupId>
	<artifactId>shiro-freemarker-tags</artifactId>
	<version>0.1</version>
</dependency>

添加一个java文件

代码语言:javascript
复制
/**
 * 自定义FreemarkerConfigurer类
 * @author dengp
 *
 */
public class FreeMarkerConfigExtend extends FreeMarkerConfigurer {

	@Override
	public void afterPropertiesSet() throws IOException, TemplateException {
		// TODO Auto-generated method stub
		super.afterPropertiesSet();
		Configuration cfg = this.getConfiguration();
		//shiro标签
		cfg.setSharedVariable("shiro", new ShiroTags());
	}
}

修改配置

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
<!-- 2.定义模板属性 -->
<bean
	class="com.sxt.freemarker.FreeMarkerConfigExtend"> 
	<!-- 定义模板位置 -->
	<property name="templateLoaderPath" value="/WEB-INF/ftl/" /> 
	<!-- 编码方式 -->
	<property name="defaultEncoding" value="utf-8" />
	<!-- 设置键值对 -->
	<property name="freemarkerVariables">
		<map>
			<entry key="root" value="${root}"></entry>
			<entry key="a1" value="${a1}"></entry>
		</map>
	</property> 
	<!--设置属性值 -->
	<property name="freemarkerSettings">
		<props>
			<prop key="template_update_delay">10</prop>
			<prop key="locale">zh_CN</prop>
			<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
			<prop key="date_format">yyyy-MM-dd</prop>
			<prop key="time_format">HH:mm:ss</prop>
			<prop key="number_format">#.####</prop>
		</props>
	</property>
</bean> 

使用shiro标签

在需要使用的ftl页面中如下使用

代码语言:javascript
复制
<@shiro.hasRole name="role1">
	用户[<@shiro.principal/>]拥有角色role1<br/>
</@shiro.hasRole> 

更多shiro标签使用参考Shiro教程7(整合SSM项目-授权)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年01月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 引入相关依赖
  • 添加一个java文件
  • 修改配置
  • 使用shiro标签
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档