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>
相關文章
相關標籤/搜索