shiro权限框架,前端验证是为jsp设计的,其中的tag只能用于jsp系列的模板引擎。最近项目使用了thymeleaf作为前端模板引擎,使用HTML文件,没法引入shiro的tag lib,此时如果要使用shiro的话,可以引入 thymeleaf-extras-shiro.jar这个拓展包来曲线实现shiro的前端验证。
在pom.xml中加入如下依赖:
<dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>1.0.2</version> </dependency>关于版本,可以在maven仓库中查询:http://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro/1.0.2
引入jar包后,我们要简单设置一下thymeleaf的引擎:
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalDialects">
<set>
<bean class="at.pollux.thymeleaf.shiro.dialect.ShiroDialect"/>
</set>
</property>
</bean>
或者如果使用的是Java代码配置的话:
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
Set<IDialect> additionalDialects = new HashSet<IDialect>();
additionalDialects.add(new ShiroDialect());
templateEngine.setAdditionalDialects(additionalDialects);
return templateEngine;
}shiro本身的标签有限,没有hasAnyPermission标签,我在之前的一篇文章中《自定义shiro标签》有写过jsp标签的版本,其实,在这里如果使用的是thymeleaf,也可以自定义拓展一下shiro的hasAnyPermission。
将下载下来的thymeleaf-extras-shiro.jar打开,会有一个shiro-dialect.xml的文件,这里定义了thymeleaf的shiro属性,我们也可以根据这里面的例子进行简单拓展。
这里其实是在拓展thymeleaf,官方文档中有详细的说明和简单例子,对照文档,学习会更快。
原文地址:http://blog.csdn.net/coolcaosj/article/details/40083667