1. 程式人生 > >freemarker集成shiro標簽

freemarker集成shiro標簽

watermark perm over auto 標簽 htm asa enc avi

自己寫的案例在github上 可以下載下來看

https://github.com/171437912/shiro_ssm_Demo.git

freemarker集成shiro標簽

最近在做權限控制的時候用到了shiro,可惜一竅不通,學了一段時間之後,在freemarker裝飾器中集成shiro標簽時遇到了一點問題,網上資料都是在普通頁面實現,特此記錄下,如有理解不對的地方還請各位指正。
1、需要引入jar包,或者到github上下載源碼打包使用(也可以實現自己特定需求):https://github.com/zhoushuaichang/shiro-freemarker-tags 2、集成freemarker的配置類FreeMarkerConfigurer,重寫afterPropertiesSet()方法:如下
import com.jagregory.shiro.freemarker.ShiroTags;
import freemarker.template.TemplateException;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.IOException;

/**
* 繼承FreeMarkerConfigurer類,重寫afterPropertiesSet()方法;
* 集成shiroTags標簽
* Created by zsc on 2016/1/5.
*/
public class ShiroTagFreeMarkerConfigurer extends FreeMarkerConfigurer {

@Override
public void afterPropertiesSet() throws IOException, TemplateException {
super.afterPropertiesSet();
this.getConfiguration().setSharedVariable("shiro", new ShiroTags());
}

}
3、修改freemarker的xml配置文件:把freemarkerConfig bean的class指向自定義的ShiroTagFreeMarkerConfigurer,如下
<!--<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">-->
<bean id="freemarkerConfig" class="com.***.shiro.tag.ShiroTagFreeMarkerConfigurer">
<!--shiro標簽僅限用在該路徑下的ftl頁面,不能使用include引入-->
   <property name="templateLoaderPath" value="/WEB-INF/viewftl/" /> 
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">utf-8</prop>
<prop key="number_format">\#0.\#\#\#\#\#</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
<!--<prop key="auto_import">/common/page.ftl as p</prop>-->
<!--<prop key="auto_include">/common/page.ftl</prop>-->
</props>
</property>
</bean>
4、包含以下標簽 guest標簽:驗證當前用戶是否為“訪客”,即未認證(包含未記住)的用戶;shiro標簽:<shiro:guest></shiro:guest> ;freemark中: <@shiro.guest> </@shiro.guest> user標簽:認證通過或已記住的用戶 shiro標簽:<shiro:user> </shiro:user> ;freemark中: <@shiro.user> </@shiro.user> authenticated標簽:已認證通過的用戶。不包含已記住的用戶,這是與user標簽的區別所在。 shiro標簽:<shiro:authenticated> </shiro:authenticated>;freemark中: <@shiro.authenticated></@shiro.authenticated> notAuthenticated標簽:未認證通過的用戶。與authenticated標簽相對。 shiro標簽:<shiro:notAuthenticated> </shiro:notAuthenticated>;freemark中: <@shiro.notAuthenticated></@shiro.notAuthenticated> principal標簽:輸出當前用戶信息,通常為登錄帳號信息 shiro標簽:Hello, <@shiro.principal property="name" /> ;freemarker中: Hello, <@shiro.principal property="name" />, how are you today? hasRole標簽:驗證當前用戶是否屬於該角色 ,shiro標簽: <shiro:hasRole name="administrator"> Administer the system </shiro:hasRole> ;freemarker中:<@shiro.hasRole name=”admin”>Hello admin!</@shiro.hasRole> hasAnyRoles標簽:驗證當前用戶是否屬於這些角色中的任何一個,角色之間逗號分隔 ,shiro標簽: <shiro:hasAnyRoles name="admin,user,operator"> Administer the system </shiro:hasAnyRoles> ;freemarker中:<@shiro.hasAnyRoles name="admin,user,operator">Hello admin!</@shiro.hasAnyRoles> hasPermission標簽:驗證當前用戶是否擁有該權限 ,shiro標簽: <shiro:hasPermission name="/order:*"> 訂單 </shiro:hasPermission> ;freemarker中:<@shiro.hasPermission name="/order:*">訂單/@shiro.hasPermission> lacksRole標簽:驗證當前用戶不屬於該角色,與hasRole標簽想反,shiro標簽: <shiro:hasRole name="admin"> Administer the system </shiro:hasRole> ;freemarker中:<@shiro.hasRole name="admin">Hello admin!</@shiro.hasRole> lacksPermission標簽:驗證當前用戶不擁有某種權限,與hasPermission標簽是相對的,shiro標簽: <shiro:lacksPermission name="/order:*"> trade </shiro:lacksPermission> ;freemarker中:<@shiro.lacksPermission name="/order:*">trade</@shiro.lacksPermission> 5、註意:此情況只是對沒有使用sitemesh有效,若freemarker使用了裝飾器模板來對返回頁面進行裝飾,這些標簽只能用在返回的ftl頁面中,而不能用在模板ftl中,include引入的會解析不了。 解決上述問題,可以通過修改freemarker的重寫覆蓋源碼的Configuration類來實現,這樣做的話不僅可以實現上述集成的效果,而且還避免的裝飾頁面不生效的問題,具體做法如下: 1、將 freemarker.template.Configuration.java中的源碼復制到項目中,包命名和類命名與之完全相同,修改自己復制後的類中loadBuiltInSharedVariables()方法,添加sharedVariables.put("shiro", new ShiroTags());,如下:ShiroTags是上述shiro-freemarker-tags.jar中定義的。 技術分享 2、修改web.xml文件,如下,紅色字體是需要在shiroFilter中配置的,因為動態請求頁面中的標簽是通過REQUEST獲取的,而靜態裝飾頁面中的標簽是通過FORWARD或者INCLUDE獲取的,否則在解析標簽時獲取不到當前用戶的subject信息。另外,shiroFilter建議配置在sitemesh之前。
 <!-- shiro 安全過濾器 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>*.htm</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<!-- sitemesh 裝飾配置 -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

freemarker集成shiro標簽