Shiro快速入门 —— 9.freemaker使用shiro标签

本系列博文目录:http://www.javashuo.com/article/p-ewndobct-kn.htmlhtml

shiro-freemarker-tags项目jar包下载

freemaker默认状况下是不能使用shiro标签进行权限控制的。java

 

还好已经由大神James Gregory将此问题解决,并将源码发布到了GitHub上面了。git

GitHub上项目地址:https://github.com/jagregory/shiro-freemarker-tagsgithub

 

GitHub上的jar包下载报404,能够本身下载源码打包或者使用我提供的源码和jar包(用个人吧~~)spring

个人项目地址http://git.oschina.net/imlichao/shiro-freemarker-tagsmaven

个人项目jar包地址http://git.oschina.net/imlichao/shiro-freemarker-tags/raw/master/dist/shiro-freemarker-tags-0.1-SNAPSHOT.jar编码

 

jar包导入项目

若是项目没有使用maven则能够直接添加到项目中spa

若是使用了maven能够发布到私服或者直接放到本地仓库中进行引用.net

<dependency>
            <groupId>com.jagregory.shiro</groupId>
            <artifactId>freemarker</artifactId>
            <version>0.1-SNAPSHOT</version>
        </dependency>

 

在freemaker中自定义shiro标签

这个项目实质上就是实现了一套freemaker的自定义标签,所咱们须要自定义shiro标签。code

在freemaker配置文件中自定义标签(例子为spring boot配置文件截取的)

/**
 * FreeMarker配置文件
 */
@Configuration
public class FreemarkerConfig {

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer(FreeMarkerProperties freeMarkerProperties) {
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        configurer.setTemplateLoaderPaths(freeMarkerProperties.getTemplateLoaderPath()); //模板加载路径默认 "classpath:/templates/"
        configurer.setDefaultEncoding("utf-8");//设置页面默认编码(不设置页面中文乱码)
        Map<String,Object> variables=new HashMap<String,Object>();
        variables.put("shiro", new ShiroTags());
        configurer.setFreemarkerVariables(variables);//添加shiro自定义标签
        return configurer;
    }

}

 

在页面使用shiro标签

以上配置完成后,咱们就能够在ftl页面使用<@shiro>标签了

给几个例子

<!-- 游客内容 -->
<@shiro.guest>Hello guest!</@shiro.guest>

<!-- 验证权限 -->
<@shiro.hasPermission name = "admin_customer_manage">
   会员详情
</@shiro.hasPermission>
相关文章
相关标签/搜索