一、引入依賴(已解決版本沖突)
<!-- 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>