Shiro官方支持多种模板方言,却没有为Thymeleaf提供支持,幸好有第三方提供的thymeleaf-extras-shiro
它与Springboot的整合比较简单,这里就不介绍了。
在与SpringMVC整合时,却遇到了让人头疼的问题,网上到处搜索,一步步调试代码,最终找到了解决方案。
第一步,引用thymeleaf-extras-shiro包
<dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>2.0.0</version> </dependency>
第二步,声明bean
<bean id="shiroDialect" class="at.pollux.thymeleaf.shiro.dialect.ShiroDialect"/>
第三步,将ShiroDialect加入到Thymeleaf模板引擎中
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolvers" ref="templateResolver" /> <property name="additionalDialects" ref="shiroDialect" /> </bean>
第四步,模板头部加入命名空间
<!DOCTYPE html> <html lang="zh_CN" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:th="http://www.thymeleaf.org"> <head> <title>thymeleaf-extras-shiro</title> </head> <body> <p shiro:guest="">Please <a href="login.html">login</a></p> <p shiro:authenticated=""> Hello, <span shiro:principal=""></span>, how are you today? </p> </body> </html>
最后,这里有一些常用Shiro标签
Spring MVC Thymeleaf Shiro Dialect 整合_断问天的博客-CSDN博客
第二步,声明bean
<bean id="shiroDialect" class="at.pollux.thymeleaf.shiro.dialect.ShiroDialect"/>第三步,将ShiroDialect加入到Thymeleaf模板引擎中
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolvers" ref="templateResolver" /> <property name="additionalDialects" ref="shiroDialect" /> </bean>第四步,模板头部加入命名空间
<!DOCTYPE html><html lang="zh_CN" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns:th="http://www.thymeleaf.org"> <head> <title>thymeleaf-extras-shiro</title> </head> <body> <p shiro:guest="">Please <a href="login.html">login</a></p> <p shiro:authenticated=""> Hello, <span shiro:principal=""></span>, how are you today? </p> </body></html>最后,这里有一些常用Shiro标签
Attribute
<p shiro:anyTag> Goodbye cruel World!</p>Element
<shiro:anyTag> <p>Hello World!</p></shiro:anyTag>The guest tag
<p shiro:guest=""> Please <a href="login.html">Login</a></p>The user tag
<p shiro:user=""> Welcome back John! Not John? Click <a href="login.html">here<a> to login.</p>The authenticated tag
<a shiro:authenticated="" href="updateAccount.html">Update your contact information</a>The notAuthenticated tag
<p shiro:notAuthenticated=""> Please <a href="login.html">login</a> in order to update your credit card information.</p>The principal tag
<p>Hello, <span shiro:principal=""></span>, how are you today?</p>or
<p>Hello, <shiro:principal/>, how are you today?</p>Typed principal and principal property are also supported.
The hasRole tag
<a shiro:hasRole="administrator" href="admin.html">Administer the system</a>The lacksRole tag<p shiro:lacksRole="administrator"> Sorry, you are not allowed to administer the system.</p>The hasAllRoles tag
<p shiro:hasAllRoles="developer, project manager"> You are a developer and a project manager.</p>The hasAnyRoles tag
<p shiro:hasAnyRoles="developer, project manager, administrator"> You are a developer, project manager, or administrator.</p>The hasPermission tag
<a shiro:hasPermission="user:create" href="createUser.html">Create a new User</a>The lacksPermission tag
<p shiro:lacksPermission="user:delete"> Sorry, you are not allowed to delete user accounts.</p>The hasAllPermissions tag
<p shiro:hasAllPermissions="user:create, user:delete"> You can create and delete users.</p>The hasAnyPermissions tag
<p shiro:hasAnyPermissions="user:create, user:delete"> You can create or delete users.</p> ————————————————版权声明:本文为CSDN博主「断问天」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/ilyz5609/article/details/102706788
Spring MVC Thymeleaf Shiro Dialect 整合
原文:https://www.cnblogs.com/roak/p/15151840.html