Freemarker-shiro的標籤

1、引入依賴(已解決版本衝突)
複製代碼
<!-- shiro-freemarker-tags start -->
<dependency>
    <groupId>net.mingsoft</groupId>
    <artifactId>shiro-freemarker-tags</artifactId>
    <version>1.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-all</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- shiro-freemarker-tags end -->

<!-- freemarker start -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.28</version>
</dependency>
<!-- freemarker end -->

<!-- shiro begin -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-all</artifactId>
    <version>1.4.0</version>
</dependency>
<!-- shiro end -->
複製代碼
2、配置
Java代碼:

複製代碼
public class FreeMarkerConfigExtend extends FreeMarkerConfigurer {
    @Override
    public void afterPropertiesSet() throws IOException, TemplateException {
        super.afterPropertiesSet();
        Configuration cfg = this.getConfiguration();
        // 添加shiro標籤
        cfg.setSharedVariable("shiro", new ShiroTags());
    }
}
複製代碼
複製代碼
<!-- freemarker環境配置 -->
<bean id="freemarkerConfig" class="com.demo.shiro.common.freemarker.FreeMarkerConfigExtend">
    <!-- 模版位置,這裏配置了下面就不用配了 -->
    <property name="templateLoaderPath" value="/WEB-INF/views" />
    <property name="freemarkerSettings"><!-- 一些設置 -->
        <props>
            <prop key="template_update_delay">0</prop>
            <prop key="default_encoding">UTF-8</prop>
            <prop key="locale">zh_CN</prop>
            <prop key="boolean_format">true,false</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">0.##########</prop>
            <prop key="classic_compatible">true</prop>
            <prop key="template_exception_handler">ignore</prop>
            <prop key="auto_import">
                <!-- 自動裝載,引入Freemarker,用於Freemarker Macro引入 -->
                /common/_meta.ftl as _meta,
                /common/_footer.ftl as _footer
                <!--/common/menu.ftl as _menu-->
            </prop>
        </props>
    </property>
</bean>
複製代碼
3、shiro標籤詳解
1. guest(遊客)

<@shiro.guest>  
    您當前是遊客,<a href="javascript:void(0);">登陸</a>
</@shiro.guest> 
2. user(已經登陸,或者記住我登陸)

<@shiro.user>  
    歡迎[<@shiro.principal/>]登陸,<a href="/logout.shtml">退出</a>  
</@shiro.user>   
3. authenticated(已經認證,排除記住我登陸的)

<@shiro.authenticated>  
    用戶[<@shiro.principal/>]已身份驗證經過  
</@shiro.authenticated> 
4. notAuthenticated(和authenticated相反)

<@shiro.notAuthenticated>
    當前身份未認證(包括記住我登陸的)
</@shiro.notAuthenticated> 
該功能主要用途:識別是否是本次操做登陸過的,好比支付系統,進入系統能夠用記住個人登陸信息,可是當要關鍵操做的時候,須要進行認證識別。

5. principal標籤

principal標籤,取值取的是你登陸的時候。在Realm實現類中的以下代碼:

...
return new SimpleAuthenticationInfo(user,user.getPswd(), getName());
在 new SimpleAuthenticationInfo(第一個參數,....) 的第一個參數放的若是是一個username,那麼就能夠直接用。

<!--取到username-->
<@shiro. principal/>
若是第一個參數放的是對象,好比放User對象。那麼若是要取username字段。

<!--須要指定property-->
<@shiro.principal property="username"/>
和Java以下Java代碼一致

User user = (User) SecurityUtils.getSubject().getPrincipals();
String username = user.getUsername();
6. hasRole標籤(判斷是否擁有這個角色)

<@shiro.hasRole name="admin">  
    用戶[<@shiro.principal/>]擁有角色admin<br/>  
</@shiro.hasRole>   
7. hasAnyRoles標籤(判斷是否擁有這些角色的其中一個)

<@shiro.hasAnyRoles name="admin,user,member">  
    用戶[<@shiro.principal/>]擁有角色admin或user或member<br/>  
</@shiro.hasAnyRoles>   
8. lacksRole標籤(判斷是否不擁有這個角色)

<@shiro.lacksRole name="admin">  
    用戶[<@shiro.principal/>]不擁有admin角色
</@shiro.lacksRole>   
9. hasPermission標籤(判斷是否有擁有這個權限)

<@shiro.hasPermission name="user/add">  
    用戶[<@shiro.principal/>]擁有user/add權限
</@shiro.hasPermission>   
10. lacksPermission標籤(判斷是否沒有這個權限)

<@shiro.lacksPermission name="user/add">  
    用戶[<@shiro.principal/>]不擁有user/add權限
</@shiro.lacksPermission> 
相關文章
相關標籤/搜索