Freemarker 的 Shiro 標籤使用詳解
阿新 • • 發佈:2018-11-28
一、引入依賴(已解決版本衝突)
<!-- 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 -->
二、配置
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>
三、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>