shiro + freemark

来源:互联网 发布:淘宝外卖怎么加盟代理 编辑:程序博客网 时间:2024/06/07 08:53

http://lingyundouer.iteye.com/blog/2269552

1、需要引入jar包,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引入的会解析不了。
 

页面上面可直接使用标签,不用进行引入,如果引入会出现问题


原创粉丝点击