最近在做权限控制的时候用到了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>
也可以到我的CSDN博客大家讨论 http://blog.csdn.net/lingyundouer/article/details/50487459
相关推荐
freemarker集成shiro标签
主要介绍了spring-boot 集成 shiro 自定义密码验证 自定义freemarker标签根据权限渲染不同页面,需要的朋友可以参考下
并提供akka异步执行集成,多数据源自动orm映射,flyway数据库脚本升级, shiro 权限系统 freemarker-shiro标签支持,以及其他改进 使用angularjs作为前端框架, 以及 bootstrap requireJs+coffeescript+less...
解决方案:在 FreeMarkerViewExtend.java 33行处 增加了BasePath ,通过BasePath 来控制请求目录,在 Freemarker 中可以自由使用,而 JSP 中是直接在 JSP 中获取BasePath 使用。 解决后遗症:因为我们的权限是通过...
| ├── FreeMarker -- 自定义FreeMarker标签 │ ├── resources | ├── mapper -- SQL对应的XML文件 | ├── templates -- FreeMarker模版 │ ├── webapp | ├── statics -- 静态资源 | ├── upload -...
1.使用jfinal-shiro实现数据库级别的权限灵活定制,和freemarker的权限标签 2.使用jfinal-captcha实现验证码 3.使用jfinal-web实现根据ajax请求返回json数据,其他返回默认数据,支持继承JFController使用getModels...
是SpringBoot集成MyBatisPlus 升级版的maven多模块开发项目结构wstro├──sql 项目SQL语句│ ├──App ... ├─FreeMarker 自定义FreeMarker标签│ ├──resources │ ├─mapper SQL对应的XML文件│ ├─templat
数据持久化实体类│├── datasources -- 多数据源工具类│├── shiro -- Shiro验证框架│├── task -- Quartz定时任务│├── util -- 工具类| ├── FreeMarker -- 自定义FreeMarker标签│├── ...
dreampie-jfinal 使用JFinal框架,并提供akka异步执行集成,多数据源自动orm映射,flyway数据库脚本升级,shiro 权限系统 freemarker支持,以及其他改进 bootstrap前端 该项目剥离出的框架jfinal-dreampie,大家...
13.shiro的freemarker标签库 <@shiro.hasPermission name="P_USER"> <li><a href="/admin/user">${i18n.getText("admin.user")}</a></li> </@shiro.hasPermission> 14.xss过滤的StringEscapeUtils...
内容管理系统:支持多标签、多类目、强大评论的内容管理,有基本单页展示,菜单管理,系统设置等功能。 > zheng-pay - 一站式支付解决方案,统一下单接口,支持支付宝、微信、网银等多种支付方式。不涉及业务的...